Programing

awk를 사용하여 바이트 순서 표시 제거

lottogame 2020. 8. 15. 09:47
반응형

awk를 사용하여 바이트 순서 표시 제거


BOMawk 을 제거 하는 스크립트 (아마 한 줄짜리)는 어떻게 생겼습니까?

사양:

  • 첫 번째 ( NR > 1) 이후의 모든 행을 인쇄합니다.
  • 첫 번째 줄 : #FE #FF또는로 시작하는 경우 #FF #FE제거하고 나머지를 인쇄합니다.

이 시도:

awk 'NR==1{sub(/^\xef\xbb\xbf/,"")}{print}' INFILE > OUTFILE

첫 번째 레코드 (행)에서 BOM 문자를 제거하십시오. 모든 기록을 인쇄합니다.

또는 awk의 기본 작업이 레코드를 인쇄하는 것이라는 지식을 사용하면 약간 더 짧습니다.

awk 'NR==1{sub(/^\xef\xbb\xbf/,"")}1' INFILE > OUTFILE

1 항상 참으로 평가되는 가장 짧은 조건이므로 각 레코드가 인쇄됩니다.

즐겨!

-부록-

유니 코드 BOM (Byte Order Mark) FAQ 에는 각 인코딩에 대한 정확한 BOM 바이트가 나열된 다음 표가 포함되어 있습니다.

Bytes         |  Encoding Form
--------------------------------------
00 00 FE FF   |  UTF-32, big-endian
FF FE 00 00   |  UTF-32, little-endian
FE FF         |  UTF-16, big-endian
FF FE         |  UTF-16, little-endian
EF BB BF      |  UTF-8

따라서 위 표에서 BOM 바이트에 \xef\xbb\xbf해당하는 방법을 확인할 수 있습니다 EF BB BF UTF-8.


GNU 사용 sed(Linux 또는 Cygwin) :

# Removing BOM from all text files in current directory:
sed -i '1 s/^\xef\xbb\xbf//' *.txt

FreeBSD에서 :

sed -i .bak '1 s/^\xef\xbb\xbf//' *.txt

GNU 또는 FreeBSD 사용의 장점 sed: -i매개 변수는 "제자리"를 의미하며 리디렉션이나 이상한 속임수없이 파일을 업데이트합니다.

Mac :

awk다른 답변 의이 솔루션은 작동 하지만 sed명령은 작동하지 않습니다. 적어도 Mac (Sierra) sed문서에서는 16 진수 이스케이프 ala 지원에 대해 언급하지 않습니다 \xef.

moreutils 에서 sponge도구 로 파이핑하여 모든 프로그램에서 유사한 트릭을 얻을 수 있습니다 .

awk '…' INFILE | sponge INFILE

어색하지는 않지만 더 간단합니다.

tail -c +4 UTF8 > UTF8.nobom

BOM을 확인하려면 :

hd -n 3 UTF8

BOM이있는 경우 다음이 표시됩니다. 00000000 ef bb bf ...


CRLF 줄 끝을 LF로 변환하는 것 외에도 dos2unixBOM도 제거합니다.

dos2unix *.txt

dos2unix also converts UTF-16 files with a BOM (but not UTF-16 files without a BOM) to UTF-8 without a BOM:

$ printf '\ufeffä\n'|iconv -f utf-8 -t utf-16be>bom-utf16be
$ printf '\ufeffä\n'|iconv -f utf-8 -t utf-16le>bom-utf16le
$ printf '\ufeffä\n'>bom-utf8
$ printf 'ä\n'|iconv -f utf-8 -t utf-16be>utf16be
$ printf 'ä\n'|iconv -f utf-8 -t utf-16le>utf16le
$ printf 'ä\n'>utf8
$ for f in *;do printf '%11s %s\n' $f $(xxd -p $f);done
bom-utf16be feff00e4000a
bom-utf16le fffee4000a00
   bom-utf8 efbbbfc3a40a
    utf16be 00e4000a
    utf16le e4000a00
       utf8 c3a40a
$ dos2unix -q *
$ for f in *;do printf '%11s %s\n' $f $(xxd -p $f);done
bom-utf16be c3a40a
bom-utf16le c3a40a
   bom-utf8 c3a40a
    utf16be 00e4000a
    utf16le e4000a00
       utf8 c3a40a

I know the question was directed at unix/linux, thought it would be worth to mention a good option for the unix-challenged (on windows, with a UI).
I ran into the same issue on a WordPress project (BOM was causing problems with rss feed and page validation) and I had to look into all the files in a quite big directory tree to find the one that was with BOM. Found an application called Replace Pioneer and in it:

Batch Runner -> Search (to find all the files in the subfolders) -> Replace Template -> Binary remove BOM (there is a ready made search and replace template for this).

It was not the most elegant solution and it did require installing a program, which is a downside. But once I found out what was going around me, it worked like a charm (and found 3 files out of about 2300 that were with BOM).

참고URL : https://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-mark

반응형