Exploring the SED (Stream Editor ) command.
Sed is a standalone utility commonly used in Unix/Linux environment. Sed is stream editor for filtering and transforming text.
A stream is a data that travels from -:
- One process to another through a Pipe.
- One file to another as a redirect.
- One device to another.
Sed Usage:-
- Substitute some text for other text.
- Remove lines.
- Append text after given lines.
- Insert text before certain lines.
Syntax:-
sed OPTIONS... [SCRIPT] [INPUTFILE...]Important points -
* Sed by default replaces only first occurrence of the line.
Sample example for text replacement.
In above example, friends is replace by string wife. “s” in command is use for substitution. “/” this delimiter( command line version of find and replace).
SED is case sensitive by default.
In above example, sed didn’t replace the string “PROGRAMMING”.
We can make Sed to ignore the case. use the flag “i” OR “I” at the end of the command.
By default sed replaces the first occurrence of search pattern on a line.
In the above example, the word “swimming” is replace at the first occurrence of each line. in 3rd line the second part of the search string is not replaced and remains as-is.
There is another flag “g” to replace the search pattern on all line and all occurrence.
For all the example above, sed will not change the original file.
To change the original file with replacement string pattern. we can pass flag “-i” with suffix.
In this example, sed backup the original file as- test3.txt.bak. The content is replaced in test3.txt