Tuesday, July 24, 2012

Shell Scripting

1. To remove all the empty lines in a file
            #grep -v "^$" testfile where testfile - filename

^ - means beginning of the line
$ - end of the line
           
Above characters are called anchors . 

One more example : 

grep -i  ^ts testfile

Which will display the line which has "ts" in the beginning of the lines


Usage of sed:

Deleting all the lines matching a particular pattern:

Take the vfstab file where you want to delete all the lines containing tmpfs

#sed /tmpfs/d vfstab

Where d indicates delete all the lines matching pattern tmpfs
similarly p means print. Lets do one exercise.

I want to print only the ufs filesystem specified in vfstab file

#sed -n /ufs/p vfstab

Where -n option is used to print only those lines matching the pattern else all lines will be displayed.

There are few operators which are commonly used

p - print
d - delete
g - global - which means operation is performed on all the occurrence of the pattern.
s - substitute

All other operators, i believe is self explanatory.
 


# echo "suman mandal" | sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'

Output of the above command will
mandal suman

vxprint -g oracle -dF "%publen" | awk 'BEGIN {s = 0} {s += $1} END {print s/2097152, "GB"}'


2 comments:

Anonymous said...

Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. Kudos

Anonymous said...

Woah! I’m really loving the template/theme of this site. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between user friendliness and appearance. I must say you have done a amazing job with this. In addition, the blog loads super quick for me on Firefox. Exceptional Blog!