Board logo

标题: [问题求助] gawk和sed各自的特点是什么 [打印本页]

作者: 随风    时间: 2011-6-2 18:17     标题: gawk和sed各自的特点是什么

gawk 和 sed 各自的特点是什么,或者说各自的优缺点是什么。
用批处理处理文本的时候最多,但bat处理文本太弱,所以一直想学sed gawk
因理解能力和精力有限打算先从一样入手,不知该选择哪样.
sed 太晦涩,感觉gawk要容易入门点,但不知道二者功能上有什么区别,望老手们指点指点。

另外再问句,sed 或 gawk 对行的长度有限制吗?会不会有溢出的情况发生?
作者: Demon    时间: 2011-6-2 19:27

本帖最后由 Demon 于 2011-6-2 19:29 编辑

处理文本传说是Perl比较牛逼,菜鸟路过。
作者: Batcher    时间: 2011-6-2 19:44

1) What is the difference between awk and sed ?

Both are tools that transform text. BUT awk can do more things besides just manipulating text. Its a programming language by itself with most of the things you learn in programming, like arrays, loops, if/else flow control etc You can "program" in sed as well, but you won't want to maintain the code written in it.

2) What kind of application are best use cases for sed and awk tools ?

Conclusion: Use sed for very simple text parsing. Anything beyond that, awk is better. In fact, you can ditch sed altogether and just use awk. Since their functions overlap and awk can do more, just use awk. You will reduce your learning curve as well.

大概意思是说awk功能更加丰富一些,它的数组、循环、流程控制等显得容易些。

sed is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two "variables": pattern space and hold space. Readability of scripts can be difficult. Mathematical operations are extraordinarily awkward at best.

awk is oriented toward delimited fields on a per-line basis. It has much more robust programming constructs including if/else, while, do/while and for (C-style and array iteration). There is complete support for variables and single-dimension associative arrays plus (IMO) kludgey multi-dimension arrays. Mathematical operations resemble those in C. It has printf and functions. The "K" in "AWK" stands for "Kernighan" as in "Kernighan and Ritchie" of the book "C Programming Language" fame (not to forget Aho and Weinberger). One could conceivably write a detector of academic plagiarism using awk.

大概意思是说sed侧重于行处理,awk侧重于列处理。
作者: xslxslxsl    时间: 2011-8-17 23:34

我也是很想知道,学习学习




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2