LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 05-07-2002, 02:17 PM   #1
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
[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.
 
Old 05-07-2002, 02:30 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
erm, so you just read a *nix book... erm, congratulations.
 
Old 05-07-2002, 02:31 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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.

Last edited by trickykid; 05-07-2002 at 02:32 PM.
 
Old 05-07-2002, 02:58 PM   #4
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Original Poster
Rep: Reputation: 30
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.
 
Old 05-07-2002, 03:06 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 05-07-2002, 03:11 PM   #6
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Original Poster
Rep: Reputation: 30
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.
 
Old 05-07-2002, 03:17 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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?
 
Old 05-07-2002, 03:18 PM   #8
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Original Poster
Rep: Reputation: 30
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.
 
Old 05-18-2002, 10:07 PM   #9
tifkat
Member
 
Registered: May 2002
Location: Perth, Australia
Distribution: Slack, RH, *BSD, Tru64/DECUnix/OSF1/Ultrix, Solaris/SunOS...
Posts: 40

Rep: Reputation: 16
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
 
Old 05-19-2002, 01:32 AM   #10
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

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

awk??? As in: The awk flew over the house. or I awk my dog everyday.
 
Old 05-19-2002, 10:20 AM   #11
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602

Rep: Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084Reputation: 4084
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
 
Old 05-29-2002, 11:03 PM   #12
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
  


Reply



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 ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
Trick Yum volvogga Linux - Newbie 4 08-18-2005 05:53 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration