sed is stream editor for filtering and transforming text.
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
sed is very useful when you are making same changes on multiple files or multiples times in same file. Imagine using regular text editor for such scenario. you will feel rest-less when you do such changes manually. once you start using sed, those changes will be done easily.. quickly..How to find version of sed on your system ?
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
--- Read this man page for more details.
Example 1 :
$sed 's/wrong pattern/correct pattern/' original.file
substitutes in EVERY line of a file called wrong.file the first and only the first occurrence of “wrong pattern” by “good pattern” and displays the corrected text at standard output. Check the output until everything is ok, then redirect the output (you can not overwrite the old file)
$sed 's/wrong pattern/correct pattern/' original.file > updated.file
Another way to redirect the output:
$cat wrong_file.txt | sed 's/wrong pattern/good pattern/g' > correct_file.txt
If you want to replace the “wrong pattern” globally (whole file), e.g. several times a line, then enter
$sed 's/wrong pattern/good pattern/g' wrong.file > correct.file
To replace the “wrong pattern” only once at the very first occurrence we need to adress the substitution. Either we know at which line the “wrong pattern” is located, i.e. line 16, then type
$sed '16s/wrong pattern/good pattern/' original.file > correct_new.file
If we don’t know the position of the “wrong pattern”, we need to give an address range where sed should do the substitution, i.e.
$sed '1,16s/wrong pattern/correct pattern/' original.file > correct_new.file
but in an general case we won’t know if the “wrong pattern” occurs exactly once in the first 20 lines… But we can define the range with the pattern itself, so we expect
$sed '1,/wrong pattern/s/wrong pattern/correct pattern/' correct_new.file
to do the job: start at line 1 and substitute until the first occurrence is found. Unfortunately this doesn’t work IF the “wrong pattern” appears in the first AND second line! Because the 1 activates the first line the second address of the range is at least the second line. In this case the “wrong pattern” will be substituted in both lines. What we need is
$sed '0,/wrong pattern/s/wrong pattern/good pattern/' wrong.file
to prevent the activation of the second line where the “wrong pattern”
could appear again.
How to perform different append operations using sed?
Append content of file year.txt at line 3 of data.txt.
$sed '3 r file1.txt' file2.txt > file1_2.txt
Append after each line with the string “pattern” a line with text “new”.
$sed '/pattern/anew' original.file
Append after each line a new line with text “new”.
$sed 'a/new' original.file
Why “sed” is system administrators friend ?
How do is edit multiple files in 1 go ?
Make your edit/update/delete/replace easier with sed?
Sed “The King” of stream / patter edditing / formating and transforming