sed、grep、find、awk、gawk 之间有什么关系,首选哪个?

发布于 2024-10-09 04:32:18 字数 91 浏览 0 评论 0原文

我是 Linux/Unix 新手。我发现使用这些命令可以通过多种方式完成某些任务。他们之间有什么关系吗?或者说更喜欢哪一个呢?哪一个已经过时了?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月棠 2024-10-16 04:32:18

请参阅 SO 366980 了解有关Perl、Python、Awk 和 Sed 之间的区别。

五个命令列表中有四个不同的命令; awk 和 gawk 密切相关,GNU Awk 是 Awk 的 GNU 实现。

  • find 用于根据文件特征(例如名称或修改时间)在一组目录中查找文件。 GNU Find 比传统或 POSIX 版本的 Find 具有更多的功能。

  • grep 用于使用正则表达式来定位文件中的内容来控制选择的内容。 GNU Grep 比传统的或 POSIX 版本的 Grep 具有更多的功能。

  • sed 用于使用编辑命令(包括正则表达式)修改文件内容。 GNU Sed 比传统或 POSIX 版本的 Sed 具有更多功能。

  • awk 是一种模式匹配和格式化语言。它是一种编程语言,而您提到的其他工具则不是。当需要的时候,它非常有用。然而,Perl 和 Python 也具有 Awk 的功能和许多附加功能,因此很多人使用它们来代替 Awk。 GNU Awk 比传统或 POSIX 版本的 Awk 具有更多功能。

因此,您列出的工具可以完成不同的工作,但可以通过多种方式协同工作。您应该了解的另一个工具是 xargs,它获取文件列表并依次对每个文件运行指定的命令。

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.

可是我不能没有你 2024-10-16 04:32:18

AWK 是由 Aho、Weinberger 和 Kernighan 设计的一种编程语言。 gawk 是 AWK 的一种实现,但还有其他几种实现,包括 mawknawk。它是一种成熟的编程语言,具有变量、控制结构和关联数组,但通常针对处理 UNIX 系统上常见的基于文本的数据进行了优化。

sed 是流编辑器,受到 ed 编辑器的启发。它有一个简单的命令集,主要限于逐行编辑。 sed 命令可以很容易地在 awk 中模仿。以下是等效的:

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

grep 查找文本。基本的 grep 功能可以在 sedawk 中轻松模仿。以下是等效的:

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

哎呀,错过了find

find 遍历文件系统树,根据指定的标准执行操作。例如,

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

将从当前目录 . 开始遍历所有文件和目录,排除(且不下降到)名称以 . 开头的任何目录,并运行 cat 在名称不以 ~ 结尾的每个文件上(结果是打印出该文件的内容)。同样,这在 AWK 或 Perl 或许多其他编程语言(甚至在本例中是纯 shell)中都是可行的,但使用专用工具更容易、更快地编写和理解。

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.

聆听风音 2024-10-16 04:32:18

不同的工具适合不同的工作。问题就像“什么是更好的工具:锤子、螺丝刀还是组合钳?”

grep - 在文件/流中查找/过滤文本。行匹配/不匹配 - 将其流式传输到输出。用于将输出量减少到您需要的量。如果您获得太多信息,通常以交互方式使用。也经常在脚本中用于拉出“包含我需要的内容的一行”

通常在脚本化使用 grep 之后,您会看到 awk (复杂的表达式或只是一个老派程序员)或 cut (速度更快但相当简单)提取一个值。 (grep 水平切,cut/awk 垂直切)

使用 awk 进行全功能编程目前几乎见不到。

sed - 搜索和替换,通常是脚本化的(如果您想以交互方式进行操作,可视化编辑器会更好 - 您会看到正在执行的操作。)

find - 查找符合给定(高级)参数的文件。

Different tools for different jobs. The question is like "what is a better tool: a hammer, a screwdriver or combination pliers?"

grep - find/filter text in a file/stream. Line matches/doesn't match - stream it to output. Use to reduce amount of output to what you need. Commonly used interactively if you get too much information. Also often used in scripts to pull "that one line containing what I need"

