I’ve been a Linux user for a while, and I’ve a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, and sed.
About the only thing I’ve used grep for is piping stuff into it to find files in log files, the output of ps etc. I haven’t used awk or sed at all. Are there any good tutorials for these utilities?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Typically, grep is used for search operations, sed is used for search and replace operations and awk is particularly well suited for tabular data and has a lower learning curve than some alternatives. There are overlapping features between these tools as well, see: When to use grep, less, awk, sed
grep
- Documentation:
man grep,info grep, GNU grep online manual - An introduction to grep and egrep
- grep tutorial
- ripgrep – alternate implementation, recursive by default, respects
.gitignore, Rust/PCRE2 regex, SIMD, etc - Ebook on GNU grep and ripgrep
sed
- Documentation:
man sed,info sed, GNU sed online manual - Sed – An Introduction and Tutorial
- Sed by Example Part 1, Part 2, Part 3
- sed tutorial
- Ebook on GNU sed one-liners
awk
- Documentation:
man awk,info awk,info gawk, GNU awk online manual - AWK: A Tutorial and Introduction
- Awk by Example Part 1, Part 2, Part 3
- An AWK Primer (alt link)
- Ebook on GNU awk one-liners
Further Reading
Method 2
The O’Reilly sed and awk book is great for er sed and awk.
Method 3
I wrote a book on sed—Definitive Guide to sed—that includes a tutorial. It fully covers sed, as well as related commands like grep, tr, head and tail. Also fully covers regular expressions much better than I’ve seen elsewhere.
I agree with others that solid understanding regular expressions well is very important. I also agree that sed is best used for simpler tasks, more complex scripts quickly get obscure.
I disagree that awk is obsolete, just the opposite. It’s like many unix things (e.g., vi), there is a learning curve, but it’s worth it.
I disagree with the suggestion to use awk in place of grep. Does not make sense in my experience. grep is so great and simple.
Method 4
The Regular Expressions Cookbook published by O’Rielly would be enough to get you anywhere in any language that uses them.
Method 5
The authors of the book are Kernigan and Pike the title is something like “The Unix Programming Environment”.
The book that I actually learned from was called “An Introduction to Berkely Unix”.
Method 6
If you are to learn one out of these 3( grep , sed and awk ) , you can just learn awk/gawk.. awk can do grep and sed’s functions, ie using regex to search/replace text, plus much more because its also a programming language. If you learn the inside outs of gawk/awk, you won’t need to use grep/sed/wc/cut etc. Just one tool does it.
Method 7
In my opinion, awk is more or less obsolete (however, others will strongly argue this opinion), most people I know nowadays instead use some script language like perl or today often python for advanced text manipulation. sed is great for simpler text manipulation tasks, I use it all the time. I learned it mainly by looking at sed one-liners like those at http://sed.sourceforge.net/sed1line.txt. Regarding grep: Well you basically want to get a solid understanding of regular expressions (also needed for sed). I here just used the texinfo manuals.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0