0

我有这个文件:

# 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

你能提出一个解决方案吗?我将不胜感激!

谢谢,安德烈

4

3 回答 3

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$
于 2013-10-15T11:10:30.373 回答
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
于 2013-10-15T11:54:53.737 回答
0

这是一个 sed 版本:

sed 's/\(.*,\)\([^[:space:]]*\)[[:space:]]*\(.*\)/\1\3=\2/' pt_Sed.txt
于 2013-10-15T11:45:36.490 回答