Efficient Shell Scripting
1) Grep for pattern in a file. Inefficient way:
$ cat filename | grep "pattern"
Efficient way:
$ grep "pattern" filename
2) Counting the number of matching lines in a file Inefficient way:
$ cat filename | grep "pattern" | wc -l
Efficient way:
$grep -c "pattern" filename
3) Listing unique lines in a file. Inefficient way:
$ sort filename | uniq
Efficient way:
$ sort -u filename
4) Don't need to use grep and awk together. Inefficient way:
$ grep "pattern" filename | awk '{print $1}'
Efficient way:
$ awk '/pattern/ {print $1}' filename
5) Use shell glob rather than spawning "ls" process for manipulating files. Inefficient ways:
$ for i in `ls` ; do echo $i; done $ for i in `ls *.txt`; do echo $i; done
Efficient ways:
$ for i in *; do echo $i; done $ for i in *.txt; do echo $i; done