Bash
Script to email changes in a file
Submitted by madvip on Fri, 02/01/2008 - 12:48.Suppose you want to email changes in the Oracle's alert log, or maybe syslog's messages, it would be nice to email new 'tailed' entries. Something like tail -f logfile | mailx ... won't work as tail -f will never pipe any output. The idea is to take a snapshot of the file every x minutes, archive it, take another snapshot after x minutes, diff the two files, and email the changes. Then you roundrobin the snapshot and repeat the whole process:
crontab -l */5 * * * * /usr/local/bin/swatch-action.sh > /dev/null 2>&1 [root]# cat /usr/local/bin/swatch-action.sh
Search and Replace in VI
Submitted by madvip on Wed, 01/23/2008 - 12:11.To search and replace text in vi:
% (entire file) s (search and replace) /old text with new/ c (confirm) g (global - all) :%s/oldstring/newstring/cg
To ignore case during search
:set ic
Insert a string before or after each line of a file
Submitted by madvip on Fri, 01/11/2008 - 10:55.Let's say you have a list of tables inside a text file called e_tables.txt and you want to generate an SQL file to drop these tables. With sed this is a very easy task to do:
sed -e 's/.*/DROP TABLE & CASCADE CONSTRAINTS;/' e_tables.txt
The code can be interpreted as follows. The ".*" tells sed to match every line and replace it with strings "DROP TABLE", "CASCADE CONSTRAINTS" and the matched line, denoted by "&".
Read a text file line by line in Bash
Submitted by madvip on Wed, 12/05/2007 - 13:47.The code is very simple:
while read line; do echo $line; done < file.txt
To output the result to another text file, for example after some text manipulation:
while read line; do echo "Manipulated $line"; done < file.txt > output.txt

