Common uses and tricks of the Perl command line

Source: Internet
Author: User

Common uses and tricks of the perl command line Lazy OPS Source: Lazy Ops

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 $. > 50′file

Same command Head-n 50

Do not display the beginning 10 lines:

Perl-ne ' Print unless 1.. 10′file

Display 15 rows to 17 rows:

Perl-ne ' Print if 15.. 17′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 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;}

Common uses and tricks of the Perl command line

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.