Linux에서 여러 값 세트를 행으로 바꿀 수 있나요?
아래는 제가 가지고 있는 샘플 데이터입니다.
UID,UNAME,UKEY,UFROMDATE,UTODATE,UL,ULID,UF,UFID
"1","abc","12344321","2019-01-01","2019-01-10","1","1A","X1","XA"
"2","abc","12344322","2019-02-01","2019-02-10","2","2A","X2","XB"
위의 데이터를 다음 형식으로 변환할 수 있나요?
UID,UNAME,UKEY,UFROMDATE,UTODATE,ColVal,ID,ColName
"1","abc","12344321","2019-01-01","2019-01-10","1","1A",UL
"1","abc","12344321","2019-01-01","2019-01-10","X1","XA",UF
"2","abc","12344322","2019-02-01","2019-02-10","2","2A",UL
"2","abc","12344322","2019-02-01","2019-02-10","X2","XB",UF
어떤 제안이라도 도와주세요..
답변1
인용된 문자열에 쉼표가 없다고 가정하면 awk를 사용하여 필드 구분 기호를 쉼표로 설정할 수 있습니다. 그런 다음 헤더 행을 수동으로 인쇄하고 각 행에 대해 열 1~7, 1-5,8-9를 인쇄합니다.
$ awk -F, -vOFS=, 'NR == 1 {print "UID,UNAME,UKEY,UFROMDATE,UTODATE,ColVal,ID,ColName"};
NR != 1 {print $1, $2, $3, $4, $5, $6, $7, "UL";
print $1, $2, $3, $4, $5, $8, $9, "UF"; }' < file.txt
UID,UNAME,UKEY,UFROMDATE,UTODATE,ColVal,ID,ColName
"1","abc","12344321","2019-01-01","2019-01-10","1","1A",UL
"1","abc","12344321","2019-01-01","2019-01-10","X1","XA",UF
"2","abc","12344322","2019-02-01","2019-02-10","2","2A",UL
"2","abc","12344322","2019-02-01","2019-02-10","X2","XB",UF
답변2
sed와 관련된 더 더러운 것들 중 일부
$ echo "UID,UNAME,UKEY,UFROMDATE,UTODATE,ColVal,ID,ColName" && sed -E '1d; s|"([0-9]+",.+),("[0-9]","[0-9][A-Z]"),("X[0-9]","X[A-Z]")$|\1,\2,UL\n\1,\3,UF|' file.txt
결과
UID,UNAME,UKEY,UFROMDATE,UTODATE,ColVal,ID,ColName
1","abc","12344321","2019-01-01","2019-01-10","1","1A",UL
1","abc","12344321","2019-01-01","2019-01-10","X1","XA",UF
2","abc","12344322","2019-02-01","2019-02-10","2","2A",UL
2","abc","12344322","2019-02-01","2019-02-10","X2","XB",UF