Perl Command Line remarks

Source: Internet
Author: User
Tags add numbers


Common Perl Command Line options

Common unix Commands: perl implements sed awk tr nl


Perl, sed, awk, tr, grep, nl, and other common commands

Task Sed Perl
Replace12WithTwelve sed 's/12/twelve/g' perl -pe 's/12/twelve/g'
Replace the wordShWithBourne Shell sed -e 's/ sh / Bourne Shell /g'[1] perl -pe 's//bsh/b/Bourne Shell/g'[2]
Remove lines 2 to 4 from stream sed '2,4d' perl -nle 'print if $.<2 || $.>4'

Task Awk Perl
Print second field (whitespace-separated) awk '{print $2}' perl -lane 'print $F[1]'
Count lines starting with X awk '/^X/ {++x} END {print x}' perl -nle '++$x if /^X/; print $x if eof'
Add numbers in second column and print sum awk '{sum+=$2} END {print sum}' perl -lane '$sum+=$F[1]; print $sum if eof'

Task Tr Perl
ROT13 tr 'A-Za-z' 'N-ZA-Mn-za-m' perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'
Remove carriage return from DOS files[3] tr -d '/r' perl -pe 'tr//r//d'

Task Grep Perl
Print only lines containing12 grep '12' perl -nle 'print if /12/'
Print only lines not containing12 grep -v '12' perl -nle 'print if !/12/'

Task Nl Perl
Insert line numbers (lined up) nl -ba perl -nle 'printf "%6s  %s/n", $., $_'


Perl Command Line Perl parameter: allows short Perl programs to run on the Perl Command Line.

Allows the Perl program to run on the Perl Command Line.
For example, we can run the "HelloWorld" program on the Perl Command Line without writing it into a file and then running it.
$ Perl-e 'print "HelloWorld \ n "'

Multiple-e can also be used at the same time. The running sequence depends on the position where it appears.
$ Perl-e 'print "Hello"; '-e' print "World \ n "'
Like all Perl programs, only the last line of the program does not need to end.

A model can be referenced as usual.
$ Perl-MLWP: Simple-e 'getstore ("", "163.html" '# download the entire webpage
-The M + Module name is the same as the use module name.

Part 3: Implicit Loop
Added the loop function, allowing you to process files in one row
Using perl-n-e'print0000'1.txt000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
This is the same as the following program.

  1. LINE:
  2. While (<> ){
  3. Print;
  4. }


<> Open a file in the Perl Command Line and read one row. Each row is saved in $ _ by default _
$ Perl-n-e 'print "$.-$ _" 'file

The above line can be written

  1. LINE:
  2. While (<> ;){
  3. Print "$.-$ _"
  4. }


Output current row $. and current row $ _.

4:-p, the same as-n, but the $ _ content will be printed.

If you want to do some processing before and after the loop, you can use BEGIN or ENDblock. the following line calculates the number of words in the file.

$ Perl-ne 'end {print $ t} @ w =/(\ w +)/gw.{t}@w'file.txt

Put all matched words in each row into the array @ w, and then add the number of @ w elements to the total number of words in the print output file in $ t. ENDblock.
There are two other parameters to make the program simpler.

Enable the automatic separation (split) mode. The space is the default separation number. The input is separated based on the separation number and then placed in the default array @ F

Use-a. The preceding command can be written as follows:
Using perl-ane'endpoint print0000x00000000x00000000@f'file.txt # use-

Change the default separator to the one you want. For example, set the separator as a non-character. The preceding command can be changed:
$ Perl-F' \ w'-ane'endpoint ?print=x={x}}@f'file.txt

The following uses the Unixpassword file to introduce a complex example. Unixpassword is a text file, and each row is a user record,
Separated by the colon (:). Row 7th is the user's logon shell path. We can determine the number of users using each different shell path:

$ Perl-F': '-ane' $ s {$ F [6] }++; '\>; -e 'end {print "$ _ = $ s {$ _}" for keys % s} '/etc/passwd

Although it is not a row, you can see what problems can be solved by using parameters.

Part 4: RecordSeparators Data Separator

$/And $ \ -- Input and Output separator.
$/Is used to separate the data read from the file handle. The default $/separator is \ n, so that each row is read from the file handle.
$ \ The default value is null, which is used to automatically add to the end of the data to be printed. This is why \ n is always added to the end of print.
$/And $ \ can be used with-n-p. The Perl Command Line should be-0 (0) and-l (this is L ).
-0 can be followed by a hexadecimal or octal value, which is used to pay $ /.
-00 open paragraph mode,-0777 open slurp mode (that is, you can read the entire file at a time), this is the same effect as setting $/to null characters and undef.

Using-l independently has two effects:
1. Automatic chomp input Separator
Second, pay the $/value to $ \ (in this case, \ n is automatically added at the end of the print)

1:-l parameter, used to add \ n to each output. For example:
$ Perl-le 'print "HelloWorld "'

Part 5: In-situ editing

Using existing parameters, we can write a very effective Perl Command Line program. Common UnixI/O redirection:
$ Perl-pe 'somecode'> output.txt

This program reads data from input.txtand then processes the data and outputs it to output.txt. Of course, you can also redirect the data to the same file.

The above program can be simpler by using the-I parameter.

Rename the source file and read it from the renamed source file. Finally, write the processed data to the source file.
If-I is followed by another string, the string is merged with the source file name and a new file name is generated.
This file will be used to store the original file to avoid being overwritten by the-I parameter.

In this example, replace all php characters with perl:
$ Perl-I-pe's/\ bPHP \ B/Perl/g'file.txt
The program reads each row of the file, replaces the characters, and re-writes (overwrites) the processed data to the source file.

If you do not want to overwrite the source file, you can use
$ Perl-i.bak-pe's/\ bPHP \ B/Perl/g'file.txt

The processed data is written to file.txt, and file.txt. bak is the backup of the source file.

Perl classic example

Encountered a problem:

Similar to this output, I want to convert them into the following form:

Add the numbers after the same Email name. Can you give me some ideas on how to use perl to implement it.

Answer: perl-anle '$ cnt {$ F [0]} + = $ F [1]; END {print "$ _ \ t $ cnt {$ _}" forkeys % cnt} 'urfile

If you are familiar with the usage of the preceding Perl Command Line parameters, the above command should be well understood:
Each time a line of urfile is read, because-a is used, the automatic split mode is enabled. space is the default separator. the input is separated based on the separator and placed in the default array @ F,
Take the first behavior example of the file $ F [0] is the, $ F [1] is 2

$ Cnt {$ F [0]} + = $ F [1] is a hash array with $ F [0] as the key and $ F [1] As the value, overlays the values of the same key. then, process each row of the file.
END {} is processed after the loop. It means to print the % cnt hash array. The key of the hash array is the mailbox name, and the value is the number after the superposition.

The following is the text format of the preceding command:

  1. #! /Usr/bin/perl
  2. Usestrict;
  3. Usewarnings;
  4. My % hash;
  5. While (<> ){
  6. Chomp;
  7. My @ array = split;
  8. $ Hash {$ array [0]} + = $ array [1];
  9. }
  10. END {
  11. Foreach (keys % hash ){
  12. Print "$ _ \ t $ hash {$ _} \ n ";
  13. }
  14. }



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: 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.