Main Menu

Search

Showing posts with label Linker Article Linux sed. Show all posts
Showing posts with label Linker Article Linux sed. Show all posts

LINUX - FInd Command to FInd and Replace Particular String in All files Inside A Directory (SED Command) (How To Doc)

Below is find command to find and replace particular string in all files in current directory.

In this command example we are replacing host1 inside all files which has it in current directory with host2 word.


find ./ -type f -exec sed -i 's/host1/host2/g' {} \;



Products to which Article Applies
All Linux Operating Systems

Article Author: Tarun Boyella

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