![첫 번째 ','를 제외한 모든 항목을 "로 변경합니다."파일의 각 줄에 대해 (bash) [중복]](https://linux55.com/image/139304/%EC%B2%AB%20%EB%B2%88%EC%A7%B8%20'%2C'%EB%A5%BC%20%EC%A0%9C%EC%99%B8%ED%95%9C%20%EB%AA%A8%EB%93%A0%20%ED%95%AD%EB%AA%A9%EC%9D%84%20%22%EB%A1%9C%20%EB%B3%80%EA%B2%BD%ED%95%A9%EB%8B%88%EB%8B%A4.%22%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EA%B0%81%20%EC%A4%84%EC%97%90%20%EB%8C%80%ED%95%B4%20(bash)%20%5B%EC%A4%91%EB%B3%B5%5D.png)
저는 bash를 사용하고 있으며 데이터의 두 열(App, Blurb)만 필요한 csv 파일(dat.csv)이 있지만 각 행에 ","가 많기 때문에 많은 열이 됩니다.
문제가 있는 csv.dat의 예:
App , Blurb
diff, this is the diff program, bla bla bla, yadda yadda
word, this is ms product, it is not very good, I dont like it
dd, this is a Linux disk application , its awesome!, bla bla, ttly
...
내가 겪고 있는 문제는 'Blurb' 열에 추가 ''가 있기 때문에 데이터가 dat.csv 파일의 후속 열(c, d 등)로 파이프된다는 것입니다.
목표는 각 행의 첫 번째 ","를 제외한 모든 항목을 "COMMA"로 변경하여 모든 "Blurb" 데이터가 B열에 유지되도록 하는 것입니다.
예를 들어 원하는 출력은 다음과 같습니다.
App, Blurb
diff, this is the diff program<COMMMA> bla bla bla<COMMA> yadda yadda
word, this is ms product<COMMA> it is not very good<COMMA> I dont like it
dd, this is a Linux disk application <COMMA> its awesome!<COMMA>bla bla<COMMA> ttly
...
감사해요!
답변1
GNU 사용 sed
:
sed 's/,/<COMMA>/2g' infile
또는 이식성을 위해:
sed 's/,/<COMMA>/g; s/<COMMA>/,/' infile
답변2
당신은 또한 이것을 할 수 있습니다POSIX-라이다음과 같이:
sed -e '
y/,/\n/ ;# change all commas to newlines, which are guaranteed to not be there
s/\n/,/ ;# then change the first of those newlines to a comma, i.e., restore
s//<COMMA>/g ;# and all the remaining newline(s) change to <COMMA>
' dat.csv
답변3
어쩌면 필드 주위에 따옴표를 넣을 수도 있습니다. 이렇게 하면 내부의 쉼표가 필드 구분 기호가 아니라는 것을 csv 파서에 알릴 수 있습니다.
sed 's/"/""/g; # escape existing " as ""
s/[[:space:]]*,[[:space:]]*/","/; # replace the first , and the
# whitespace around it with ","
s/^[[:space:]]*/"/; # add a " at the start (and
# get rid of whitespace there)
s/[[:space:]]*$/"/; # same at the end'