Monday, August 5, 2013

Add blank lines using sed command.

Add a double blank lines after each line

sed G test.txt

Linux  is a Unix-like computer operating system.

Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                

Learning Linux is very easy.

Add triple blank lines after each lines.

[root@machine2 ~]# sed 'G;G' test.txt
Linux  is a Unix-like computer operating system.


Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.


Learning Linux is very easy.

[root@machine2 ~]# sed  -i 'G;G' test.txt

[root@machine2 ~]# cat test.txt
Linux  is a Unix-like computer operating system.


Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.


Learning Linux is very easy.

Undo double space.

[root@machine2 ~]# sed 'n;d' test.txt
Linux  is a Unix-like computer operating system.


Learning Linux is very easy.

Add a single blank line above the matching pattern.

[root@machine2 ~]# sed '/Redhat/{x;p;x}' test.txt
Linux  is a Unix-like computer operating system.

Learning Linux is very easy.

Redhat Enterprise linux

[root@machine2 ~]# cat test.txt
Linux  is a Unix-like computer operating system.

Learning Linux is very easy.
Redhat Enterprise linux

Add a blank line below the matching pattern.


[root@machine2 ~]# sed '/Redhat/G' test.txt
Linux  is a Unix-like computer operating system.

Learning Linux is very easy.
Redhat Enterprise linux

Add a blank line above and below the matching pattern.

[root@machine2 ~]# sed '/Redhat/{x;p;x;G}' test.txt
Linux  is a Unix-like computer operating system.

Learning Linux is very easy.

Redhat Enterprise linux

Number the lines in linux

[root@machine2 ~]# sed = test.txt | sed 'N;s/\n/\t/'
1       Linux  is a Unix-like computer operating system.
2
3       Learning Linux is very easy.
4       Redhat Enterprise linux

 [root@machine2 ~]# sed = test.txt
1
Linux  is a Unix-like computer operating system.
2

3
Learning Linux is very easy.
4
Redhat Enterprise linux

N join the lines.

The 's/\n/\t/' replaces the newline chars with tabs,

1       Linux  is a Unix-like computer operating system.
2
3       Learning Linux is very easy.
4       Redhat Enterprise linux

Cound the number of lines in a file

[root@machine2 ~]# sed -n '$=' test.txt
4

Print number of lines in a file.

[root@machine2 ~]# sed 2q test.txt
Linux  is a Unix-like computer operating system.

Prints specific line number in a file

No comments: