Friday, August 20, 2010

UNIX Tips

'VI' Search and Replace: -

Go to the Command Mode(By Pressing ‘Esc’)

First occurrence on current line: :s/OLD/NEW

On current line: :s/OLD/NEW/g

Between two lines #,#: :#,#s/OLD/NEW/g (example 1,10s/OLD/NEW/g)

Every occurrence in file: :%s/OLD/NEW/g

 To append the text at the end of  the line   
                     %s/$/ \>\> \/tmp\/test\/test.log/g  
Where
' \' - is a DELIMITER
Above command will add '>> /tmp/test/test.log' to end of the each line.
Eg, File having the line 
                    cat session.sql
                    ALTER SYSTEM KILL SESSION '358, 22' IMMEDIATE ; 
                    Would become
                    ALTER SYSTEM KILL SESSION '358, 22' IMMEDIATE ; >> /tmp/test/test.log  
To add the text infront of the line         
                    %s/^/SQL\> /
                    Would become
                    SQL> ALTER SYSTEM KILL SESSION '358, 22' IMMEDIATE ; >>  /tmp/test/test.log    
Remove ^M(Carriage Return) from the file:
You could face this issue when doing FTP/SFTP from Windows to Unix.
To remove ^M for say 100 files in a directory.
Just go the the directory  and run
                    perl -p -i -e 's/\r\n$/\n/g'   *
$  -  for the end 
 ^  -  for the beginning 
\  -  as a delimiter to special character (like > < $ etc) if putting special symbol..put "\" and then place the actual character you want to display. 
% - place after the command prompt to makes changes across the whole file.

No comments: