複数の列セットを行に置き換える方法

複数の列セットを行に置き換える方法

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

関連情報