4 ms·
What is better? Starting with awk or sed?
by ymgch 3y ago
What is better? Starting with awk or sed?
- asicsp 3y agoDepends on the task. Sed is typically used for search and replace and Awk is better suited for field based processing. Both these tools also have filtering features (regexp based, line number based, range, etc). See also: When to use grep, sed, awk, perl, etc https://unix.stackexchange.com/q/303044 https://unix.stackexchange.com/q/303044
- _ZeD_ 3y agoWhile each with their scope and idiosyncrasies ... they're pretty similar, at least in the pattern matching part, and both of them have a pretty internal "core" of functionalities that is easy to grasp. so the honest answer IMHO is ... "both"
- alpaca128 3y agoIf you're familiar with Vim's search/replace syntax you already know how to use "sed -e" to replace text, that's how I got into it.
- chasil 3y agoA sed binary is usually much smaller than an awk binary, either POSIX or GNU. The memory footprint of sed will be much more compact. However, sed has grown out of the command language used by the tty editors, and is more difficult to program (although it is Turing-complete). The awk language implements much of the syntax of C, and it is not difficult to write a very slow and inefficient script. This inefficiency is harder to reach in sed, because it takes more effort to abuse it. O'Reilly's book on sed and awk is available free online, both to browse and to download as a ZIP. https://docstore.mik.ua/orelly/unix/index.htm https://docstore.mik.ua/orelly/unix/index.htm
- Anthony-G 3y agoFor those who care about copyright, that URL is not from O’Reilly; it’s a copy of a book-set that O’Reilly used to distribute via CD-ROM – with a nice user interface that used web technologies (even included a search feature). O’Reilly could make it available for free – as they’ve done so for other books such as Apache Security¹ or Using Samba² – but they still (as is their right) expect you to pay for the sed & awk book³. ¹ https://blog.ivanristic.com/2015/02/apache-security-ten-years-later.html https://blog.ivanristic.com/2015/02/apache-security-ten-year... ² https://www.oreilly.com/openbook/samba/book/ https://www.oreilly.com/openbook/samba/book/ ³ https://www.oreilly.com/library/view/sed-awk/1565922255/ https://www.oreilly.com/library/view/sed-awk/1565922255/
- e63f67dd-065b 3y agoThe old sysadmin in me says to forget both and just learn Perl.
- radiator 3y agoStart with sed. It can take you a long way and it is more succint.
- pphysch 3y agoI'd start with AWK because it is covers more use cases
- whartung 3y agoDepends on your use case. I can't speak to sed, I don't know it very well. Awk is my SAK. But I learned awk while sitting in an office at a client site. I forget the specific scenario, but I wanted to split up some files into some other files. I didn't even know awk, but grokked enough from the man page to let me do what I wanted to do. I can't even say what provoked me to turn to awk in the first place. I do know I ran into some internal open file limits, but worked around that. If you want to tear files apart, or summarize them in some way, or push the fields around, awk is much better. sed is an editor. If I have a sed scenario, I'm more apt to just do it in vi and save the result than stitch together some pipeline with sed. Most of my use cases are one off processing and analysis. I've never had any workflows that relied on awk or most anything like that. It was almost all throw away code, a tool on the workbench, not the production line.
- dredmorbius 3y agoEach has its uses, and there are things which are more easily achieved in one than the other. (I have a set of scripts I use to parse NOAA's weather web page to plain text, and ended up resorting to both sed and awk in the process, and haven't yet tried to simplify that to a single script.) Sed is usually used for simple text substitutions and manipulations. Awk has built-in record and array concepts, as well as more standard programming constructs (loops, if/then, case/switch, printf, and external system interfaces (launching and/or reading from external programmes). My view is that the tools overlap considerably, but also complement one another strongly.