我收集了很长的记录作为代码的输出,
Part1=STS768976N-4BT345432-X00001-87675789AAAS Session=9
Part2=STS876578N-2BT234522-X00002-23456789UJHG Session=10
Part3=ST9876887N-1BT876577-X00003-65778978KJUH Session=11
Session=10 70000
Session=11 80000
Session=9 90000
我想这样组织数据
Part1=STS768976N-4BT345432-X00001-87675789AAAS 90000
Part2=STS876578N-2BT234522-X00002-23456789UJHG 70000
Part3=ST9876887N-1BT876577-X00003-65778978KJUH 80000
我怎样才能做到这一点
答案1
awk
可以做到:
awk '/Part/{i[$2]=$1;next}$1 in i{print i[$1],$2}' file | sort -t= -k1
Part1=STS768976N-4BT345432-X00001-87675789AAAS 90000
Part2=STS876578N-2BT234522-X00002-23456789UJHG 70000
Part3=ST9876887N-1BT876577-X00003-65778978KJUH 80000
答案2
此方法需要对文件进行 2 次传递:一次收集 id,然后再次打印结果
awk '
NR==FNR {if (/^Session=/) id[$1] = $2; next}
/^Part/ {$NF = id[$NF]; print}
' file file
答案3
for i in Session=9 Session=10 Session=11; do sed -n '/'$i'/p' j.txt| sed "N;s/\n/ /g"| sed 's/'$i'//g'; done
Part1=STS768976N-4BT345432-X00001-87675789AAAS 90000
Part2=STS876578N-2BT234522-X00002-23456789UJHG 70000
Part3=ST9876887N-1BT876577-X00003-65778978KJUH 80000