bash는 변수의 특수 문자를 바꿉니다.

bash는 변수의 특수 문자를 바꿉니다.

파이프(|)는 처리를 위해 Windows 응용 프로그램에서 텍스트 파일을 전송하는 것을 제한합니다. 처리시 파일의 첫 번째 행과 첫 번째 열에 특수 문자가 있습니다. Windows에서 전송하기 전 파일이 메모장에서 보이는 모습입니다.

Sector|Name|Manager|...

을 읽으면 IFS='|' read -r -a fields < "/uploads/file_data.txt"첫 번째 열 섹터가 "Sector"특수 문자 접두어로 읽혀집니다.

이렇게 하면 head -1 "/uploads/file_data.txt" | od -c인쇄되는 값은 다음과 같습니다.

0000000 357 273 277   S   e   c   t   o   r   |

나는 시도했지만 tr -d < //uploads/file_data.txt > /uploads/file_data_temp.txt도움이 되지 않았습니다. 향후 업로드되는 파일에 알 수 없는 문자가 있는 경우 특수 문자뿐만 아니라 어떻게 대체할 수 있습니까?

답변1

시스템의 "little-endian"/"big-endian" 특성을 지정하기 위해 유니코드 로케일 기반 시스템에서 사용되는 "bom"(바이트 순서 표시)이 있을 수 있습니다.

바라보다https://en.wikipedia.org/wiki/Byte_order_mark

고맙게도 이것은 utf-8 로케일에서 작동하는 것 같습니다. 이는 ASCII 1-177 문자만 예상하는 경우 좋은 것입니다...

다음을 "확인"하기 위해 (일시적으로) C 로케일을 사용하도록 강제되는 sed를 삽입하여 이를 제거할 수 있습니다.

LC_ALL=C sed '1s/^\xEF\xBB\xBF//' 

예를 들어 다음과 같이 사용됩니다.

incoming program | LC_ALL=C sed '1s/^\xEF\xBB\xBF//' | somecmd
 # or
< incomingfile LC_ALL=C sed '1s/^\xEF\xBB\xBF//' > outputfile
  #  <incomingfile  : will give "incomingfile" content as stdin to sed 
  # then sed modifies only the first line, replacing the BOM with ""
  #    (the rest is not touched by sed and is transmitted as-is)
  #  > outputfile : directs sed output (ie, incomingfile without the BOM) to "outputfile"

관련 정보