Main Menu

Search

Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts

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/

LINUX: "sed" Command to Find and Replace All Occurrences of a string inside a Directory & its Sub directories(How To Doc) ("grep" command)

Below command can be used to find and replace all occurrences of a string in files inside a directory and sub directories.

grep -rl 'string1' /tmp | xargs sed -i 's/string1/string2/g'

Above command looks for string 1 in all files inside /tmp and replaces it with string2. You can change the directory name as needed from /tmp to any directory where you want to find and replace a string.

Products to which Article Applies


All Linux Operating Systems

Additional References


https://stackoverflow.com/questions/6758963/find-and-replace-with-sed-in-directory-and-sub-directories