Wednesday, February 16, 2011

perl one liner types

perl normal guy just used a line

    
[Replace]
    
All files in the foo bar c replaced.
    
bak backup with
    
\ B is word boundary
    
perl-p-i.bak-e 's / \ bfoo \ b / bar / g' *. c

    
Saves an increasing number contained in each file
    
bak backup with
    
perl-i.bak-pe 's / (\ d +) / 1 + $ 1 / ge' file1 file2 ....

    
0D0A from 0A to convert the line endings
    
perl-pe 's / \ 015 \ 012 / \ 012 / g' file

    
To convert the line endings from 0A 0D0A
    
perl-pe 's / \ 012 / \ 015 \ 012 / g' file



    
[Partial extraction]
    
Show up to the fourth and sixth from the beginning of the field separated by spaces
    
perl-lane 'print "@ F [0 .. 4] $ F [6]"' file

    
Show up to the beginning of the fourth colon separated field
    
perl-F:-lane 'print "@ F [0 .. 4] \ n"' / etc / passwd

    
START and END part between the view
    
perl-ne 'print if / ^ START $ / .. / ^ END $ /' file

    
START and END part between the outside view
    
perl-ne 'print unless / ^ START $ / .. / ^ END $ /' file

    
Displaying the first 50 rows
    
perl-pe 'exit if $.> 50' file

    
Do not show the first 10 rows
    
perl-ne 'print unless 1 .. 10' file

    
Lines 17-15 show only
    
perl-ne 'print if 15 .. 17' file

    
Discard after 80 characters of each line
    
perl-lne 'print substr ($ _, 0, 80) = ""' file

    
Discard the first 10 characters of each line
    
perl-lne 'print substr ($ _, 10) = ""' file



    
[Better than grep]
    
comment line is displayed that contains the string
    
perl-ne 'print if / comment /' duptext
    
comment line is displayed does not contain the string
    
perl-ne 'print if / comment /' duptext

    
apple or comment line is displayed that contains the string
    
perl-ne 'print if / comment / | | / apple /' duptext

    
apple or comment line is displayed that contains the string
    
perl-ne 'print if / comment / | | / apple /' duptext

    
apple display and comment lines that contain both text
    
perl-ne 'print if / comment / | | / apple /' duptext

    
comment line and displays the file name contains the string
    
find useful in conjunction with
    
perl-ne 'print "$ ARGV: $ _" if / comment /' file1 file2 file3 ...

    
int matches only (print does not agree with) grep
    
perl-ne 'print if / \ bint \ b /' test.txt

    
port9 port0 from 10 words to match up
    
perl-ne 'print if / \ bport \ d \ b /' test.txt

    
take the words that match port0 port9999 numbers, such as at the end
    
perl-ne 'print if / \ bport \ d + \ b /' test.txt



    
[Operation between the Field
    
The second sum field from the first and last field
    
perl-lane 'print $ F [0] + $ F [-2]'



    
[Sort]
    
Sort the file line by line
    
perl-e 'print sort <>' file

    
Sort paragraphs per file
    
perl -00-e 'print sort <>' file

    
Presented as a single file and then sort to compare multiple files, each file
    
Masu
    
perl -0777-e 'print sort <>' file1 file2



    
Invert
    
Turn over the given file line by line
    
perl-e 'print reverse <>' file1

    
Turn over every file in characters
    
perl-0777e 'print scalar reverse <>' f1 f2 f3 ...

    
Turn over a paragraph per file
    
perl -00-e 'print reverse <>' file1 file2 file3 ....

    
Turn over in characters in a row
    
perl-nle 'print scalar reverse $ _' file1 file2 file3 ....

    
Locate the palindrome from the dictionary. Unfortunately, the Japanese are not supported
    
perl-lne 'print if $ _ eq reverse' / usr / dict / words

    
If two or more words in succession in the file, print one of them
    
The
    
perl -0777-ne 'print "$.: doubled $ 1 \ n" while / \ b (\ w +) \ b \ s + \ b \ 1 \ b / gi'



    
Convert to Number
    
Hexadecimal display convert to a decimal number entered. CTRL-C terminated
    
perl-ne 'printf "% x \ n ",$_'

    
Displays octal numbers to decimal numbers into the input. CTRL-C terminated
    
perl-ne 'printf "% o \ n ",$_'

    
And displays the hexadecimal to decimal conversion was entered. CTRL-C terminated
    
perl-ne 'print hex ($_)." \ n "'

    
Display converted to decimal octal entered. CTRL-C terminated
    
perl-ne 'print oct ($_)." \ n "'

    
Can be calculated as a simple calculator. CTRL-C terminated
    
perl-ne 'print eval ($_)." \ n "'



    
●-M would be foul. Also useful. Module one line guy

    
View the source of the page http://www.yahoo.co.jp/
    
perl-MLWP:: UserAgent-e 'LWP:: UserAgent-> new-> request (HTTP:: Request-> new (' GET ', $ ARGV [0]), sub {print $ _ [0];}) ; 'http://www.yahoo.co.jp/

    
Find and display all the files from where the argument
    
perl-MFile:: Find-le 'find {wanted => sub {print ;}},$ ARGV [0]'

    
Pl extension from the current directory and displays all found files
    
perl-MFile:: Find-le 'find {wanted => sub {print if / \. pl $/;}},$ ARGV [0]'

    
Create a directory. mkdir can be made at once, unlike the deep layers
    
The.
    
perl-MFile:: Path-e 'mkpath ([@ ARGV], 1,0777)' test/tmp1 test2/tmp2

    
Base64-encoded
    
perl-MMIME:: Base64-ne 'print & MIME:: Base64:: encode_base64 ($ _)' file

    
Base64-encoded file to decode the
    
perl-MMIME:: Base64-ne 'print & MIME:: Base64:: decode_base64 ($ _)' file

    
Quoted-Printable encoded (see RFC 2045)
    
perl-MMIME:: QuotedPrint-ne 'print & MIME:: QuotedPrint:: encode_qp ($ _)' file

    
Fetch and display the specified page.
    
perl-MLWP:: Simple-e 'getprint "http://www.foo.bar.com/";

    
Download the file by ftp.
    
perl-MLWP:: Simple-e 'getstore "ftp://ftp.sunet.se/pub/lang/perl/CPAN/src/latest.tar.gz", "perl.tar.gz"'

    
http mirror of the document
    
perl-MLWP:: Simple-e 'mirror ("http://www.perl.com/", "perl.html");'



    
● explain the options for oneliners

    
-0 Digit (octal)
    
Record ($ /) as an octal number. The improved Deforutosepareta
    
Lines

    
-00
    
The paragraph mode. Line breaks and paragraphs are separated by a continuous
    
Chunks of text

    
Disable -0777 separator. That is the whole file in one paragraph
    
Rimasu

    
Auto-a mode split
      
Array @ F = split ('');
      
The delimiter can be specified with-F
      
The default is one character space

    
-E
    
Keywords for a guy line
    
The given string as a command line, run perl program
    
Multiple-e also need to use a semicolon

    
-I extension
    
. Bak to specify, that, to back up files to override the individual
    
Masu.

    
-L number (octal)
    
Auto chomp
    
When the output number (octal) to replace the carriage returns.


    
-N
    
Automatic loop
      
while (<>) {
              
... # This script is executed
      
}


    
-P
    
Print with automatic loop
      
while (<>) {
              
... # This script is executed

      
}

    
Combination

    
-Na-F regexp
      
while (<>) {
            
@ F = split (/ regex /);
              
... # This script is executed
      
}

    
-Pa-F regexp
      
while (<>) {
            
@ F = split (/ regex /);
              
... # This script is executed
            
print;
      
}

No comments: