Using sed for word count

Not sure how much use this is but using sed to count number of strings in a file e.g. how many times does Python appear in test.txt

[oracle@usnyssmtaora03 ~]$ cat test.txt
1
2
Python
this is a test
34
9
Perl
Python
Python
Python
Python
Python
Python
Python

[oracle@usnyssmtaora03 ~]$ sed -n 's/Python/&/gp' test.txt |sed 's/Python/&\n/g'|sed '/^ *$/d'|sed -n '$='
8


Search for cubica in a file i.e. string /u01/cubica - replace the string with cubicb and keep a previous copy of the file.

#!/bin/sh

for f in `find . -name '*.txt' -exec grep -il 'cubica' {} \;`
do
sed 's?cubica?cubicb?g' $f > ${f}.new
cp -p $f ${f}.prev
mv ${f}.new $f
done



No comments:

Post a Comment