Linux-35-liunx Basic Important Command (L005-17)

Source: Internet
Author: User

Detailed awk commands ( example one):

1. Command 1:awk ' {if (nr>19&&nr<31) print $} ' ett.txt

where NR is the line number, and $ A indicates all the line contents (if only the first line is output, the second line is $ ...). All lines are $), so the command is to filter out all the lines with the Ett.txt file number greater than 19 and the line number less than 31, and then output the entire line,




2. Command 2:awk ' {if (nr==19) print $} ' ett.txt

Print out the contents of line 19th in the Ett.txt file,




" sed and the awk Summary "

sed good at printing output lines

awk good at printing output columns

For example , replace all strings that contain haha in the Linux system root directory and all of the files named Ett.txt in their subdirectories with Xixi.

Answer:

(1) Append the string haha to the ett.txt,




View the contents of the Ett.txt file, found that the original 1~100 natural number sequence is gone, now only the string haha,




Copy the Ett.txt file to the/etc directory and the/opt directory,




Then there are 3 ett.txt files in the Linux system (1 in the root directory, 1 in/etc directories, 1 in the/opt directory, altogether 3),

(2) using sed s#haha#xixi#g ett.txt command modification, where s means edit replace, g means all replace, the first two # is the content to be modified, the last two # is the result of the modification, that is, # to be modified # Modify the results #,




View Ett.txt file,




found that the above just changed the printout, and the original file content is not modified,

(3) Only need to add-I to the above command,-I means to modify the contents of the file, that is, the following command:

sed-i s#haha#xixi#g ett.txt,




View Ett.txt file,




found that the contents of the Ett.txt file have been modified,

(4) How can I modify the contents of the 3 ett.txt in the Linux system? With the help of the Find command, First we use the Find command to find all files named Ett.txt, starting with the root directory.




(5) The answer to the question is:

Find/-type f–name "Ett.txt" | Xargs Sed–i s#haha#xixi#g




Which pipeline | Before starting from the root directory to find out all the files in the current system named Ett.txt, Pipeline | Then the query to all the files in haha to Xixi,

Query the result of the modification,




The modification was successful.

Linux-35-liunx Basic Important Command (L005-17)

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.