2010-03-19 114 views

回答

15

如果沒有答案又是對你有好處,我會盡力拿到賞金;-)

#!/usr/bin/perl 
# Lines beginning with a hash (#) denote optional comments, 
# except the first line, which is required, 
# see http://en.wikipedia.org/wiki/Shebang_(Unix) 

use strict; # http://perldoc.perl.org/strict.html 
use warnings; # http://perldoc.perl.org/warnings.html 

# http://perldoc.perl.org/perlsyn.html#Compound-Statements 
# http://perldoc.perl.org/functions/defined.html 
# http://perldoc.perl.org/functions/my.html 
# http://perldoc.perl.org/perldata.html 
# http://perldoc.perl.org/perlop.html#I%2fO-Operators 
while (defined(my $line = <>)) { 
    # http://perldoc.perl.org/functions/split.html 
    my @chunks = split ' ', $line; 
    # http://perldoc.perl.org/functions/print.html 
    # http://perldoc.perl.org/perlop.html#Quote-Like-Operators 
    print "$chunks[0] $chunks[2]\n"; 
} 

要運行此腳本,因爲它的名字是script.pl,調用它作爲

perl script.pl FILE 

其中FILE是要分析該文件。另見http://perldoc.perl.org/perlrun.html。祝你好運! ;-)

+0

甚至可以用'split/\ s + /'替換'split''來拆分所有空格(重複或不是) – Aif 2014-04-11 07:12:03

+0

@Aif通過''' '在Perl中有特殊的含義。 – codeholic 2014-08-12 21:38:54

+0

這是什麼意思? – Aif 2014-08-13 16:13:11

4

像Perl一行程序:

perl -ane 'print "@F[0,2]\n"' file

或者可執行腳本:

#!/usr/bin/perl 

use strict; 
use warnings; 

open my $fh, '<', 'file' or die "Can't open file: $!\n"; 

while (<$fh>) { 
    my @fields = split; 
    print "@fields[0,2]\n"; 
} 

執行該腳本是這樣的:

perl script.pl 

chmod 755 script.pl 
./script.pl 
+1

如果您使用嚴格和警告,你可能想關閉您的文件在腳本的末尾:-) – Aif 2014-04-11 07:14:12

11
 
    perl -lane 'print "@F[0,2]"' < file 
+0

在awk-killer模式下使用Perl - 使用autosplit。 – 2010-03-20 00:48:01

+0

-a隱式設置-n,所以你實際上不需要n。所以perl -lae就足夠了。但無論如何,這裏最好的答案是 – 2017-03-16 15:35:09

8
while (<>) { 
    my @fields = split; 
    print "@fields[0,2]\n"; 
} 

和公正的品種,在Windows上:

C:\Temp> perl -pale "$_=qq{@F[0,2]}" 

,並在Unix

$ perl -pale '$_="@F[0,2]"' 
1
while(<>){ 
    chomp; 
    @s = split ; 
    print "$s[0] $s[2]\n"; 
} 

請拿出去通過documentation以及

+1

http://perldoc.perl.org/functions/split.html:對'/ \ s + /'的分割就像是一個split('')',除了任何前導空格產生一個null第一場。沒有參數的分割實際上在內部分割('',$ _)'。 – 2010-03-19 17:15:22

13

對於像perl這樣強大的東西來說,這確實有點浪費,因爲你可以在awk的一個簡單的行中做同樣的事情。

awk '{ print $1 $3 }'

+5

當你知道Perl時,這是浪費時間學習'awk'。 – codeholic 2010-03-20 09:18:38

+6

@codeholic,如果OP *知道* Perl,他們不會問這個問題。使用Perl就像使用熱核彈頭試圖殺死蒼蠅一樣。 +1這個答案。 – paxdiablo 2010-03-23 05:36:28

+0

不說平臺,但比用awk更容易得到perl。此外,這可能是一個更大的編程任務的一部分......我相信awk能夠勝任,但有時候這個工作需要它。 – 2010-03-27 05:45:11

4

我敢肯定,因爲這個問題詢問在Perl給出的結果,但無論如何,我不應該得到的賞金:

在bash/KSH /灰/等:

cut -d " " -f 1,3 "file" 

在Windows/DOS:

for /f "tokens=1-4 delims= " %i in (file) do (echo %i %k) 

優點:與其他人一樣說,沒有必要學習明珠,awk中,什麼都沒有,只知道一些工具。這兩個調用的結果可以通過使用「>」和「>>」運算符保存到磁盤。

+0

+1切割。儘管cut需要一個可選的文件參數,所以我可能會繞過cat和管道。 – Hobo 2010-03-28 09:11:51

+0

@霍波:真的,我會糾正這一點。謝謝你的提示。 – 2010-03-30 14:42:06

0
#!/usr/bin/env perl 
open my$F, "<", "file" or die; 
print join(" ",(split)[0,2])."\n" while(<$F>); 
close $F 
0

一個簡單的方法是:

(split)[0,2] 

例子:

$_ = 'abc def ghi gkl'; 
print((split)[0,2] , "\n"); 
print(join(" ", (split)[0,2] ),"\n"); 

命令行:

perl -e '$_="abc def ghi gkl";print(join(" ",(split)[0,2]),"\n")' 
+0

我對[]構造感到困惑,不得不繼續#perl並詢問。它是「切片」:https://perldoc.perl.org/perldata.html#Slices。 – Richlv 2017-08-31 19:11:02

相關問題