Sunday, 19 November 2017

sed command usage and examples

SED is a streamline editor screen-oriented utility. The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. The stream editor is ideally suited to performing repetitive edits that would take considerable time if done manually. Sed edits line by line; it reads the next line of the file and repeats the process until it is finished with the file.
Syntax
sed options 'command' filename

Let us use below test.txt file for our sed examples.

This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Print mth line from the file.

Syntax
sed -n np filename

Example
sed -n 2p test.txt

Output
SED is a streamline editor.

Print certain range (n1 to n2) of lines from the file.

Syntax
sed -n 'n1,n2p' filename

Example
sed -n '2,4p' test.txt

Output
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.

Print every nth line starting with the specific mth line

Syntax
sed -n 'm~np' filename

testnew.txt file
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.
This line is added to understand current example.

Example
sed -n '2~3p' testnew.txt

Output
SED is a streamline editor.  
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
This line is added to understand current example.

Print only specific lines from file.
We need to use execute option for this purpose as shown below to display 2nd and 5th line.

Syntax
sed -n -e 2p -e 5p test.txt

Output
SED is a streamline editor.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 

Print line from the file containing specific pattern.

Syntax
sed -n '/pattern/p' filename

Example
sed -n '/edit/p' test.txt

Output
SED is a streamline editor.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.

Print line from the file starting with number.
sed -n '/^[0-9]/p' filename

Print line from the file containing consecutive 4 numbers.
sed '/[0-9]\{4\}/p' file.txt

Print line from the file with specific string in uppercase.

Syntax
sed -n 's/lowercasestring/\U&p' filename

Example
sed -n 's/test/\U&p' test.txt

Output
This is a TEST file to learn sed command.
This is a TEST file to learn sed command.
This is a TEST file to learn sed command.

Use L instead of U for lowercase.

Display particular paragraph from the file

Syntax
sed -n '/startofpara/,/endofpara/p' filename

testfile.txt
This is a test file race
You are so fantastic barry.
Run barry Run.

All the great things happens from small begining.

Super dancer go to dance floor.
Have a rocking performance.

Example
sed -n '/This/,/Run/p' testfile

Output
This is a test file race
You are so fantastic barry.
Run barry Run.

Print lines from particular line number to specific string from the file

Syntax
sed -n 'num,/string/p' filename

Example
sed -n '1,/fantastic/p' testfile

Output
This is a test file race
You are so fantastic barry.

Display the file excluding nth line from the file.

Syntax
sed nd filename

Example
sed 2d test.txt

Output
This is a test file to learn sed command.
We will learn usage of sed command with various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Display the file excluding last line from the file.

Syntax
sed '$d' filename

Example
sed '$d' test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.

Display the file excluding certain range(n1 to n2) of lines.

Syntax
sed 'n1,n2d' filename

Example
sed '2,4d' test.txt

Output
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Replacing a string with another string

Syntax
sed s/stringtoreplace/newstring/ filename

Example
sed s/test/dummy/ test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Replacing multiple strings with another string in the entire file at the same time

Syntax
sed 's/stringtoreplace/newstring/;s/stringtoreplace/newstring/' file

Example
sed 's/test/dummy/;s/usage/operations/' test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn operations of sed command with various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Replacing a string with other string in the file for specific line

Syntax
sed 'n s/existingstring/newstring/' filename

Example
sed '4 s/test/dummy/' test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with  various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Replacing a string with other string in the file except specific line

Syntax
sed '!n s/existingstring/newstring/' filename

Example
sed '!4 s/test/dummy/' test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn operations of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

We can also use range instead of number if required.
sed 'n1,n2 s/existingstring/newstring/' filename

Replacing a string with another string in the file for the lines if specific string found

Syntax
sed '/specificstring s/stringtoreplace/newstring/' filename

Replacing a string with another string in the file for all the lines except specific string found.

Syntax
sed '/specificstring/!s/stringtoreplace/newstring/' filename

Example
sed '/sed s/usage/operation/' filename

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn operation of sed command with  various examples.
This is a test file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a test file to learn sed command.

Inserting BLANK lines in file

Syntax
sed G filename

Example
sed G test.txt

Output

This is a test file to learn sed command.

SED is a streamline editor.

We will learn usage of sed command with various examples.

This is a test file to learn sed command.

The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 

Sed edits line by line.

This is a test file to learn sed command.


To insert two blank lines execute following

Syntax
sed 'G:G' filename

In place editing of the file and performing sed operations on nth occurrence of matched string in the line

Syntax
sed -i 's/replacestring/newstring/n' test.txt

Below example will modify the test.txt file.

sed -i 's/test/dummy/gi' test.txt

g - global replace i.e. replace every instance of occurence of string in the line.
i - ignore case

Below example will modify the test.txt file and also keep the original test.txt file with org extension.

sed -i'.org' 's/test/dummy/gi' test.txt

Executing sed commands through script

Syntax
sed -f script.sed filename

cat script.sed
s/test/dummy

Example
sed -f script.sed test.txt

Output
This is a dummy file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.
This is a dummy file to learn sed command.
The sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. 
Sed edits line by line.
This is a dummy file to learn sed command.

Alternative for head command

Syntax
sed nq filename

Example
sed 3q test.txt

Output
This is a test file to learn sed command.
SED is a streamline editor.
We will learn usage of sed command with various examples.

Print last line of the file also Alternative for tail-1 command.

Syntax
sed -n '$p' test.txt

Output
This is a test file to learn sed command.

No comments:

Post a Comment