sed command with examples | Linux in Telugu

It used to replacing,searching,delete,insert data
& also is mostly used command in scripting

why late lets know about sed command.






systax 
 sed OPTIONS... [SCRIPT] [INPUTFILE...] 

Example:1

How to replace word with another word name.

cat nagios.txt | sed 's/Nagios/nagios/'

here,we replaced Nagios with nagios 

Example:2
How to replace word with another word name using global variable.

cat nagios.txt | sed 's/Nagios/nagios/g'

here,we replaced Nagios with nagios all places
Example:3
How to replace word with another word name nth line.

cat nagios.txt | sed 's/Nagios/nagios/'2g'

here,we replaced Nagios with nagios up to second line
Example:4
How to replace word with another word name in specific line.

cat nagios.txt | sed '2 s/Nagios/nagios/'

here,we replaced Nagios with nagios in second line only
----------------------------------------

How to delete lines from a particular file.

1.To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '2d' filename.txt

here,we are deleting 2nd line.

2. To Delete a last line

Syntax:
$ sed '$d' filename.txt

here,we are deleting last line.
3. To Delete line from range x to y

Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt

here, we are deleting between 3rd and 6th line. 

4. To Delete from nth to last line

Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt

5. To Delete pattern matching line

Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt

here,we are deleting abc word.
---------------------------------------

How to add pdf at the end of the file

cat numbers.txt | xargs -r |sed 's/ /.pdf /g'

Comments

Popular posts from this blog

Awk command with simple examples

Learn Linux in Telugu | Linux complete Free Course in Telugu by 7Hills

rsync Command Examples | rsync Command In Telugu