Monday, August 5, 2013

Advanced examples with sed command,

Sed - Stream Editor is a powerful command to modify the files in linux. Sed command is one of most useful commands used in shell scripting. Some of the sed examples



[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.                                                                        

Search and replace certain patterns in a file

Replaces first occurance of the pattern in file                                                                        

[root@machine2 ~]# sed 's/Linux/unix/1' test.txt                                                            
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.                                                                        

Replaces second occurance of pattern in the file

[root@machine2 ~]# sed 's/Linux/unix/2' test.txt                                                            
Linux  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.                                                                        

Even this syntax replaces the first occurance.

[root@machine2 ~]# sed 's/Linux/unix/' test.txt                                                            
unix  is a Unix-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.
                                           
Replaces all the matching pattern in the file.
                           
[root@machine2 ~]# sed 's/Linux/unix/g' test.txt                                                            
unix  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.

[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.                                                

Replaces the matching pattern from the second occurances to the last.

[root@machine2 ~]# sed 's/Linux/unix/2g' test.txt                                                          
Linux  is a Unix-like computer operating system. unix is assembled under the model of free and open source software development and distribution.Love to work with unix.                                                  

[root@machine2 ~]# cat > test1.txt                                                                          
http://google.com                                                                                          
http://yahoo.com                                                                                            

[root@machine2 ~]# cat test1.txt                                                                            
http://google.com                                                                                          
http://yahoo.com                                                                                            

Replacing a url. Here \ is the delimiter

[root@machine2 ~]# sed 's/http:\/\//www/' test1.txt                                                        
wwwgoogle.com                                                                                              
wwwyahoo.com                          
                                                                   

[root@machine2 ~]# sed 's/http:\/\//www./' test1.txt                                                        
www.google.com                                                                                              
www.yahoo.com                                                                                              

We can also use _ as a delimiter

[root@machine2 ~]# sed 's_http://_www._' test1.txt                                                        
www.google.com                                                                                            
www.yahoo.com                                                                                            

Here | is the delimiter.

[root@machine2 ~]# sed 's|http:|_www.|' test1.txt                                                        
_www.//google.com                                                                                        
_www.//yahoo.com                                                                                        

[root@machine2 ~]# sed 's|http:|www.|' test1.txt                                                        
www.//google.com                                                                                        
www.//yahoo.com                                                                                        

[root@machine2 ~]# sed 's|http://|www.|' test1.txt                                                        
www.google.com                                                                                            
www.yahoo.com                                                                                            

[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.                                                

Searches for a specific pattern and add some extra characters, symbols using &

[root@machine2 ~]# sed 's/Linux/{&}/' 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.                                              

[root@machine2 ~]# sed 's/Linux/,&/' 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.                                              

[root@machine2 ~]# sed 's/Linux/&,/' 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.                                              

To include the same patter once again use &&

only in First occurance.

[root@machine2 ~]# sed 's/Linux/{&&}/' test.txt
{LinuxLinux}  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.

Includes the extra characters to all the matching pattern.

[root@machine2 ~]# sed 's/Linux/{&}/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}.

Search and replaces the matching pattern and prints line double.

[root@machine2 ~]# sed 's/Linux/unix/p' test.txt
unix  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.
unix  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.

Search, replace and prints the line.

[root@machine2 ~]# sed -n 's/Linux/unix/p' test.txt
unix  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.

Executing multiple sed commands in single line using | symbol and -e option

[root@machine2 ~]# sed  's/Linux/LINUXOS/' test.txt | sed 's/Unix/UnixOS/'
LINUXOS  is a UnixOS-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.

[root@machine2 ~]# sed -e 's/Linux/LINUXOperatingSystem/' -e 's/Unix/UnixOperatingSystem/' test.txt
LINUXOperatingSystem  is a UnixOperatingSystem-like computer operating system. Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.


[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.

Replaces word Linux with unix in the second line of the file
                                                                             
[root@machine2 ~]# sed '2 s/Linux/unix/' test.txt
Linux  is a Unix-like computer operating system.
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning Linux is very easy.                                                                                

Replaces word Linux with unix in the first and second line of the file. We can also specify the line range.

[root@machine2 ~]# sed '1,2 s/Linux/unix/' test.txt                                                        
unix  is a Unix-like computer operating system.                                                            
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning Linux is very easy.                                                                                

Replaces word Linux with unix from first to last line of the file

[root@machine2 ~]# sed '1,$ s/Linux/unix/' test.txt
unix  is a Unix-like computer operating system.  
unix is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                  
Learning unix is very easy.                                                                                

Search for a specific pattern and replaces it.
                                             
[root@machine2 ~]# sed '/Linux/ s/Linux/centos/' test.txt                                                  
centos  is a Unix-like computer operating system.                                                          
centos is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                
Learning centos is very easy.                                                                              

Here, Search for the occurances a matching pattern and replaces it with the given word.

[root@machine2 ~]# sed '/Linux/ s/Unix/Fedora/' test.txt                                                  
Linux  is a Fedora-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.    

In above searches for Unix and replaces with Fedora.


Prints the line twice in a file.

[root@machine2 ~]# sed 'p' test.txt                        
Linux  is a Unix-like computer operating system.            
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.                                                                                                
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.                                                                                
Learning Linux is very easy.  

Like grep command fetches and prints the line where the matching string found.
                                                                           
[root@machine2 ~]# sed -n '/free/ p' test.txt
Linux is assembled under the model of free and open source software development and distribution.Love to work with Linux.                                                                                                

Prints the line other than the matching string line.

[root@machine2 ~]# sed -n '/free/ !p' test.txt                                                              
Linux  is a Unix-like computer operating system.                                                            
Learning Linux is very easy.                                                                                

Add a new line after the matching pattern.

[root@machine2 ~]# sed '/Learning/ a "Adding a new line" ' 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.                                                                                
"Adding a new line"

Add a new line before the matching pattern.

[root@machine2 ~]# sed '/Learning/ i "Adding a new line" ' 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.
"Adding a new line"
Learning Linux is very easy.

Change the line where the matching string found.

[root@machine2 ~]# sed '/Learning/ c "chaning with a new line" ' 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.
"chaning with a new line"

[root@machine2 ~]# sed '/Learning/ c "changing with a new line" ' 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.
"changing with a new line"

Transfrom with the specific letters like tr command.

[root@machine2 ~]# sed 'y/L/l' test.txt
sed: -e expression #1, char 5: unterminated `y' command

[root@machine2 ~]# sed 'y/L/l/' 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

No comments: