Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-01-2012, 06:04 AM
|
#1
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
|
Looking for tutorial - Awk Vs grep
Hello Friends,
Could anybody provide me links to any online available document or tutorial or blogs about "awk vs grep". I have though searched over Internet, but couldn't find anything useful.
I am looking for some tutorials having some advanced comparision between these two commands. My goal is to avoid using multiple filters again and again in commands & shell scripts. Looking for something that is short & sweet like awk.
Thanks for your help!
|
|
|
|
11-01-2012, 06:19 AM
|
#2
|
|
Senior Member
Registered: Jan 2011
Distribution: Slack14_64_Multilib
Posts: 1,491
Rep: 
|
clarify "advanced comparison" please...
|
|
|
|
11-01-2012, 06:30 AM
|
#3
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 623
|
The difference is that grep is a simple tool for searching patterns in text, while awk is an advanced scripting language that alows you to process and modify text in practically any way you want. Anything that you can do with grep can be done with awk, too. If I over-simplified it: if you know awk, you don't even need grep. The only reason to use grep is when the grep command happens to be shorter.
|
|
|
|
11-01-2012, 07:50 AM
|
#4
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
Original Poster
|
Quote:
Originally Posted by Habitual
clarify "advanced comparison" please...
|
I need overall comparision between awk and grep, which can give me a clear idea and I can replace grep all the time in my work.
I need a tutorial.
|
|
|
|
11-01-2012, 08:12 AM
|
#5
|
|
Member
Registered: Apr 2005
Distribution: Ubuntu, Debian, OS X (bsd)
Posts: 93
Rep:
|
As millgates mentioned, they are two very different things. AWK is an interpreted programming language. Grep is a plain utility that does simple pattern matching. To get started with awk, just search for awk and/or tutorial or howto. That will bring up many useful guides like this one: http://www.grymoire.com/Unix/Awk.html
Probably the difference will become more obvious if you try using them. The next step up from awk would be perl.
|
|
|
|
11-01-2012, 08:14 AM
|
#6
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
As already suggested, you will not find a comprehensive comparison of AWK and GREP---they are totally different things. I can offer one comparison: The average person should be able to learn GREP in about 5 minutes. Learning AWK might take closer to 5 DAYS.
I recommend that you just learn AWK and see for yourself---I would start here:
http://www.grymoire.com/Unix/Awk.html
|
|
|
1 members found this post helpful.
|
11-01-2012, 08:24 AM
|
#7
|
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,060
Rep: 
|
Among the Awk variants so far I find Gawk the best, and the manual was everything I needed save the examples around the web. http://www.gnu.org/software/gawk/manual/
For grep, I just needed "man grep".
|
|
|
|
11-01-2012, 09:34 AM
|
#8
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
Original Poster
|
Alright! I learned the lession - "Practice makes a man perfect". I will go through some awk tutorials for it's better understanding.
Thanks everyone for your time.
|
|
|
|
11-01-2012, 09:46 AM
|
#9
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
Original Poster
|
|
|
|
|
11-01-2012, 10:13 AM
|
#10
|
|
Member
Registered: Apr 2005
Distribution: Ubuntu, Debian, OS X (bsd)
Posts: 93
Rep:
|
It's interesting in that it shows implementations of grep capabilities using awk. That can be done but, awk is far more powerful and is usually used to manipulate the data. Whereas grep can only find a pattern.
Here is a very simple sample:
Code:
ifconfig eth0 | grep inet
ifconfig eth0 | awk '$1=="inet" {print substr($2,6)}'
ls -l /usr/bin/ | awk '{if ($5 < 3000) print }'
ls -l /usr/bin/ | awk '{ printf "%10d %s\n", $5,$9; sum += $5} END { print sum " Grand Total "}'
dpkg --get-selections | awk '$2 == "install" { print $1 }'
There's a lot more. It's hard to describe in a nutshell what awk is capable of because it can do so many things as it is a scripting language.
|
|
|
1 members found this post helpful.
|
11-01-2012, 10:22 AM
|
#11
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
Original Poster
|
Sorry to re-open a solved thread. I am aware what awk and grep is and what do they do. But my motive is to find a tutorial or lession where I can get situations in which grep can be rerplaced with awk. Because awk can be helpful in doing multiple task in a single cmd, whereas grep can't!
|
|
|
|
11-01-2012, 10:30 AM
|
#12
|
|
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,OpenBSD
Posts: 521
Rep: 
|
http://shop.oreilly.com/product/9781565922259.do
Or my advice is that any time your shell-grep-sed-awk requirements get at all complicated head straight for perl. It's just not worth a lot of effort in 2012 learning awk.
|
|
|
|
11-01-2012, 10:51 AM
|
#13
|
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,060
Rep: 
|
Quote:
Originally Posted by shivaa
Sorry to re-open a solved thread. I am aware what awk and grep is and what do they do. But my motive is to find a tutorial or lession where I can get situations in which grep can be rerplaced with awk. Because awk can be helpful in doing multiple task in a single cmd, whereas grep can't!
|
Reading the whole grep manual it's actually easy to see the limits of grep and where you'd start using awk. It's just like using a ladder, if grep is not enough, use awk (assuming you won't consider sed and egrep). If awk is not enough, use larger languages like Perl, Ruby, or Python, etc.
Also, it's always easier to learn by the problems you encounter rather than learning by available examples, but if you want to learn ahead there are many examples around the web for that. It's really easy to find them actually. Then as you read them you just have to use a reference (like the Gawk manual) to see how or why they work then that's how you learn.
---- Add ----
Of course we don't only use Awk because Grep is no longer able to handle the task(s). Some just prefer to use Awk because the resulting code is cleaner or simpler, and sometimes it's just a matter of favor, taste or preference regardless if it has a sensible reason or not.
Last edited by konsolebox; 11-01-2012 at 10:58 AM.
|
|
|
1 members found this post helpful.
|
11-01-2012, 12:17 PM
|
#14
|
|
Member
Registered: Mar 2011
Distribution: Ubuntu 12.04 with KXStudio, MATE & Compiz
Posts: 46
Rep:
|
A long time ago, when I was beginning a new career sitting in front of a Unix computer, and finding that it had a huge range of tools that were good for exploring, handling and manipulating data in text format, and also finding that I had a growing need to do so, I spent a lot of money on a book called
Unix Text Processing. by Dale Dougherty and Tim O'Reilly
Now it is available for free download at that link.
This substantial book will give you an overview of all the major *ix text editors and processing tools. It will put them in context in relationship to each other.
If you have a quick project you need to stamp "completed," this book will be too much for. If you have a personal or professional need to discover and work in this area, then it is a treasure chest.
It is the closest thing to the tutorial you originally asked for as I have ever seen, but this is not the sort of tutorial that is an hours work: this is one for weeks and months, even if (like me) you have no need to really use the Unix typesetting tools.
I can say that I built a career on this book, the Awk Programming Language book, some books on administration --- and the Unix manuals (man pages).
(I don't know what edition is online. You may, for practical purposes, need to consult man pages and other sources for up-to-date options and capabilities)
Last edited by Thad E Ginataom; 11-01-2012 at 12:19 PM.
|
|
|
1 members found this post helpful.
|
11-01-2012, 12:48 PM
|
#15
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,676
Original Poster
|
Special thanks to you @Thad E Ginataom! This book is really looking a treasure.
I am thankful to everyone for your time & efforts.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:01 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|