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 to make it simple for below command for numbering.

Example18: I want to give numbering to all the lines after search and replace. Line numbers are given by using = option

	sed = tem.txt

Output

1

surendra audi

2

kumar nudi

3

mouni surendra

4

baby dudy

Example19: But is this right way to do the things? If I can display output as below that would be great.

Output

1 surendra audi

2 kumar nudi

3 mouni surendra

4 baby dudy

To get above output we have to use N option to combine two lines

	sed = tem.txt | sed ‘N;s/n/t/’

Search operator (/searcterm/)

Example20: sed can work for you on particular line where you given one word which is matching in that line. For example in fle tem.txt where ever it finds Surendra, replace audi with xyz. The code for that one is below

	 sed ‘/surendra/ s/audi/xyz/’ tem.txt

Example21: How about a range of lines, search for audi from first line to the search term Surendra and replace audi with xyz

	sed ‘1,/Surendra/ s/audi/xyz/’ tem.txt

Example22: How about search from 3rd line to till it find Surendra?

	sed ‘3,/Surendra/ s/audi/xyz/’ tem.txt

Example23: How about search and replace audi with xyz between two search terms Surendra and mouni?

	sed ‘/Surendra/,/mouni/ s/audi/xyz/’ tem.txt

Example24: How about searching from a search pattern to end of the line?

	sed ‘/Surendra/,$ s/audi/xyz/’ tem.txt

Negation operation(!)

 This operator will work with w, p and d options. We will see some examples below. This operator is bit confusing and require more practice to get more out of this.

	This option is useful for negating the search pattern.
Example25: I want to display all the lines which do not contain abc word
	
sed –n ‘/abc/ !p’ tem.txt
	
Example26: I want to delete all lines expect the lines which contain surendra.
	
sed '/surendra/ !d' tem.txt
	
You can combine all the above mention techniques with this negation option.
For example
Example27: What to delete all the lines expect 1 to 3


sed '1,3 !d'

 SED scripting operator (-f switch)

This option is useful for writing complex SED script, as you people are aware of SED is one more scripting language such as Shell scripting, AWK and Perl etc.

	sed -f sedscript.sed
	Where sedscript.sed will contain SED script as we do for Bash scripting.

We are collecting SED onlines which are in net and going to explain them which are not explained in details for our readers.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.