Usually after scripted use of grep you see either awk (either a complicated expression or just an old-school programmer) or cut (which is much faster but pretty simplistic) to extract the one value. (grep chops horizontally, cut/awk chops vertically)

Using awk for fully-featured programming is currently almost unseen.

sed - search-and-replace, usually scripted (a visual editor is much better for that if you want to do it interactively - you see what is being done.)

find - find files conforming to given (advanced) parameters.

负佳期 2024-10-16 04:32:18

这些工具执行不同的操作,因此首选哪一个取决于您想要做什么。

在您提到的工具中,find 与其他工具略有不同 - 它在文件系统上搜索具有某些属性(名称、日期、权限...)的文件。

sedgrep 将文本文件作为输入,对其进行操作,然后输出结果。他们都大量使用正则表达式。 sed(“Stream EEditor”)通常用于搜索和替换操作。 grep(“全局正则表达式打印”)在文本文件中输出与某些模式匹配的行。

在 Linux 系统上,awkgawk 通常是同一个程序的不同名称,即 AWK 的 GNU 版本(以其创建者“Aho、Weinberger、Kernighan”命名)。 awk 也可以指没有 GNU 扩展的 awk 的“经典”版本,并且还有其他变体 nawk (" new awk”)和 mawk (“迈克的 AWK”)。 awk 的作用远不止于模式匹配;它确实是一种成熟的编程语言,尽管它旨在将输入文本分解为行和字段并对其进行操作(包括数字操作,因此很容易执行诸如总计列或进行统计计算之类的操作)。

These tools do different things, so which one is preferred depends on what you want to do.

Of the tools you mention, find is a little different than the others -- it works on filesystems to search for files with certain attributes (name, date, permissions...).

sed and grep take text files as input, operate on them, and output the results. They both heavily use regular expressions. sed ("Stream EDitor") is generally used for search-and-replace operations. grep ("Global Regular Expression Print") outputs lines in a text file that match some pattern.

On Linux systems, awk and gawk are generally different names for the same program, the GNU version of AWK (named after its creators "Aho, Weinberger, Kernighan"). awk can also refer to the "classic" version of awk that doesn't have the GNU extensions, and there are other variants nawk ("new awk") and mawk ("Mike's AWK"). awk does far more than pattern matching; it's really a full-blown programming language, though it's geared toward breaking up input text into lines and fields and operating on them (including numeric operations, so it's easy to do things like total up columns or do statistical calculations).

安人多梦 2024-10-16 04:32:18

按我使用它们的频率顺序列出,并附有评论。有关更多详细信息,请在命令行尝试 man 命令,例如 man grep 以获取更多详细信息。

grep - 打印与模式匹配的行 * 我个人认为这是最有用的。

grep foo *.txt
grep 192.168.1.1 output.log
grep -i steve */*.txt 

find - 遍历文件层次结构 * 可用于查找子目录中的文件或
在文件上执行 shell 命令

find . -name lost.txt -print
find . -name "*.txt" -print
find . -type f -exec chmod 0644 {} \;

sed - 流编辑器 * 帮助自动对批量文件进行简单的文本编辑

sed "s/abc/ABC/" foo.txt

awk - 模式定向扫描和处理语言 * 很长时间没有使用它了。
如果需要使用 awk,我通常会使用 Ruby 之类的东西。

Listed in order of how often I use them, appended with comment. For more details, try man command, eg man grep at the command line for more details.

grep - print lines matching a pattern * I personally find this the most useful.

grep foo *.txt
grep 192.168.1.1 output.log
grep -i steve */*.txt 

find - walk a file hierarchy * can be used to find files in subdirectories or
execute shell commands on files

find . -name lost.txt -print
find . -name "*.txt" -print
find . -type f -exec chmod 0644 {} \;

sed - stream editor * helps automate simple text edits on batches of files

sed "s/abc/ABC/" foo.txt

awk - pattern-directed scanning and processing language * haven't used it for a long time.
If it gets to the point I need to use awk, I generally use something like Ruby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文