Introduction to grep, sed, and awk usage in Linux

Source: Internet
Author: User

Linux File Operations Command introduction
1) grep
grep is used to find eligible records in a file
grep parameter Filter criteria file
Regular expressions can be used in filtered conditions
-C Displays the number of rows that match
-I ignores case
-n displays records that meet the requirements, including line numbers
-V displays records that do not meet the requirements
-a n displays the records that meet the requirements, and the following n rows
-B N Displays the records that meet the requirements, and the n rows above
-c n Displays records that meet the requirements, and n rows above and below

Example: Grep-n ' Perl ' yum.log-20160823

2) SED
SED is used to query qualifying files and output to a screen or write query results to a file
SED parameter filter criteria file

A) display the first few lines of records, with the letter P
N in single quotes means the first few lines
Show nth row record sed-n ' n ' p file name

Displays the M,n line record sed-n ' m,n ' p filename

Display last line record sed-n ' $ ' p file name

b) Show records that contain conditions
Sed-n '/tomcat/' P/var/log/tomcat.log

c) display of records with multiple criteria
Sed-e '/tomcat/' p-e '/2017-03-08/' p-n/var/log/tomcat.log

d) Delete the first few lines of records, using the letter D
N in single quotes means the first few lines
Delete nth row record sed-n ' n ' d file name

Delete the M,n line record sed-n ' m,n ' d file name

Delete last line record sed-n ' $ ' d file name

e) Replace character, character s for substitution, and g to replace all occurrences in the file
Sed ' 1,9s/09/nine/g '/var/log/tomcat.log

f) match the character to the position substitution, replace the word trailing characters () wrapped up, before the parentheses need to add \, shielding special characters
Sed ' s/\ (test\) \ (log\)/\2\1/g '/var/log/tomcat.log

g) Modify the file, the character s represents the replacement, and G indicates that all occurrences of the file are replaced
Sed-i ' 1,9s/09/nine/g '/var/log/tomcat.log

3) awk
Awk is used to intercept eligible records from a file
awk Parameter Filter criteria file

A) Displays the records that have been obtained to match the criteria, and the columns in the file are split to show the 3rd column record
Head-n10/var/log/tomcat.log| Awk-f ': ' {print $} '

b) display of qualifying records
awk '/log/'/var/log/tomcat.log

c) display matches multiple criteria records

Awk-f ': '/log/{print $1,$4}/2017/{print $2,$7} '/var/log/tomcat.log

d) Logical conditions can be judged,> >= < <=! =, etc., the condition "" denotes the character, otherwise expressed as a number
Awk-f ': ' $2>100 '/var/log/tomcat.log

NF: The number of columns produced after splitting with a separator
$NF: The value of the last 1 columns
NR: Number of rows

Introduction to grep, sed, and awk usage in Linux

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.