我有一个名称为
的文件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"
但没有成功?
答案 0 :(得分:2)
您可以执行以下操作:
$ 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
答案 1 :(得分:2)
只是打击:
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