Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the relationship between sed, grep, find, awk, gawk, which one is preferred?

I'm new to Linux/Unix. I find some task can be accomplished by many ways using these commands. Is there some relationship between them? Or which one is preferred? Which one is outdated?

Thanks.

like image 346
Just a learner Avatar asked Dec 21 '10 07:12

Just a learner


People also ask

What is the difference between grep and awk and sed?

Grep is used for finding text patterns in a file and is the simplest of the three. Sed can find and modify data, however, its syntax is a bit more complex than grep. AWK is a full-fledged programming language that can process text and perform comparison and arithmetic operations on the extracted text.

Which is better awk or sed?

The awk is more powerful and robust than sed. It is similar to a programming language.

What is the difference between sed and grep?

The sed command is a stream editor that works on streams of characters. It's a more powerful tool than grep as it offers more options for text processing purposes, including the substitute command, which sed is most commonly known for.

What are sed and awk used for?

What Is AWK? AWK, like sed, is a programming language that deals with large bodies of text. But while people use sed to process and modify text, people mostly use AWK as a tool for analysis and reporting. Like sed, AWK was first developed at Bell Labs in the 1970s.


2 Answers

See SO 366980 for discussion about the differences between Perl, Python, Awk and Sed.

There are four distinct commands in the list of five; awk and gawk are closely related, with GNU Awk being the GNU implementation of Awk.

  • find is for locating files in a set of directories based on file characteristics such as name or modification time. GNU Find has many more capabilities than the traditional or POSIX versions of Find.

  • grep is for locating content within files using regular expressions to control what is selected. GNU Grep has many more capabilities than the traditional or POSIX versions of Grep.

  • sed is for modifying the contents of files using editing commands, including regular expressions. The GNU Sed has many more capabilities than the traditional or POSIX versions of Sed.

  • awk is a pattern matching and formatting language. It is a programming language in a way that the other tools you mention are not. When needed, it is very useful. However, Perl and Python also have the capabilities of Awk and many extras, so many people use them instead of Awk. GNU Awk has some more capabilities than the traditional or POSIX versions of Awk.

So, the tools you list do different jobs, but can work together in many ways. One other tool you should be aware of is xargs, which takes lists of files and runs a specified command on each of the files in turn.

like image 79
Jonathan Leffler Avatar answered Sep 24 '22 04:09

Jonathan Leffler


AWK is a programming language designed by Aho, Weinberger, and Kernighan. gawk is one implementation of AWK, but there are several others, including mawk, and nawk. It is a full-blown programming language with variables, control structures, and associative arrays, but generally optimized for dealing with the sorts of text-based data commonly found on UNIX systems.

sed is the stream editor, inspired by ed the editor. It has a simple command set mostly limited to line-by-line editing. sed commands can easily by imitated in awk. The following are equivalent:

sed -e 's/foo/bar/g'
awk '{ gsub(/foo/, "bar"); print $0 }'
awk '{gsub(/foo/,"bar")}1'

grep finds text. Basic grep functionality can easily be imitated in sed and awk. The following are equivalent:

grep 'foo.bar'
sed -n -e '/foo.bar/p'
awk '/foo.bar/ { print $0 }'
awk '/foo.bar/'

Oops, missed find.

find walks the filesystem tree, performing actions according to specified criterion. For example,

find . -name '.*' -prune -o ! -name '*~' -type f -exec cat '{}' \;

will walk all files and directories starting from the current directory ., excluding (and not descending into) any directories with names starting with ., and run cat on every file whose name does not end with ~ (with the result of printing out the contents of that file). Again, this would be doable in AWK or Perl or many other programming languages (or even pure shell, in this example), but is easier and faster to write and understand with a special-purpose tool.

like image 30
ephemient Avatar answered Sep 23 '22 04:09

ephemient