Skip to Content

grep, awk and sed

Posted on
  • grep: search for specific terms in a file. It can also return some other simple information like matching line numbers, match count, and file name lists.
$ grep This file.txt

Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"
``

* `awk` and `sed` are completly different than grep. They are text processors. Not only do they have the ability to find what you are looking for in text, they have the ability to remove, add and modify the text as well (and much more).

awk is mostly used for **data extraction and reporting**. sed is a **stream editor**.

```sh
$ sed -i 's/cat/dog/' file.txt
# this will replace any occurrence of the characters 'cat' by 'dog'

$ awk '{print $2}' file.txt
# this will print the second column of file.txt

$ cat file.txt
A 10
B 20
C 60
$ awk 'BEGIN {sum=0; count=0; OFS="\t"} {sum+=$2; count++} END {print "Average:", sum/count}' file.txt
Average:    30

References

comments powered by Disqus