[英]Transposing a text file in bash
我有一个文件名为
Input1 file1:45764 | file1:878755 | file1: 899787
Input2 file1: 45676 | file1:769678 | file1: 6454764
现在我想这样做
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764
有猜到吗? 我试过sed "s/s/n/g"
和sed "s/s+/n/g"
但没有成功吗?
您可以执行以下操作:
$ awk -F' [|] ' '{split($1,fld,/ /);print $1;for(i=2;i<=NF;i++)print fld[1],$i}' file
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764
或者习惯上,您可以:
$ awk 'gsub(/[|]/,ORS $1)' file
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764
只是猛击:
while read input line; do
IFS="|" read -a words <<< "$line"
printf "$input %s\n" "${words[@]}"
done << END
Input1 file1:45764 | file1:878755 | file1: 899787
Input2 file1: 45676 | file1:769678 | file1: 6454764
END
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.