开发者

Reverse text of third column

开发者 https://www.devze.com 2023-02-17 14:02 出处:网络
I have exported the SQL data to a text in Tab 开发者_开发问答separted or comma separated format.

I have exported the SQL data to a text in Tab 开发者_开发问答separted or comma separated format.

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"

I need to reverse the text in third column as shown below, while keeping the other columns untouched.

nmaheiraM
rardA
alfeD niA
tnehcmeT niA
unatnahS

Any one of the above (TAB or CSV) formats can be used.


see Text::CSV - comma-separated values manipulator like,

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;

Output:

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


you can use awk. For 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"

or if you have Ruby(1.9+)

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

Update: If you want to save the file, in awk just redirect to a temp file

eg

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

And by the way , in sed, its -i to save the file "inplace"


I can suggest


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

Its probably also possible to do with awk, but I've never used it.

Update: sorry, it worked in cmd, and I didn't check bash. For bash it should look like this:

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


To process file with either commas or tabulations delimiter, you can try:

#!/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"

output:

$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"'
        ];
0

精彩评论

暂无评论...
验证码 换一张
取 消