我有这个文件:
# cat pt_Sed.txt
ULNET-PA,broker_etrade,broker_etrade_uk^M,BR DELIVERTOCOMPID
ULNET-PA,broker_etrade,broker_etrade_uk^M,CO TARGETSUBID
我想排列这些线条,使它们看起来像这样:
ULNET-PA,broker_etrade,broker_etrade_uk^M,DELIVERTOCOMPID=BR
ULNET-PA,broker_etrade,broker_etrade_uk^M,TARGETSUBID=CO
我尝试了以下操作,但似乎没有按预期工作:
# x=`awk '{ print $NF }' pt_Sed2.txt`
# echo $x
# DELIVERTOCOMPID
# sed -re "s/\^M,*/&$x=/g" -e 's/ .*//' < pt_Sed.txt
# ULNET-PA,broker_etrade,broker_etrade_uk^M,DELIVERTOCOMPID=BR
# ULNET-PA,broker_etrade,broker_etrade_uk^M,DELIVERTOCOMPID=CO
请您提出解决方案吗? 我非常感谢!
谢谢, 安德烈
答案 0 :(得分:0)
您可以使用awk:
awk -F '[ ,]+' '{$0=$0 "=" $(NF-1); $(NF-1)=""; gsub(/,,/, ",")} 1' OFS=, file
<强>测试强>
awk -F '[ ,]+' '{$0=$0 "=" $(NF-1); $(NF-1)=""; gsub(/,,/, ",")} 1' OFS=, file | cat -vte
ULNET-PA,broker_etrade,broker_etrade_uk^M,DELIVERTOCOMPID=BR$
ULNET-PA,broker_etrade,broker_etrade_uk^M,TARGETSUBID=CO$
答案 1 :(得分:0)
这是一个sed版本:
sed 's/\(.*,\)\([^[:space:]]*\)[[:space:]]*\(.*\)/\1\3=\2/' pt_Sed.txt
答案 2 :(得分:0)
$ sed -r 's/,([^[:space:],]+)[[:space:]]+(.*)/,\2=\1/' file
ULNET-PA,broker_etrade,broker_etrade_uk^M,DELIVERTOCOMPID=BR
ULNET-PA,broker_etrade,broker_etrade_uk^M,TARGETSUBID=CO