我是 Unix 新手。
我有以下文件:
AB 123-01 vl 278
AB 123-01 na "aa"
AB 123-01 eg 5,6
AB 445-01 vl 521
AB 445-01 na "cd"
AB 445-01 eg 7,6
AB 945-01 vl 215
AB 945-01 na "Discreet"
AB 945-01 eg 1,6
我想得到以下输出:
AB 123-01,278,aa,5,6
AB 445-01,521,cd,7,6
AB 945-01,215,Discreet,1,6
用逗号把三行变成一行(总是三行) 并删除单词:vl, na, eg
最佳答案
如果您更喜欢单行代码,可以使用 awk。
输入文件名.txt
AB 123-01 vl 278
AB 123-01 na "aa"
AB 123-01 eg 5,6
AB 445-01 vl 521
AB 445-01 na "cd"
AB 445-01 eg 7,6
AB 945-01 vl 215
AB 945-01 na "Discreet"
AB 945-01 eg 1,6
代码:
awk 'BEGIN {RS=NULL} {printf("%s %s,%s,%s,%s\n", $1, $2, $4, substr($8,2,length($8)-2), $12)}' filename.txt
输出
AB 123-01,278,aa,5,6
AB 445-01,521,cd,7,6
AB 945-01,215,Discreet,1,6
关于linux - 在linux中将三行转换为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723820/