SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old October 27th, 2004, 02:56 PM
yogi27 yogi27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Dallas
Posts: 50 yogi27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
help needed with grep

Hi,

My file contains the following info:

0x00201800 0x172165 0x002bc3 32 0 0 0
0x00211800 0x172179 0x002bc3 32 0 0 0
0x00301800 0x972165 0x0025c3 48 0 0 0
0x00311800 0x972179 0x0025c3 48 0 0 0
0x00381800 0xd72165 0x0077c2 56 0 1 0
0x00391800 0xd72179 0x0077c2 56 0 1 0


When i do:
grep -w 32 <filename> | awk '{print $2}'

I get 2 values: 0x172165 0x172179

1. How can I assign these 2 values to 2 different variables?
2. Else can I grep one line at a time?

Thanks,
Yogesh.

Reply With Quote
  #2  
Old October 28th, 2004, 10:03 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
I don't know what you mean by assigning them to two different variables or processing grep one line at a time...

If you want to process your result set you could do a for loop:
Code:
for X in `grep -w 32 textfile |awk '{print $2}'`
do
  echo Processing value $X
done 
Processing value 0x172165
Processing value 0x172179


You could process the text using awk:
Code:
awk '{
         if($4==32)
           {print "Processing line "$0}
         else
           {print "Ignoring line   "$0}
}' textfile
Processing line 0x00201800 0x172165 0x002bc3 32 0 0 0
Processing line 0x00211800 0x172179 0x002bc3 32 0 0 0
Ignoring line   0x00301800 0x972165 0x0025c3 48 0 0 0
Ignoring line   0x00311800 0x972179 0x0025c3 48 0 0 0
Ignoring line   0x00381800 0xd72165 0x0077c2 56 0 1 0
Ignoring line   0x00391800 0xd72179 0x0077c2 56 0 1 0


...and you can parse the file into a for loop:
Code:
cat textfile |while read VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7
do
  if [[ $VAR4 -eq 32 ]]; then 
    echo Processing line $VAR1 $VAR2 $VAR3 $VAR4 $VAR5 $VAR6 $VAR7
  else
    echo Ignoring line $VAR1 $VAR2 $VAR3 $VAR4 $VAR5 $VAR6 $VAR7
  fi
done
Processing line 0x00201800 0x172165 0x002bc3 32 0 0 0
Processing line 0x00211800 0x172179 0x002bc3 32 0 0 0
Ignoring line 0x00301800 0x972165 0x0025c3 48 0 0 0
Ignoring line 0x00311800 0x972179 0x0025c3 48 0 0 0
Ignoring line 0x00381800 0xd72165 0x0077c2 56 0 1 0
Ignoring line 0x00391800 0xd72179 0x0077c2 56 0 1 0

Reply With Quote
  #3  
Old November 2nd, 2004, 04:57 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
i know: unix piping is magnetic += fantastic

andy's first is fast: awk has less to do, the job is made by grep,
i prefer the second, even if less performant it's more intelligent,
the third one is for greens and {dos,win}-experts.

Q: in this case are piping needed ?
A: NO
assumed your line:
>>> 0x00201800 0x172165 0x002bc3 32 0 0 0 <<<

sed -n 's/[ST]*\(.*\)[ST]*[ST]\(.*\).*[ST]*[ST]32[ST]*[ST].*/\2/p' filename >output

substitute ST by a SPACE followed by a TAB character
should do the job.
(untested, at the moment my *nix is down)

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > help needed with grep


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway