Search Results for: sed

SED find and replace multiple search patterns

Q. I have a word abc and cde and I want to replace them both with xyz. How can I do that in Shell scripting? We can not do this without using sed command. Below are the ways you can do that. Below example will try to replace abc or cde with xyz. I used regexp ‘|’ to accomplish this task. -r is for enabling regexp and -i for inserting the changes in to the file.sed -ri ‘s/abc|cde/xyz/g’ filename Some more examples on replacing multiple words/char. In below example we can replace either a or b with d sed -i ‘s/[ab]/d/g’ filename I want to replace swapon and swapoff words with just word “free” sed -r ‘s/swap(on|off)/free/g’ filenameIn order to understand () and | we have know about regexp please click here to know more about regexp Below is bit odd way to do the multiple word changes using sed. sed ‘s/abc/get/g;s/def/get/g’ filenameorsed -e ‘s/abc/get/g’ -e ‘s/def/get/g’...

Read More

Learn SED with examples

This is our second post on SED, which is a Stream EDitor. Please find our Previous post on SED. You can find our other posts on RegExp basics and extended. Line number operator ( , and = switch) We can search in a file according to line numbers. Example13: Suppose I want to search in only 3rd line and then replace the term in that line alone. sed ‘3 s/Surendra/bca/’ tem.txt Example14:Search from 1st line to 4th line and replace that term. sed ‘1,4 s/Surendra/bca/’ tem.txt Example15:Search for a term from 3rd line to 5th line and replace it only between these lines. sed ‘3,5 s/Surendra/bca/’ tem.txt Example16:Search for a term from 2nd line to the end of the file and replace it with a term. sed ‘2,$ s/Surendra/bca/’ tem.txt Example17:How about combining two-line in to a single line. The below script will use N option which try to merge two, two lines in to single line and it will keep newline character in the combine line. sed ‘N’ tem.txt The output will not differ to the input file. If you want to really see the difference you have to use search and replace for n to some space or tab(t) sed ‘N;s/n/ /’ tem.txt Output: surendra audi kumar nudi mouni surendra baby dudy Note:The above command is not used much of the time and I used this example...

Read More

SED(Stream EDitor ) Explained in detail for Linux/Unix

Learn SED with examples Before going to learn SED a brief intro is required. a)sed is a Stream EDitor. b)It reads line by line when a file is feed to it. c)sed will not edit the input file by default and it displays output on the screen. d)SED is mainly used for search for a term and replace with desired term in a file/stream SED can do following things. 1) Search and replace (s switch) –90 % of your SED related work is completed with this option 2) Printing (-n and p switch) 3) Editing (-I, w, d switch) 4) Multiple SED commands and continuation operation (-e and ; switch) 5) Line number ( , and = switch) 6) Search operation (/searcterm/) 7) Negation operation(!) 8) SED scripting operator (-f switch) 9) Miscellaneous SED examples Search and replace operator( s for search) Below is the syntax for search a term and replace it with another team. sed ‘s/searchterm/replaceterm/’ inputfile or cat inputfilename | sed ‘s/searchterm/replaceterm/’ or echo “This is test message” | sed ‘s/searchterm/replaceterm/’ Exameple1: Search for google in file:test.txt and replace with yahoo sed ‘s/google/yahoo/’ test.txt Example2: How about globally? By default sed will work on only first find term. Suppose I have following sentence. “sheena leads, sheila needs”. I want to replace sh with le. If I use below example it will replace only first occurrence. echo...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe

ABOUT ME..!

My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.