LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   [trick] Stay away from 'grep' (https://www.linuxquestions.org/questions/slackware-14/%5Btrick%5D-stay-away-from-grep-20419/)

vfs 05-07-2002 02:17 PM

[trick] Stay away from 'grep'
 
I've read some scripts using this type of redirection:

grep 'pattern' file | awk '{ print $1 }'

Instead of using another proccess and redirection, do it all with awk:

# i like this style :-)
awk '
{
if (/pattern/) {
print $1
}
}' < file

If you know a better way to this (with awk and shell -- no perl, tcl or python, please :-), let me know!

Regards,

vfs.

acid_kewpie 05-07-2002 02:30 PM

erm, so you just read a *nix book... erm, congratulations. :confused:

trickykid 05-07-2002 02:31 PM

i like grep... nothing wrong with using it. and there are many other uses for grep instead of using it along with awk, so your thread title could be very misleading to others reading.

vfs 05-07-2002 02:58 PM

Ok, I assume the title is not very happy, but my intention was to show how unnecessary is 'grep', if combined with 'awk'.

I use 'grep' a lot... :-0

see you,

vfs.

acid_kewpie 05-07-2002 03:06 PM

ughh, ok we have one easy to read line, or your *better* version which you have as 6 lines....

personally, i'd say the awk is unnecessary, not the grep. i'd use cut instead.

vfs 05-07-2002 03:11 PM

Ok.

So, please show me the "cut" code.

And you must agree that the 'awk' + 'grep' will use 2 processes, instead of one, as in 'awk' alone or 'cut', as you said.

And you can also inline everything:

awk '{ if (/pattern/) { print $1 }}' < file

vfs.

acid_kewpie 05-07-2002 03:17 PM

surely you know how to use something as trivial as cut???

grep pattern file | cut -f 1

ok, yes you'll use two processes... erm.. and?? it's not like there is a world shortage of processes. the 0.2 second execution time is hardly gonna impact anything is it?

vfs 05-07-2002 03:18 PM

Unless you run everything in a crap machine as mine, it's ok to fill your process table.

But my system almost hang when 'updatedb' starts to run...

vfs.

tifkat 05-18-2002 10:07 PM

Quote:

Originally posted by acid_kewpie
ok, yes you'll use two processes... erm.. and?? it's not like there is a world shortage of processes. the 0.2 second execution time is hardly gonna impact anything is it?
This assumption is good if it's your personal machine and you're not offering public services. If you're running a website based on php and your php code calls this kind of thing everytime you get a hit, and you're getting a couple hundred hits a second, you're probably looking for a better way to do it :)

I like vfs's way of thinking, ie looking for a way to do it in just one process. I do think his thread title is misleading. I said "What!?!?!?" when I read it and had to find out just why such a rash thing would be said ;)


tifkat

MasterC 05-19-2002 01:32 AM

grep??? As in: I grepped the cup of coffee.

awk??? As in: The awk flew over the house. or I awk my dog everyday.

jeremy 05-19-2002 10:20 AM

OK, just for arguments sake:
Code:

wc -l test
  50932 test

Code:

time awk '
> {
> if (/php/) { print $1 }
> }' < test 1>/dev/null
0.51user 0.06system 0:00.57elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (165major+48minor)pagefaults 0swaps

and:
Code:

time grep 'php' test | awk '{ print $1 }' 1>/dev/null
0.08user 0.09system 0:00.27elapsed 60%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (121major+31minor)pagefaults 0swaps

--jeremy

jtshaw 05-29-2002 11:03 PM

Who says grep isn't more efficient at searching for things then then the none grep example? I would rather use 2 processes that take .2s then one that takes .4 and does the same job...

You have to look at the whole equation, number of process, about of time per process, amount of memory used... then you have to weight the options to see what is better for you.


All times are GMT -5. The time now is 07:21 AM.