Perl a sentence summary of common parameters in command line programming _perl

Source: Internet
Author: User

The online environment at work has a lot of Perl command-line commands like a sentence, and today concludes some of the things that Perl command line programming.

- e The string followed by the quotation marks is the command to execute:

Copy Code code as follows:

king@king:~$ perl-e ' print ' Hello world \ \ n '
Hello World

If you can use multiple-e for more than one command, do you think of sed here? But be aware of which of the middle ";"
Copy Code code as follows:

king@king:~$ perl-e ' print ' Hello world \ n '; '-e ' print ' My I-i-perl command line script \ n '
Hello World
My i-Perl command line script

- m import modules to use:

Copy Code code as follows:

king@king:~$ perl-mlwp::simple-e ' Print head ' http://www.chinacache.com ', ' \ n '
text/html; charset=utf-81337525116978307200apache/2.2.3 (CentOS)

In addition, M can determine if a module has been installed

- m is similar to-M, except that the-M module name is equivalent to the Use module name, which introduces some of the default functions or other things, and the-M module name closes these defaults, so that you can introduce some of the functions you use only, such as the above example:

Instead of M, there will be no output;-M and-m pass = to introduce a particular function of a module.

Copy Code code as follows:

Perl-mcgi= ' header,start_html '-e ' print header, start_html '

- WEquivalent to use warnings
-n-pUse <> to run all @ARGV parameters as files on a per-line basis (with looping meaning oh, often working with other parameters, the file is traversed implicitly by the line. Each row is saved by default in $_, but-p prints the content "duplicate" And-N is more likely to print a line that satisfies a certain condition (here are some useful variables such as $.) Represents the number of rows in the current row):
Copy Code code as follows:

king@king:~$ Cat File.txt
A 1
B 2
C 3
king@king:~$ perl-p-E ' print ' file.txt
A 1
A 1
B 2
B 2
C 3
C 3
king@king:~$ perl-n-E ' print ' file.txt
A 1
B 2
C 3

See here is not the thought of SED's-n feature:
Copy Code code as follows:

king@king:~$ sed-ne ' P ' file.txt
A 1
B 2
C 3
king@king:~$ sed-e ' P ' file.txt
A 1
A 1
B 2
B 2
C 3
C 3

- I.will be modified to write directly to the file, this and sed is the same oh;
Copy Code code as follows:

king@king:~$ Cat File.txt
A 1
B 2
C 3
king@king:~$ perl-pi-e ' s/a/a/' file.txt
king@king:~$ Cat File.txt
A 1
B 2
C 3

-AOpen automatic detach (split) mode. A space is the default separator number. The input is separated according to the separation number and then placed in the default array @F.
Copy Code code as follows:

king@king:~$ perl-na-e ' Print $F [1], "\ n" ' file.txt
1
2
3

Is it like awk? There's something more like that. - F, like awk, change the delimiters between fields, by default, by Space and tab;
Copy Code code as follows:

king@king:~$ perl-f ': '-alne ' Print $F [0], "", $F [-1] if/bash$/'/etc/passwd
Root/bin/bash
King/bin/bash
Guest-b0siey/bin/bash

See the one on the top. - L's argument? Its main function is to chomp the field and add "\ n" to the end of each output, so that the output can be wrapped; Is it really useful?

Is there any other place like awk? The answer is yes,

begin{},end{} is not also very familiar with it?

Here are some common simple examples

#perl-pi-e ' s/aaa/bbb/' filename modifies files in the current file and does not generate intermediate files. Remember the-i switch because it lets you edit the file in situ.

#perl-ne ' Print if/^aaaaa/' filename filters what is needed in a file like grep. This place uses-N, so one line of action is done until the entire file is read. Also, in the pipeline,-N will be the same to traverse the contents of the pipe.

#perl-N-E ' print ' $. –$_ "' filename in this example, no-ne, just command written-n-e, in fact, this example, is to the current file in the contents of a line number printed out. Note: $. Represents the current line number

#perl-pe ' $_ = ' $. $_ "' filename this is actually the same as above, only use the-p to replace-N, this has a benefit, the same everywhere else, but-p by line to traverse the file, will be printed out to $_.

You remember the awk split domain (awk ' {i = nf–1 print $ + $i} ') Ah, isn't it convenient, let's look at Perl

Copy Code code as follows:

#perl-lane ' Print $F [0] + $F [-2] ' This magical place lies in-a, after using-a. Because-n branches are read in, then-a divides the data into @f arrays.
#perl-ne ' Print if/^start$/... /^end$/' Print the place from $start to $end
#perl-ne ' Print if $. >= 15; Exit if $. >= 17; ' Effectively print rows in a range of numbers
#perl-P-i.bak-e ' s/\bfoo\b/bar/g ' *.c the magic of modifying the-I switch in situ is that it replaces the file version produced by the file output with the script for every file in the @ARGV
#perl-ne ' Print scalar reverse $_ ' test to sort the contents of the file in reverse order, such as ABC in the file, it will become the CBA

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.