[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. |
erm, so you just read a *nix book... erm, congratulations. :confused:
|
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.
|
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. |
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. |
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. |
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? |
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. |
Quote:
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 |
grep??? As in: I grepped the cup of coffee.
awk??? As in: The awk flew over the house. or I awk my dog everyday. |
OK, just for arguments sake:
Code:
wc -l test Code:
time awk ' Code:
time grep 'php' test | awk '{ print $1 }' 1>/dev/null |
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. |