What are the common uses of the Perl command line
This article mainly introduces the common usage of Perl command line, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.
Replacement of Perl command line
◆ replaces foo in all C programs with bar, and backs up old files to .bak.
Perl-p-i.bak-e's/\ bfoo\ b/bar/g'*.c
Very powerful functions, especially in large programs to do refactoring. I remember it was only used in UltraEdit. If you don't want to back it up, just write it as perl-p-i-e or simpler perl-pie. Well, pie is a good word.
◆ adds one of the values that appear in each file
Perl-i.bak-pe's/ (\ d +) / 1+$1/ge'file1file2... .
◆ replaces the newline character\ r\ nwith\ n
Perl-pie's/\ r\ n /\ n/g'file
Same as the dos2unix command.
◆ replaces the newline character\ nwith\ r\ n
Perl-pie's/\ n /\ r\ n/g'file
Same as the unix2dos command.
Take out part of the file
Displays fields 0-4 and 6, with the delimiter of the field being a space
Perl-lane'print "@ F [0.4] $F [6]" 'file
Very good and powerful, same as awk'print$1,$2,$3,$4,$5,$7'. The parameter name lane is also easy to remember.
◆ if the field delimiter is not a space but a colon, use perl-F:-lane'print "@ F [0.4]\ n" / etc/passwd
Show the part between START and END
Perl-ne'printif/ ^ start $/.. / ^ end $/ 'file
I'm afraid only sed can do this.
In contrast, the part between START and END is not displayed
Perl-ne'printunless/ ^ start $/.. / ^ end $/ 'file
Display the first 50 lines:
Perl-pe'exitif$. > 50'file
Same command head-n50
Do not display the first 10 lines: perl-ne'printunless1..10'file
Display lines 15 to 17: perl-ne'printif15..17'file
Take the first 80 characters per line: perl-lne'printsubstr ($_, 0pj80) = "" 'file
Discard the first 10 characters per line: perl-lne'printsubstr ($_, 10) = "" 'file
Perl command line search
◆ finds comment string: perl-ne'printif/comment/'duptext
This is the normal grep command.
◆ looks for lines that do not contain comment strings: perl-ne'printunless/comment/'duptext
The reverse grep, or grep-v.
◆ looks for lines that contain comment or apple:
Perl-ne'printif/comment/ | | / apple/'duptext, egrep is about to be used for the same function.
Calculation of Perl command line
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 it as awk' {ifold NFMUI 1 locus printful5percent I}'.
Sorting and reversal of the Perl command line
Files sorted by line: perl-e'printsort'file
The equivalent of a simple sort command.
◆ files are sorted by paragraph: Perl-00-e'printsort'file
Multiple files are sorted by file contents and the merged files are returned:
Perl-0777-e'printsort'file1file2
◆ files are reversed by line: Perl-e'printreverse'file1
Are there any corresponding orders? There is. But it's quite out of line, tac (the reversal of cat)
Numerical calculation of Perl Command Line
Decimal to hexadecimal:
Perl-ne'printf "x\ n", $_'
From 10 to 8: perl-ne'printf "% o\ n", $_'
Hexadecimal to decimal:
Perl-ne'printhex ($_). "\ n"
8-to-decimal:
Perl-ne'printoct ($_). "\ n"
A simple calculator.
Perl-ne'printeval ($_). "\ n"
Other
Start the interactive perl:
Perl-de1
View the content that contains the path:
Perl-le'printfor@INC'
Remarks
Perl command line parameters related to One-Liner:
-0
(in octal) specifies the record delimiter ($/ variable), which defaults to newline
-00, paragraph mode, that is, a continuous exchange of behavior delimiters
-0777, disable the delimiter, that is, the entire file as a record
-a, automatically separate the mode, separate $_ with a space and save it to @ F. Equivalent to @ F=split. The delimiter can be specified using the-F parameter
-F, specify the delimiter of-a, you can use regular expressions
-e, execute the specified script.
-I replace the file in place and back up the old file with the specified extension. If you do not specify an extension, you will not back up.
-l, automatically chomp the input content and automatically add line breaks to the output content
-n, auto loop, equivalent to while () {script;}
-p, auto loop + automatic output, equivalent to while () {script; print;}
Thank you for reading this article carefully. I hope the article "what are the common uses of the Perl command line" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!