LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 11-01-2012, 06:04 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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!
 
Old 11-01-2012, 06:19 AM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Slack14_64_Multilib
Posts: 3,039
Blog Entries: 4

Rep: Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734
clarify "advanced comparison" please...
 
Old 11-01-2012, 06:30 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 11-01-2012, 07:50 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by Habitual View Post
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.
 
Old 11-01-2012, 08:12 AM   #5
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Debian, OS X (bsd)
Posts: 143

Rep: Reputation: 27
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.
 
Old 11-01-2012, 08:14 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
Old 11-01-2012, 08:24 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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".
 
Old 11-01-2012, 09:34 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 11-01-2012, 09:46 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Got something interesting here at: http://www.theunixschool.com/2012/09...rn-search.html
 
Old 11-01-2012, 10:13 AM   #10
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Debian, OS X (bsd)
Posts: 143

Rep: Reputation: 27
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.
Old 11-01-2012, 10:22 AM   #11
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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!
 
Old 11-01-2012, 10:30 AM   #12
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 740
Blog Entries: 2

Rep: Reputation: 192Reputation: 192
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.
 
Old 11-01-2012, 10:51 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by shivaa View Post
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.
Old 11-01-2012, 12:17 PM   #14
Thad E Ginataom
Member
 
Registered: Mar 2011
Distribution: Ubuntu 12.04 with KXStudio, MATE & Compiz
Posts: 46

Rep: Reputation: 7
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.
Old 11-01-2012, 12:48 PM   #15
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Special thanks to you @Thad E Ginataom! This book is really looking a treasure.
I am thankful to everyone for your time & efforts.
 
  


Reply

Tags
awk, grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep and AWK keogk Linux - General 10 01-04-2010 08:03 PM
awk tutorial mindseye Linux - General 5 07-13-2009 02:24 AM
Best awk array tutorial bijit1709 Programming 1 01-25-2008 01:04 AM
Tutorial on sed & awk LinuxTiro Programming 4 11-10-2003 03:53 AM
Good tutorial for sed/awk or both !! paonethestar Programming 2 11-08-2003 03:38 PM


All times are GMT -5. The time now is 11:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration