Обратный текст третьего столбца - PullRequest
0 голосов
/ 17 марта 2011

Я экспортировал данные SQL в текст в формате с разделением табуляцией или через запятую.

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 ]

2 голосов
/ 17 марта 2011

см. Текст :: 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 голос
/ 17 марта 2011

вы можете использовать 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 для сохранения файла «на месте»

1 голос
/ 17 марта 2011

Я могу предложить


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
0 голосов
/ 17 марта 2011

Чтобы обработать файл с разделителем запятыми или табуляцией, вы можете попробовать:

#!/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"'
        ];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...