我想要一个替换,我想在一个看起来像这样的文件的行上做:
aoipp;dadada.12312;ss;1245454;Xiop;12.12;45.3;47.897;31.5;
asdfafd;14355.54664;peasd;125.1;900.2;76.897;67.456;asdfdf;
perio;777.2;ipoes;900.34;2;1980.45;870.98;67.67;
我希望将每个.
替换为,
,但仅在第五次出现分隔符;
之后。其他一切都需要保持不变。所以所需的输出文件如下所示:
aoipp;dadada.12312;ss;1245454;Xiop;12,12;45,3;47,897;31,5;
asdfafd;14355.54664;peasd;125.1;900.2;76,897;67,456;asdfdf;
perio;777.2;ipoes;900.34;2;1980,45;870,98;67,67;
我有兴趣主要在perl中这样做,所以我可以将它合并到一个更大的程序中,但是bash / awk中的任何解决方案也是受欢迎的。提前谢谢。
答案 0 :(得分:3)
这个awk单行应该适合你:
=MONTH(B1)
从第6个字段(awk -F';' -v OFS=";" '{for(i=6;i<=NF;i++)gsub("[.]",",",$i)}7' file
分隔)开始,每个字段将所有;
替换为.
。
使用您的数据进行测试:
,
答案 1 :(得分:3)
我使用数组切片@fields[ 5 .. $#fields ]
来仅访问要更改的元素。
#!/usr/bin/perl
use warnings;
use strict;
my @input = qw( aoipp;dadada.12312;ss;1245454;Xiop;12.12;45.3;47.897;31.5;
asdfafd;14355.54664;peasd;125.1;900.2;76.897;67.456;asdfdf;
perio;777.2;ipoes;900.34;2;1980.45;870.98;67.67;
);
my @expected = qw( aoipp;dadada.12312;ss;1245454;Xiop;12,12;45,3;47,897;31,5;
asdfafd;14355.54664;peasd;125.1;900.2;76,897;67,456;asdfdf;
perio;777.2;ipoes;900.34;2;1980,45;870,98;67,67;
);
sub process {
my (@input) = @_;
my @output;
for my $line (@input) {
my @fields = split /;/, $line;
s/\./,/ for @fields[ 5 .. $#fields ];
push @output, join ';', @fields, q();
}
return \@output
}
use Test::More tests => 1;
is_deeply(process(@input), \@expected);
答案 2 :(得分:3)
while (my $line = <DATA>) {
if ($line =~ /^(?:[^;]*;){5}/) {
substr($line, $+[0]) =~ y/./,/;
}
print $line;
}
__DATA__
aoipp;dadada.12312;ss;1245454;Xiop;12.12;45.3;47.897;31.5;
asdfafd;14355.54664;peasd;125.1;900.2;76.897;67.456;asdfdf;
perio;777.2;ipoes;900.34;2;1980.45;870.98;67.67;
答案 3 :(得分:1)
perl -pe 's/(.*?;){6}\K(.*)/$2 =~ s!\.!,!rg /ge'
;
((.*?;){6}\K
),$2 =~ s!\.!,!rg
)答案 4 :(得分:0)
# this should do your work
sed -i 's/;/,/6g' filename
cat filename
aoipp;dadada.12312;ss;1245454;Xiop;12.12,45.3,47.897,31.5,
asdfafd;14355.54664;peasd;125.1;900.2;76.897,67.456,asdfdf,
perio;777.2;ipoes;900.34;2;1980.45,870.98,67.67,