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.
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.
The awk is more powerful and robust than sed. It is similar to a programming language.
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With