第三栏的反向文本

时间:2011-03-17 07:49:14

标签: perl sed awk grep

我已将SQL数据导出为Tab separted或逗号分隔格式的文本。

2       AX      Mariehamn       Mariehamn       ACTIVE  2011-03-15 17:23:12     2011-03-15 17:23:12     SYSTEM  Al-Ma
4       DZ      Adrar   Adrar   ACTIVE  2011-03-15 17:23:12     2011-03-15 17:23:12     SYSTEM  Al-Ad
6       DZ      Ain Defla       Ain Defla       ACTIVE  2011-03-15 17:23:12     2011-03-15 17:23:12     SYSTEM  Al-Ai
8       DZ      Ain Temchent  Ain Temchent  ACTIVE  2011-03-15 17:23:12     2011-03-15 17:23:12     SYSTEM  Al-Ai
10      DZ      Shantanu   Shantanu   ACTIVE  2011-03-15 17:23:12     2011-03-15 17:23:12     SYSTEM  Al-Al

2,"AX","Mariehamn","Mariehamn","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ma"
4,"DZ","Adrar","Adrar","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ad"
6,"DZ","Ain Defla","Ain Defla","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
8,"DZ","Ain Temchent","Ain Temchent","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
10,"DZ","Shantanu","Shantanu","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Al"

我需要反转第三列中的文本,如下所示,同时保持其他列不变。

nmaheiraM
rardA
alfeD niA
tnehcmeT niA
unatnahS

可以使用上述(TAB或CSV)格式中的任何一种。

4 个答案:

答案 0 :(得分:2)

请参阅Text::CSV - 逗号分隔值操纵器,

use strict;
use warnings;
use Text::CSV;
use Data::Dumper;
my @mydata;
my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                 or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
my $data = reverse($row->[2]);
 push(@mydata, $data);
 }
 print Dumper(\@mydata);
 $csv->eof or $csv->error_diag();
 close $fh;

<强>输出:

$VAR1 = [
          'nmaheiraM',
          'rardA',
          'alfeD niA',
          'tnehcmeT niA',
          'unatnahS'
        ];

答案 1 :(得分:1)

我可以建议


perl -pe "@_=split ',',$_;$_[2]=scalar reverse $_[2];$_=join ',',@_;" <1.csv >2.csv

它可能与awk有关,但我从未使用它。

更新:抱歉,它在cmd中工作,我没有检查bash。 对于bash,它应该是这样的:

perl -aF/,/ -pe "\$F[2]=scalar reverse \$F[2];\$_=join ',',@F;" <1.csv

答案 2 :(得分:1)

你可以使用awk。对于CSV,

$ awk -F"," '{for(i=length($3);i>=1;i--){s=s substr($3,i,1)};$3=s;s=""}1' OFS="," file
2,"AX","nmaheiraM","Mariehamn","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ma"
4,"DZ","rardA","Adrar","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ad"
6,"DZ","alfeD niA","Ain Defla","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
8,"DZ","tnehcmeT niA","Ain Temchent","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
10,"DZ","unatnahS","Shantanu","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Al"

或者如果你有Ruby(1.9 +)

$ ruby -i.bak -F"," -nae '$F[2].reverse! if $F[2];print $F.join(",")' file

更新:如果要保存文件,请在awk中重定向到临时文件

例如

awk -F"," '{for(i=length($3);i>=1;i--){s=s substr($3,i,1)};$3=s;s=""}1' OFS="," file > temp && mv temp file

顺便说一下,在sed中,它的-i用于保存文件“inplace”

答案 3 :(得分:0)

要使用逗号或制表符分隔符处理文件,您可以尝试:

#!/usr/bin/perl
use Modern::Perl;
use Data::Dumper;

my @rows;
while(<DATA>) {
    chomp;
    my ($delim) = $_ =~ /(,|\t)/;
    my @fields = split/,|\t/;
    $fields[2] = reverse $fields[2];
    push @rows, join $delim, @fields;
}
say Dumper \@rows;


__DATA__
2   AX  Mariehamn   Mariehamn   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ma
4   DZ  Adrar   Adrar   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ad
6   DZ  Ain Defla   Ain Defla   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ai
8   DZ  Ain Temchent    Ain Temchen ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ai
10  DZ  Shantanu    Shantanu    ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Al

2,"AX","Mariehamn","Mariehamn","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ma"
4,"DZ","Adrar","Adrar","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ad"
6,"DZ","Ain Defla","Ain Defla","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
8,"DZ","Ain Temchent","Ain Temchent","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"
10,"DZ","Shantanu","Shantanu","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Al"

<强>输出:

$VAR1 = [
          '2    AX  nmaheiraM   Mariehamn   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ma',
          '4    DZ  rardA   Adrar   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ad',
          '6    DZ  alfeD niA   Ain Defla   ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ai',
          '8    DZ  tnehcmeT niA    Ain Temchen ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Ai',
          '10   DZ  unatnahS    Shantanu    ACTIVE  2011-03-15 17:23:12 2011-03-15 17:23:12 SYSTEM  Al-Al',
          '',
          '2,"AX","nmaheiraM","Mariehamn","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ma"',
          '4,"DZ","rardA","Adrar","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ad"',
          '6,"DZ","alfeD niA","Ain Defla","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"',
          '8,"DZ","tnehcmeT niA","Ain Temchent","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Ai"',
          '10,"DZ","unatnahS","Shantanu","ACTIVE","2011-03-15 17:23:12","2011-03-15 17:23:12","SYSTEM","Al-Al"'
        ];