2011-03-17 60 views
0

我已經將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)格式。

回答

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" 

,或者如果你有紅寶石(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來保存文件 「就地」

+0

謝謝。在sed中#我可以使用-e替換文件。 awk中可能有類似的東西嗎?或者我必須保存> to.file,然後將文件移回? – shantanuo 2011-03-17 09:08:52

+0

'perl -i.bak -F,-lape'$ F [2] = reverse $ F [2]; $ _ =加入「,」,@F'文件' – ikegami 2011-03-17 13:53:48

+0

@ikegami,請將您的Perl解決方案作爲您自己的答案。 – kurumi 2011-03-17 14:54:02

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 
+0

#語法錯誤##執行-e因編譯錯誤而中止。 – shantanuo 2011-03-17 08:11:15

+0

使用perl的'-a'和'-F'選項爲你分割(數組'@ F') - http://perldoc.perl.org/perlrun.html#Command-Switches – 2011-03-17 10:43:45

+1

沒有必要用<重定向輸入。只需將文件名作爲參數傳遞給perl即可。一般來說,這是一個更好的做法,因爲它可以讓perl訪問文件名。 – 2011-03-17 13:46:18

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

輸出:在-e 1號線,靠近 「TXT [」

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