Linux Three Musketeers awk, SED, grep

Source: Internet
Author: User

Linux Three Musketeers awk, SED, grep

1, awk is mainly used to do the breakdown, matching, calculation work
2, SED is mainly used to do the replacement (stream processing)
3, grep is mainly used to filter out the required text

awk (the default delimiter is a space) awk-f ":" '/bonzi/{if (nr==2) print $} ' (General mode)

-F Set delimiter, you can set multiple delimiters-F "[:!] +"

NR denotes line number

$NF indicates a trailing line

NF number of fields in the current record


Built-in functions

Gsub (R,s)

Use s instead of R for the entire $

Gsub (r,s,t)

Replace R with S in the whole t

Index (S,T)

Returns the first position of the string T in S

Length (s)

return s length

Match (S,R)

Tests if s contains a string matching R

Split (S,A,FS)

Dividing s into sequence a on FS

Sprint (FMT,EXP)

Returns the EXP formatted by FMT

Sub (r,s)

Replace s with the longest substring in the leftmost string

SUBSTR (S,P)

Returns the suffix part of the string s starting from P

SUBSTR (S,p,n)

Returns the suffix part of the string s from p starting at length n


Sed (stream edit) sed-n ' 2s# Original # change #g ' file

-N cancels the default output

-I modify Content

-E allows multiple edits

-R Extended Regular

Sed-n '/bonzi/s# Original # change #gp ' file

Sed-i ' 2,5s# Original # change #g ' file

Examples of latter references:

[Email protected] test]# ifconfig eth1|sed-nr ' 2s#.*addr: (. *) BC.*$#\1#GP '
192.168.1.8



grep (filter text)

-V Exclusion

-I is case insensitive

-o outputs only matching content

-A print matches to the following line,-a 3 prints after three lines

-B Prints the preceding line that matches to,-B 4 prints the first 4 lines

-C print the line that matches to the context, and-C 5 prints the top and bottom 5 lines



Linux Three Musketeers awk, SED, grep

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.