Use of Linux Grep,sed,awk and diff

Source: Internet
Author: User
Tags egrep

1:grep// Display Lines

# grep ' main '/home/myhome/a.c//displays the A.C containing the main line

# grep-v ' main '/HOME/MYHOME/A.C//display all rows except main

# grep-n ' The ' a.c//show A.C contains the line

# grep-vn ' The ' a.c//shows no Line

# grep-in ' The ' a.c//does not consider the case

# grep-n T[ae]st a.c//[] Count only one character and search for rows containing test and tast

# grep-n ' [^g]st ' a.c//show St's line but the St is not near the G

# grep-n [^a-g]st a.c//st front cannot have lowercase

# grep-n ' ^the ' A.C//Find only the first character of the line

# grep-n ' ^[a-z] ' a.c//starts with a lowercase letter line

# grep-n ' ^[^a-za-z] ' A.C//is not the beginning of the English alphabet line, ^ in the [] table counter-selected in the outer position at the beginning of the

# grep-n ' \.$ ' A.C//. For the vulgar meaning, use the escape character \ To find the last line that is. No $ will look for a bit of line

# grep-n ' ^$ ' A.C//blank line, only first line and last line

# grep-n ' t. T ' A.C//.. Table any character, such as test,tast, etc.

# grep-n ' 0* ' a.c//0 or spaces, so all the data will be displayed

# grep-n ' 00* ' A.C//so contains 0 lines

# grep-n ' 000* ' a.c mean at least two lines of 0

# grep-n ' t*t ' a.c//so contains t lines, t* denotes spaces

# grep-n ' t.*t ' a.c//must contain two T

# grep-n ' o\{2\} ' a.c//{} range character, special what Suzis asking character with escape character \ Remove particularity, at least two O

# grep-n ' ^# ' A.C//search for lines beginning with #

# grep-n ' d$ ' A.C//tail behavior D

# egrep-n ' go+d ' a.c//extension, more than one O

# egrep-n ' go?d ' a.c//o? Empty or an O

# egrep-n ' Good|goood ' A.C//two or three x O

# NL My_printf.sh | Sed ' 2,5d '//nl print and delete rows 2 to 5

Use of 2:sed

# sed-n ' 3,5 ' p my_printf.sh//print lines

# sed-n ' 1,$ ' P my_printf.sh//Print all Lines

# grep-n '. * ' my_printf.sh | Sed-n ' 3 ' p//prints the third line and displays the line number

# sed-n '/tom/' P my_printf.sh//print a line containing Tom string, using//

# sed-n '/^i/' P my_printf.sh//print I lines beginning with

# sed-n '/!$/' P my_printf.sh//print! End of Line

# sed-e ' 1 ' p-e '/T/' p-n my_printf.sh//e equivalent to the conveyor, first print front and then print back

# sed ' 1 ' d my_printf.sh//delete first line

# sed '/yes/' d my_printf.sh Delete rows containing yes

# sed ' 2s/name/myname/g ' my_printf.sh//replace the second row of Name for Myname,g is global, no only replaces the first

# sed ' s/s/myname/g ' my_printf.sh//No line number default replaces all lines

# sed ' 1,4s/s/myname/g ' my_printf.sh//replace 1~4 line

# sed ' s/[0-9]//g ' my_printf.sh//Delete all the numbers

# sed ' s/[a-z]/\u&/g ' my_printf.sh//lowercase letters all replaced with uppercase letters

# sed ' s/[a-z]/\l&/g ' my_printf.sh//uppercase replaced by lowercase

# sed-n ' 3s/[a-z]/\l&/g ' p my_printf.sh//uppercase replace lowercase and print third line

# sed-r ' s/(AM) (. *) (old)/\3\2\1/' my_printf.sh//Exchange location of AM and old,-r table no justification \, () Table fragment

-N: Only available after SED processing

S: Search or replace

P: Print

-e: SED operation directly on the command line

-R: Extending regular Expressions

