Differences between grep, awk and sed

Possible duplicate:
What are the differences between Perl, Python, AWK and sed?
What is the difference between sed and awk?

This may not be a very specific question, but I'm confused by the differences between grep , awk and sed in terms of their role in Unix / Linux system administration and text processing.

+75
linux bash grep awk sed
Oct 11 2018-11-11T00:
source share
3 answers

Brief definition:

grep : search for specific terms in a file

 #usage $ grep This file.txt Every line containing "This" Every line containing "This" Every line containing "This" Every line containing "This" $ cat file.txt Every line containing "This" Every line containing "This" Every line containing "That" Every line containing "This" Every line containing "This" 

Now awk and sed completely different from grep . awk and sed are word processors. They not only have the ability to find what you are looking for in the text, but also the ability to delete, add and modify text (and much more).

awk is mainly used to retrieve data and reports. sed - stream editor Each of them has its own functionality and specialty.

Example
Sed

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

Awk

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

The main use of awk :
Calculate the sum / average value / max / min / etc. what you might need.

 $ 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 

I recommend you read this book: Sed and Awk: 2nd Ed.

This will help you become an experienced sed / awk user in any unix-like environment.

+116
Oct 11 '11 at
source share

Grep is useful if you want to quickly find lines matching a file. It can also return some other simple information, such as matching line numbers, matches, and file name lists.

Awk is a whole programming language created for reading CSV files, processing records and, if necessary, printing out a set of results data. It can do many things, but it is not the easiest tool for simple tasks.

Sed is useful when you want to make changes to a file based on regular expressions. This makes it easy to match parts of the lines, make changes, and print the results. This is less expressive than awk, but makes it a little easier to use simple tasks. This has many more complex statements that you can use (I think this is even completed), but in general you will not use these functions.

+36
Oct. 11 2018-11-11T00:
source share

I just want to mention a thing, there are many tools that can do text processing, for example. sort, cut, split, join, paste, comm, uniq, column, rev, tac, tr, nl, pr, head, tail .....

they are very convenient, but you should study their options, etc.

The lazy way (not the best way) for learning text processing might be: just learn grep, sed and awk. With these three tools, you can solve almost 99% of the problems with text processing and do not need to remember above other cmds and parameters. :)

And, if you studied and used three, you knew the difference. In fact, the difference here means which tool is good at solving which problem.

a more lazy way could be learning the script language (python, perl or ruby) and any text processing with it.

+24
Oct 11 '11 at 19:51
source share



All Articles