
CSV 파일을 조옮김/회전해야 합니다. 이것이 가능한지 모르겠습니다
이 CSV 파일의 내용을 가정합니다.
filename;rating;id;summary
S4348gjO.doc;good;0001;describing how to reach your goals
S4348gjO.doc;good;0002;some recipes for avoiding an argument
S4348gjO.doc;bad;0003;boring part of the page
A234HK.doc;fairly good;0001;how to deploy a server
A234HK.doc;bad;0002;start and stop the server
출력은 다음과 같아야 합니다.
filename;good;fairly good;bad;id
S4348gjO.doc;describing how to reach your goals; ; ;0001
S4348gjO.doc;some recipes for avoiding an argument; ; ;0002
S4348gjO.doc; ; ;boring part of the page;0003
A234HK.doc; ;how to deploy a server; ;0001
A234HK.doc; ; ;start and stop the server;0002
답변1
원하는 내용은 다음과 같습니다.
awk 'BEGIN{FS=OFS=";"}
FNR==1{print "filename;good;fairly good;bad;id"}
$2=="good"{print $1, $4, " ", " ", $3}
$2=="fairly good"{print $1, " ", $4, " ", $3}
$2=="bad"{print $1, " ", " ", $4, $3}' infile
따라서 해당 등급 열에는 요약이 포함되어 있으며 나머지 두 열에는 공백이 표시됩니다(예를 들어 빈 필드가 필요한 경우 로 바꾸십시오 " "
) ""
.
답변2
살짝 수정해 보세요don_crissti의 스크립트
awk -F\; '
BEGIN{
P["good"]="%s;%s;;;%s\n"
P["fairly good"]="%s;;%s;;%s\n"
P["bad"]="%s;;;%s;%s\n"
}
FNR==1{
print "filename;good;fairly good;bad;id"
next
}
{
printf(P[$2],$1,$4,$3)
}
' infile
답변3
필드를 키 필드로 사용하고 필드를 값 필드로 사용하여 데이터를 회전하려면 reshape
Miller( ) 하위 명령을 사용합니다 . mlr
그런 다음 각 레코드에서 누락된 새 필드에 null 값을 할당하고 질문에 표시된 순서대로 필드를 재정렬합니다.rating
summary
$ mlr --csv --fs ';' reshape -s rating,summary then unsparsify then reorder -f filename,good,'fairly good',bad,id file
filename;good;fairly good;bad;id
S4348gjO.doc;describing how to reach your goals;;;0001
S4348gjO.doc;some recipes for avoiding an argument;;;0002
S4348gjO.doc;;;boring part of the page;0003
A234HK.doc;;how to deploy a server;;0001
A234HK.doc;;;start and stop the server;0002
질문에 표시된 대로 누락된 값을 공백으로 채웁니다.
$ mlr --csv --fs ';' reshape -s rating,summary then unsparsify --fill-with ' ' then reorder -f filename,good,'fairly good',bad,id file
filename;good;fairly good;bad;id
S4348gjO.doc;describing how to reach your goals; ; ;0001
S4348gjO.doc;some recipes for avoiding an argument; ; ;0002
S4348gjO.doc; ; ;boring part of the page;0003
A234HK.doc; ;how to deploy a server; ;0001
A234HK.doc; ; ;start and stop the server;0002