이 파일 "origin.txt"를 변환하는 bash 스크립트가 있습니다.
cxx-yyy-zzz-999-111
2018-01-1T00:10:54.412Z
2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt
2018-01-1T00:41:38.867Z
2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123
2018-01-1T00:12:37.152Z
2018-01-5T00:12:44.307Z
도착하다
cxx-yyy-zzz-999-111,2018-01-1T00:10:54.412Z,2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt,2018-01-1T00:41:38.867Z,2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123,2018-01-1T00:12:37.152Z,2018-01-5T00:12:44.307Z
AWK를 사용하여 bash에서 이 작업을 어떻게 수행할 수 있나요?
답변1
tmp.awk
다음과 같은 awk 스크립트 파일이 있습니다.
BEGIN {
i=0
}
{
if (i==0) {
f1=$0
i++
} else if (i==1) {
f2=$0
i++
} else {
i=0
print f1","f2","$0
}
}
origin.txt
그리고 다음 내용을 포함하는 파일
cxx-yyy-zzz-999-111
2018-01-1T00:10:54.412Z
2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt
2018-01-1T00:41:38.867Z
2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123
2018-01-1T00:12:37.152Z
2018-01-5T00:12:44.307Z
다음은 awk 명령과 샘플 출력입니다.
zb@server ~ $ awk -f tmp.awk origin.txt
cxx-yyy-zzz-999-111,2018-01-1T00:10:54.412Z,2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt,2018-01-1T00:41:38.867Z,2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123,2018-01-1T00:12:37.152Z,2018-01-5T00:12:44.307Z
답변2
sed를 사용하세요:
sed 'N;N;s/\n/,/g' origin.txt
awk를 사용하세요:
awk '{ printf "%s", $0; if (NR % 3 == 0) print ""; else printf "," }' origin.txt
산출:
cxx-yyy-zzz-999-111 2018-01-1T00:10:54.412Z 2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt 2018-01-1T00:41:38.867Z 2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123 2018-01-1T00:12:37.152Z 2018-01-5T00:12:44.307Z