![Bash를 사용하여 두 단어 사이의 쉼표(,)를 제거하는 방법](https://linux55.com/image/115803/Bash%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EB%91%90%20%EB%8B%A8%EC%96%B4%20%EC%82%AC%EC%9D%B4%EC%9D%98%20%EC%89%BC%ED%91%9C(%2C)%EB%A5%BC%20%EC%A0%9C%EA%B1%B0%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
두 단어 사이의 쉼표(,)를 제거하는 방법은 무엇입니까?
어떻게 이 두 단어를 서로 다른 두 줄에 넣을 수 있나요?
이것은 내 입력입니다.
ent0
ent4
ent1,ent5
ent2,ent6
ent3,ent7
ent29,ent30
답변1
tr ',' '\n'
입력 파일의 모든 s를 개행 문자로 바꾸는데 ,
, 이는 원하는 대로 들립니다.
답변2
사용 tr
:
$ tr ',' '\n' < FILE
ent0
ent4
ent1
ent5
ent2
ent6
ent3
ent7
ent29
ent30
답변3
사용 bash
:
#!/bin/bash
while IFS='' read -r line; do
echo "${line//,/$'\n'}";
done <infile.txt
또는sed
sed -e $'s/,/\\\n/g' infile.txt
또는
sed 's/,/\
/g' infile.txt
답변4
cut
다음과 같이 사용할 수 있습니다 .
귀하의 데이터가 어디서 나오는지 모르겠습니다. 하지만 다음 주소로 귀하의 정보를 보내주실 수 있을 것으로 확신합니다 cut
.
cat yourfile.data | cut -d, --output-delimiter ' ' -f 1,2
~에 따르면 man cut
...
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
--output-delimiter=STRING
use STRING as the output delimiter the default is to use the input delimiter
-f, --fields=LIST
select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
...
그러나 추가 논리를 추가해야 하는 경우 sed
@RomanPerekhrest가 말한 내용을 사용하는 것이 좋습니다. 강력한 정규식을 사용하면 데이터를 보다 유연하게 구문 분석할 수 있습니다. 내 생각에는 이 작업을 수행하는 가장 좋은 정규식은 다음과 같습니다.
'/^\s*\([^,]\+\)\s*,\s*([^,]\+\)\s*$/'
~ 0개 이상의 공백 뒤에 쉼표를 제외한 모든 항목, 0개 이상의 공백, 쉼표, 0개 이상의 공백 뒤에 쉼표를 제외한 모든 항목, 0개 이상의 공백이 옵니다.