Main Menu

Search

LINUX: SED Command To Print Lines In A File Between Two Matching Patterns (How To Doc)

Below SED Command can be used to pring lines in a file between two matching patterns.
sed -n '/<string1>/,/<string2>/p' <filename>

To demonstrate this with example, lets assume we have testfile.out with below contents.

A789652=alpha
A789651=alpha
A28ee69=beta
A28ee6a=beta
A2991a5=alpha
A2991a6=alpha
A3285c1=alpha
A3285c2=alpha
A32860d=alpha
A32860e=alpha
A3286d5=alpha

If you want to print lines between "A28ee6a" and "A3285c1" in testfile.out file, your SED command looks as follows:
sed -n '/A28ee6a/,/A3285c1/p' testfile.out

Below is example output of above command, As you can see it printed 4 lines only.

# sed -n '/A28ee6a/,/A3285c1/p' testfile.out
A28ee6a=beta
A2991a5=alpha
A2991a6=alpha
A3285c1=alpha


Products to which Article Applies

All Linux Operating Systems

Additional Reference

https://likegeeks.com/sed-linux/



tarun boyella

 

No comments:

Post a Comment