LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looking for tutorial - Awk Vs grep (http://www.linuxquestions.org/questions/linux-newbie-8/looking-for-tutorial-awk-vs-grep-4175435051/)

shivaa 11-01-2012 06:04 AM

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!

Habitual 11-01-2012 06:19 AM

clarify "advanced comparison" please...

millgates 11-01-2012 06:30 AM

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.

shivaa 11-01-2012 07:50 AM

Quote:

Originally Posted by Habitual (Post 4819589)
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.

Turbocapitalist 11-01-2012 08:12 AM

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.

pixellany 11-01-2012 08:14 AM

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

konsolebox 11-01-2012 08:24 AM

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".

shivaa 11-01-2012 09:34 AM

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.

shivaa 11-01-2012 09:46 AM

Got something interesting here at: http://www.theunixschool.com/2012/09...rn-search.html

Turbocapitalist 11-01-2012 10:13 AM

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.

shivaa 11-01-2012 10:22 AM

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!

linosaurusroot 11-01-2012 10:30 AM

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.

konsolebox 11-01-2012 10:51 AM

Quote:

Originally Posted by shivaa (Post 4819761)
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.

Thad E Ginataom 11-01-2012 12:17 PM

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)

shivaa 11-01-2012 12:48 PM

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


All times are GMT -5. The time now is 05:25 AM.