Unix_ Perl's single-line command stunt

Source: Internet
Author: User
Tags keyword list

Transferred from: superdebug.blog.chinaunix.net

Replace

Replace Foo in all C programs with bar and old files back to. bak

Perl-p-i.bak-e ' s/\bfoo\b/bar/g ' *.c

Very powerful features, especially refactoring in large programs. I remember it was only used in UltraEdit. If you don't want to back up, just write perl-p-i-e or simpler perl-pie, well, pie is a good word.

Add a value to each file that appears in the

Perl-i.bak-pe ' s/(\d+)/1 + $1/ge ' file1 file2 ....

Replace the line break \ r \ n with \ n

Perl-pie ' s/\r\n/\n/g ' file

With the Dos2unix command.

Replace the line break \ n \ r \ n

Perl-pie ' s/\n/\r\n/g ' file

With the Unix2dos command.

Take out part of a file

Field 0-4 and field 6 are displayed, the delimiter for the field is a space

Perl-lane ' print ' @f[0..4] $F [6] "' File

Very good and powerful, with awk ' print $ $, $ $, $ $4, $ $7 '. Parameter name Lane is also very well remembered.

If the field delimiter is not a space but a colon, use the

Perl-f:-lane ' print "@f[0..4]\n" '/etc/passwd

Show the section between Start and end

Perl-ne ' Print if/^start$/. /^end$/' File

I'm afraid this operation can only be done by sed ...

Instead, the part between start and end is not displayed

Perl-ne ' Print unless/^start$/. /^end$/' File

Display the beginning 50 lines:

Perl-pe ' exit if $. > ' File

Same command Head-n 50

Do not display the beginning 10 lines:

Perl-ne ' Print unless 1.. ' File

Display 15 rows to 17 rows:

Perl-ne ' Print if 15.. "File

Each row takes the first 80 characters:

Perl-lne ' Print substr ($_, 0, +) = "" ' File

The first 10 characters are discarded per line:

Perl-lne ' Print substr ($_, ten) = "" ' File

Search

To find the comment string:

Perl-ne ' Print if/comment/' Duptext

This is the normal grep command.

To find rows that do not contain comment strings:

Perl-ne ' Print unless/comment/' Duptext

The reverse of grep, i.e. grep-v.

To find a line that contains comment or Apple:

Perl-ne ' Print if/comment/| | /apple/' Duptext

The same function will be used to egrep, the syntax is more complex, I will not ...

Calculation

Calculate the sum of field 4 and the penultimate field:

Perl-lane ' Print $F [4] + $F [-2] '

If you use awk, you have to write awk ' {i=nf-1;print $5+ $i} '

Sort and invert

The files are sorted by row:

Perl-e ' Print sort <> ' file

Equivalent to a simple sort command.

The file is sorted by paragraph:

Perl-00-e ' Print sort <> ' file

Multiple files are sorted by file contents and returned to the merged file:

Perl-0777-e ' Print sort <> ' file1 file2

File is reversed by line:

Perl-e ' Print reverse <> ' file1