A: Increase

C: Replace

D: Delete

Use of 3:awk

$: ~# awk ' {print $} ' demo.txt//Print all

$NF: # awk ' {print $NF} ' demo.txt//print last field per line

$: ~# awk ' {print '} ' demo.txt//Print first field

$: ~# awk ' {print $} ' demo.txt//Print Second field

: ~# awk ' {print $ (NF-1), ' OK '} ' demo.txt//print the penultimate line and output OK

: ~# awk ' {print nr,$0} ' demo.txt//Print line number

ENVIRON: ~# awk ' {print environ[' USER '];} ' demo.txt//print path

BEGIN:: ~# awk ' begin{print environ["PATH"];} '//data to be entered before execution of the line

# last-n 5 | awk ' {print '} '///print the first five user information fields

# CAT/ETC/PASSWD | Awk-f ': ' {print '} '//-f the domain delimiter:

# CAT/ETC/PASSWD | Awk-f ': ' {print $ \ t ' $7} '//\t table interval, the account and shell are separate from tab

# awk-f: '/root/'/etc/passwd//Match Pettem (Root) to execute action (output line content is not specified)

# awk-f: '/root/{print $7} '/etc/passwd//contains the root row and prints the corresponding Shel

# awk-f ': ' {print ' file name: ' filename ', line number: ' NR ', line number: ' NF ', Content: ' $/etc/passwd '

# awk-f ': ' {printf ("File name:%10s, line number:%s, line number:%s, Content:%s\n", filename,nr,nf,$0)} '

/etc/passwd

Comparison of 4:diff differences

The function of the diff command in Linux is to compare two text files on a line-by-row basis, listing their differences. It checks the given file for a system and displays all the different rows in the two files without requiring the file to be sorted beforehand. Compare what makes old and new files do!

# Cat F1.txt

Hello

World

# Cat F2.txt

Hello

World_my

Good

# diff F1.txt F2.txt

2c2,3

< world//old files should be reduced in content

---//delimiter

> world_my//What should be added to old files

> Good

;> to add a row of old files just like new files

F1.txt old file content, f2.txt new file content

, "<" means that the following file is 1 lines less than the previous file, corresponding to the old file
">" means that the following file is 1 lines more than the previous file, corresponding to the new file

# diff-u F1.txt F2.txt//Combined comparison

---f1.txt2015-11-26 22:27:06.589702164 +0800

+ + + f2.txt2015-11-26 22:25:47.356186772 +0800

@@ -1,2 +1,3 @@

Hello

-world

+world_my

+good

Generate Patch file Patches

# diff-u F1.txt f2.txt >f1-f2.patch

# Cat F1-f2.patch

---f1.txt2015-11-26 22:27:06.589702164 +0800

+ + + f2.txt2015-11-26 22:25:47.356186772 +0800

@@ -1,2 +1,3 @@

Hello

-world

+world_my

+good

Catalog comparison

Test1 a.txt More Cc;test1 no f1.txt,f2 have f1.txt

# DIFF-UNR Test1 Test2

Diff-unr Test1/a.txt Test2/a.txt

---test1/a.txt2015-11-26 22:52:47.243168226 +0800

+ + + test2/a.txt2015-11-26 22:48:14.669955070 +0800

@@ -1,3 +1,2 @@

Aa

Bb

-cc

Diff-unr Test1/f1.txt Test2/f1.txt

---test1/f1.txt1970-01-01 08:00:00.000000000 +0800

+ + + test2/f1.txt2015-11-26 22:50:13.508227936 +0800

@@ -0,0 +1,2 @@

+hello

+world

Hit patch

# patch F1.txt <f1-f2.patch//Current directory

Undo Patch: # patch-r F1.txt <f1-f2.patch

Directory patching: Put the generated patch files in the old file directory to be patched,

Then: # PATCH-P1 < Test1-to-test2.patch

;-p1 means to cancel the first level directory

Use of Linux Grep,sed,awk and diff

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.