Sed命令在bash中使用非常简单的多行正则表达式乱码

时间:2014-09-30 07:03:05

标签: regex bash sed

我再次使用sed命令乱码,因为我很可能有sed的旧版本但是根据我的限制我无法更改'sed'的版本(!)

我的问题是我写了这么简单的正则表达式,适合我的字符串文件,如:

/[^,]*$/mg

我的字符串文件是:

23:53:20,650
23:53:20,654
23:53:20,655
23:53:20,656
23:53:21,238
23:53:21,240
23:53:21,302
23:53:21,303
23:53:21,304
23:53:21,305
23:53:21,889
23:53:21,890
23:53:21,896
23:53:21,897
23:53:21,898
23:53:21,899
23:53:22,492
23:53:22,538
23:53:22,539
23:53:23,109
23:53:23,110
23:53:23,115
23:53:23,117
23:53:23,118
23:53:23,119
23:53:23,690
23:53:23,721
23:53:23,722
23:53:24,275
23:53:24,276
23:53:24,313
23:53:24,316
23:53:24,317
23:53:24,318
23:53:24,854
23:53:24,888
23:53:24,889
23:53:24,890
23:53:24,891
23:53:50,676
23:53:50,677
23:53:50,711
23:53:50,713
23:53:50,714
23:53:51,257
23:53:51,258
23:53:51,296
23:53:51,297
23:53:51,298
23:53:51,820
23:53:51,822
23:53:51,823
23:53:52,358
23:53:52,364
23:53:52,367
23:53:52,909
23:53:52,910
23:53:52,936
23:53:52,939
23:53:52,941
23:53:52,944
23:53:52,945
23:53:52,946
23:53:52,949
23:53:52,953
23:53:52,956
23:53:52,959
23:53:52,963
23:53:52,966
23:53:52,970
23:53:52,971
23:53:52,974
23:53:52,978
23:53:52,980
23:53:52,983
23:53:52,984
23:53:52,986
23:53:52,987
23:53:52,989
23:53:52,990
23:53:52,991
23:53:52,994
23:53:52,995
23:53:52,999
23:53:53,001
23:53:53,002
23:53:53,004
23:53:53,005
23:53:53,007
23:53:53,010
23:53:53,026
23:53:53,027
23:53:53,081
23:53:53,082
23:53:53,083
23:53:53,085
07:32:54,519
07:32:54,521
07:32:54,537
07:32:54,538
07:32:54,539
07:32:54,540
07:32:54,541
07:32:54,542
07:32:54,543
07:32:54,544
07:32:54,545
07:32:54,546
07:32:54,547
07:32:54,548
07:32:54,549
07:32:54,550

我正在尝试获取逗号之后的值,然后将它们分配到数组中,当我使用sed命令时:

`sed -n '/[^,]*$/mg'` file 

它说命令乱码,我读到多线sed,但我仍然无法达到解决方案,我是新的正则表达式所以我们将非常感谢帮助。

提前谢谢!

3 个答案:

答案 0 :(得分:1)

如果你正在使用"最近的" bash,我认为你可以使用剪切并将提取的值分配给数组:

numbers="$(cut -d',' -f2 filename.txt)"
array_numbers=( $numbers )

答案 1 :(得分:0)

如果你想在逗号之后获取值,那么你可以使用下面的sed命令从开头到第一个逗号中删除值。

sed 's/^[^,]*,//' file

OR

sed 's/^.*,//' file

示例:

$ echo '23:53:22,492' | sed 's/^[^,]*,//'
492
$ echo '23:53:22,492' | sed 's/^.*,//'
492

答案 2 :(得分:0)

sed s/.*,// file 匹配直到第一个,替换匹配什么都没有,这有效地给出逗号后的值

表示输入文件

23:53:20,650
23:53:20,654
23:53:20,655
23:53:20,656
23:53:21,238
23:53:21,240
23:53:21,302
23:53:21,303
23:53:21,304
23:53:21,305
23:53:21,889
23:53:21,890
23:53:21,896
23:53:21,897
23:53:21,898
23:53:21,899
23:53:22,492
23:53:22,538

将产生输出

650
654
655
656
238
240
302
303
304
305
889
890
896
897
898
899
492
538