Do you have the appropriate command? Yes...... But quite biased, TAC (cat's reversal)

Numerical calculation

10-in-turn 16-system:

Perl-ne ' printf "%x\n", $_ '

10-in-Turn 8-in: Perl-ne ' printf '%o\n ', $_ '

16-in-turn 10-system:

Perl-ne ' Print hex ($_). " \ n "'

8-in-turn 10-system:

Perl-ne ' Print Oct ($_). " \ n "'

Simple calculator.

Perl-ne ' Print eval ($_). " \ n "'

Other

To start interactive Perl:

Perl-de 1

To view the contents of the Include path:

Perl-le ' Print for @INC '

Note

Perl command-line parameters related to one-liner:

-0< Digital > (in 8 notation) specifies the record delimiter ($/variable), the default is newline-00 paragraph mode, that is, the continuous change behavior delimiter-0777 disables the delimiter, the entire file as a record-a automatic separation mode, separated by a space $_ and saved to @f. Equivalent to @f = Split '. The delimiter can use the-f parameter to specify-F to specify the delimiter for-a, and you can use the regular expression-E to execute the specified script. -i< extension > Replace the file in place and back up the old file with the specified extension. Do not back up without specifying a name extension. -L automatically chomp the input content, automatically add line wrap-n auto-loop to the output, equivalent to while (<>) {script;}-P auto loop + Auto output, equivalent to while (<>) {script; print;}

1--> Show history Command usage frequency

[Email protected]:~$ History | Perl-f "\| | <\ (|;| \ ' |\\$\ ('-alne ' foreach (@F) {print $ if/\b (?! DO) [a-z]+] \b/i} ' | Sort | uniq-c | Sort-nr
169 ls
98 VIM
Python
Man

2--> adds a value to each file that appears in the

Perl-i.bak-pe ' s/(\d+)/1 + $1/ge ' file1 file2 ....

[Email protected]:~$ cat Ptt1.txt
204.108.13.15 ABC [] serverpath=/home/html/pics 62ms
214.92.113.13 xxx [code=5] serverpath=/home/html/pages 32ms

[Email protected]:~$ perl-i.bak-pe ' s/(\d+)/1 + $1/ge ' Ptt1.txt

Match the characters after Servrerpath and print them out

[Email protected]:~$ perl-ne ' print ' $1\n ' if/serverpath= (\s+)/g ' ptt1.txt
/home/html/pics
/home/html/pages

3--> View the contents of the containing path:

Perl-le ' Print for @INC '

4--> Remove part of a file

Field 0-4 and field 6 are displayed, the delimiter for the field is a space

perl-lane ' print ' @f[0..4] $F [6] "' File

Very good and powerful, with awk ' print $ $, $ $, $ $4, $ $7′. Parameter name Lane is also very well remembered.

If the field delimiter is not a space but a colon, use the

perl-f:-lane ' print "@f[0..4]\n" '/etc/passwd

Show the section between Start and end

perl-ne ' Print if/^start$/./^end$/' File

I'm afraid this operation can only be done by sed ...

Instead, the part between start and end is not displayed

perl-ne ' Print unless/^start$/./^end$/' File

Display the beginning 50 lines:

Perl-pe ' exit if $. > ' File

Same command Head-n 50

Do not display the beginning 10 lines:

perl-ne ' Print unless 1 ' file

Display 15 rows to 17 rows:

perl-ne ' Print if: ' File

Each row takes the first 80 characters:

perl-lne ' Print substr ($_, 0, +) = "" ' File

The first 10 characters are discarded per line:

perl-lne ' Print substr ($_, ten) = "" ' File

5--> UrlEncode and UrlDecode are often required to prepare keyword tests

Here are 2 common single-line Perl scripts (regular Expressions): input as a log or keyword list

UrlEncode: Yes \ n not transcoding

Perl-p-E ' s/([^\w\-\.\@])/$1 eq "\ n"? "\ n": sprintf ("%%%2.2x", Ord ($))/eg ' Keywords.list

UrlDecode:

Perl-p-E ' s/% (..) /pack ("C", Hex ($))/eg ' Query.log

Note

Perl command-line arguments related to one-liner

-0< Digital >
(in 8 notation) specifies the record delimiter ($/variable), the default is line wrapping
-00
Paragraph mode, which is a continuous-change-behavior delimiter
-0777
Disable delimiters, which will be the entire file as a record
-A
Auto-delimited mode, separating $_ with spaces and saving to @f. Equivalent to @f = Split ". The delimiter can be specified with the-f parameter
-F
Specify a delimiter for-a, you can use regular expressions
-E
Executes the specified script.
-i< extension >
Replace the file in place and back up the old file with the specified extension. Do not back up without specifying a name extension.
-L
Automatically chomp the input content and automatically add line breaks to the output
-N
Auto Loop, equivalent to while (<>) {script;}
-P
Auto loop + Auto output, equivalent to while (<>) {script; print;}

Unix_ Perl's single-line command stunt

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.