grep awk sed Exercises

Source: Internet
Author: User
Tags egrep

1. How can I print out a line with a user UID greater than 500 in/etc/passwd?
Awk-f ': ' $ > ' passwd


2. What does NR,NF two variables mean in awk? Awk-f ': ' {print $NR} '/etc/passwd will print out what results?
NR is the number of rows NF is the number of segments

Prints the 1th paragraph of the first row, the 2nd paragraph in the second row, the 7th segment of Line 7th, and the empty line


3. Use grep to filter the line containing ' abc ' or ' 123 ' in the 1.txt document and add the line number in front of the filtered line.

Egrep-n ' abc|123 ' passwd

4. Grep-v ' ^$ ' 1.txt which rows will be filtered out?
Filter out non-empty lines

5. '. ' What do you mean by ' * ' and '. * ' respectively? ' + ' and '? ' What does it mean that these five symbols can be used in grep and can be used in egrep, sed, and awk?

Represents any one character * represents 0 or more preceding characters. * Denotes 0 or more arbitrary characters

+ means filter one or more of the preceding characters 1? Represents a filter of 0 or 1 preceding characters

grep and SED can use '. ', ' * ' and '. * ', but cannot use ' + ' and '? ' Egrep and awk can all be used.


6. What is the use of a {} in grep?

Specifies the number of occurrences to filter characters

7. SED has an option to directly change the text file, which is the option?
-i


8. Sed-i ' s/.*ie//;s/[' |&].*//' file What do you mean by this command?

Delete a character that ends with IE or begins with "or | or &

9. How do I delete all the numbers or letters in a document?

Sed-i ' s/[0-9]//g ' passwd

Sed-i ' s/[a-z]//g ' passwd

10.   intercepts the first segment of log 1.log (separated by a space), sorts by numbers, and then goes heavy, but how do you want to keep the number of duplicates?

Awk-f "' {print '} ' passwd |sort-n |uniq-c |sort-n

11. Use awk to filter out the 7th paragraph in 1.log (space delimited) to ' 200 ' and the 8th paragraph to ' 11897 ' lines.

Awk-f "$7==" && $8= "11897" ' 1.log

12. Compare the similarities and differences between the two commands: Grep-v ' ^[0-9] ' 1.txt and grep ' ^[^0-9] ' 1.txt

Grep-v ' ^[0-9] ' 1.txt print lines that start with a non-digit, including blank lines

grep ' ^[^0-9] ' 1.txt prints lines not beginning with a number, excluding blank lines

What does the $ A in awk mean? Why is the result of the following two commands inconsistent? Awk-f ': ' {print $} ' 1.txt and Awk-f ': ' $7=1 {print $} ' 1.txt

$ A indicates that all lines are printed

Awk-f ': ' $7= 1 {print $} ' 1.txt changes the 7th paragraph to 1 and prints all lines, at which time it is not output with separators


14. When using grep to filter a keyword, how do you print the line containing the keyword along with the row above, and the next line? At the same time, the above and below are printed out?

Grep-b1 '/root ' passwd above line

Grep-a1 '/root ' passwd line below

Grep-c1 '/root ' passwd up and down each line

grep awk sed Exercises

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.