我有一个模式为'21pro_ABCD_EDG_10800_48052_2 0.0‘的文件。
如何将_0-9替换为,(逗号),以便输出为
21pro_ABCD_EDG,10800,48052,2,0.0
发布于 2011-07-13 04:03:38
要将_[0-9]替换为,,您可以执行this
$s =~ s/_([0-9])/,$1/g
#the same without capturing groups
$s =~ s/_(?=[0-9])/,/g;编辑:要获取2后面的额外逗号,可以执行以下操作:
#This puts a , before all whitespace.
$s =~ s/_(?=[0-9])|(?=\s)/,/g;
#This one puts a , between [0-9] and any whitespace
$s =~ s/_(?=[0-9])|(?<=[0-9])(?=\s)/,/g;发布于 2011-07-13 04:05:03
sed方法类似于以下内容:
rupert@hake:~ echo '21pro_ABCD_EDG_10800_48052_2 0.0' | sed 's/_\([0-9]\)/,\1/g'
21pro_ABCD_EDG,10800,48052,2 0.0发布于 2011-07-13 20:58:18
使用雅各布提到的表达式,下面是执行大文件替换的代码片段
#!/usr/local/bin/perl
open (MYFILE, 'test');
while (<MYFILE>) {
chomp;
$s=$_;
$s =~ s/_(?=[0-9])|(?<=[0-9])(?=\s)/,/g;
$s =~ s/\s//g;
print "$s\n";
}关闭(MYFILE);
https://stackoverflow.com/questions/6670318
复制相似问题