bash脚本为行添加前缀。

时间:2015-08-05 03:20:30

标签: regex bash sed

我有这个数据列表,我按摩了这个。 有些数字丢失了 - 正如第7行以CPB开头所见

232876337    ROK  37,314  37,314  RWCPRTN    116.4767  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:58:51  XNYS  USD  Stock  A4
232875608    CNC  2,000  2,000  WTCO    69.1813  UBS Sales  MONOHAJO  Limit   69.15  29Jul15 14:53:50  XNYS  USD  Stock  A4
232874715    ENS  23,987  23,987  NWQI    62.0525  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:47:32  XNYS  USD  Stock  A4
232872532    CPB  3,000  3,000  STNJ    48.6158  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:35:16  XNYS  USD  Stock  A4
232871384    XEC  2,500  2,500  ZKB    105.4786  UBS Blotter  MONOHAJO  Market   0.00  29Jul15 14:32:39  XNYS  USD  Stock
CPB  3,000  3,000  STNJ    48.6424  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:16:59  XNYS  USD  Stock  A4
232863189    CPB  2,000  2,000  STNJ    48.6533  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:43:10  XNYS  USD  Stock  A4
232861168    CPB  4,000  4,000  STNJ    48.6842  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:26:01  XNYS  USD  Stock  A4
232856253    Y  7,853  7,853  SIM    18.2700  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856254    MEG  48,300  48,300  SIM    14.8200  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856255    FM  107,200  107,200  SIM    11.3000  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856256    TD  34,200  34,200  SIM    52.6400  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4

我在这个黑客脚本中添加了一个前缀。数字是多少并不重要 - 我在其余部分之后 - 不完全是唯一标识符。 他们是一个更优雅的方式 - 没有回应$ line替换为sed。

while read line ; do 
 if  [[ $line =~ ^[0-9] ]] ; then 
   echo $line ; 
 else 
   echo $line | sed -e 's/^/0000000000 /';
fi ; 
done < /tmp/comeandplaywithus > /tmp/danny

2 个答案:

答案 0 :(得分:1)

$ cat /tmp/s
232876337    ROK  37,314  37,314  RWCPRTN    116.4767  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:58:51  XNYS  USD  Stock  A4
232875608    CNC  2,000  2,000  WTCO    69.1813  UBS Sales  MONOHAJO  Limit   69.15  29Jul15 14:53:50  XNYS  USD  Stock  A4
232874715    ENS  23,987  23,987  NWQI    62.0525  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:47:32  XNYS  USD  Stock  A4
232872532    CPB  3,000  3,000  STNJ    48.6158  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:35:16  XNYS  USD  Stock  A4
232871384    XEC  2,500  2,500  ZKB    105.4786  UBS Blotter  MONOHAJO  Market   0.00  29Jul15 14:32:39  XNYS  USD  Stock
CPB  3,000  3,000  STNJ    48.6424  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:16:59  XNYS  USD  Stock  A4
232863189    CPB  2,000  2,000  STNJ    48.6533  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:43:10  XNYS  USD  Stock  A4
232861168    CPB  4,000  4,000  STNJ    48.6842  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:26:01  XNYS  USD  Stock  A4
232856253    Y  7,853  7,853  SIM    18.2700  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856254    MEG  48,300  48,300  SIM    14.8200  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856255    FM  107,200  107,200  SIM    11.3000  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856256    TD  34,200  34,200  SIM    52.6400  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4


$ awk '{ if($1 ~ "[0-9]+") print $0 ; else print "000000000    "$0 }' /tmp/s
232876337    ROK  37,314  37,314  RWCPRTN    116.4767  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:58:51  XNYS  USD  Stock  A4
232875608    CNC  2,000  2,000  WTCO    69.1813  UBS Sales  MONOHAJO  Limit   69.15  29Jul15 14:53:50  XNYS  USD  Stock  A4
232874715    ENS  23,987  23,987  NWQI    62.0525  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:47:32  XNYS  USD  Stock  A4
232872532    CPB  3,000  3,000  STNJ    48.6158  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:35:16  XNYS  USD  Stock  A4
232871384    XEC  2,500  2,500  ZKB    105.4786  UBS Blotter  MONOHAJO  Market   0.00  29Jul15 14:32:39  XNYS  USD  Stock
000000000    CPB  3,000  3,000  STNJ    48.6424  UBS Sales  MONOHAJO  Market   0.00  29Jul15 14:16:59  XNYS  USD  Stock  A4
232863189    CPB  2,000  2,000  STNJ    48.6533  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:43:10  XNYS  USD  Stock  A4
232861168    CPB  4,000  4,000  STNJ    48.6842  UBS Sales  MONOHAJO  Market   0.00  29Jul15 13:26:01  XNYS  USD  Stock  A4
232856253    Y  7,853  7,853  SIM    18.2700  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856254    MEG  48,300  48,300  SIM    14.8200  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856255    FM  107,200  107,200  SIM    11.3000  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4
232856256    TD  34,200  34,200  SIM    52.6400  Fix Cad Blotter  MONOHAJO  Market   0.00  29Jul15 12:46:50  XTSE  CAD  Stock  A4

答案 1 :(得分:0)

Sed本身就完全做到了

sed '/^[0-9]\{9\}/! s/^/000000000/' YourFile

从9位(!)开始选择不是(/^[0-9]\{9\}/)的行,并在这种情况下添加9(仅限)