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:
  #1  
Old October 18th, 2004, 10:41 AM
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,

I have this table:

Bidirectional Allocated Connections
AtmId SpId HashId Port Vpi Vci CoreConnId
0x0024 2 0x0000 00 009 00005 0x2002
0x0023 2 0x0000 00 032 00000 0x2001
0x0023 3 0x0000 00 000 00100 0x2000
0x0022 3 0x0000 00 000 00005 0x2003

I want to get the CoreConnId i.e. the last value in a specific row. To get the row that I want, I do grep -w 00005 file1 > file2 to get:
0x0024 2 0x0000 00 009 00005 0x2002
0x0022 3 0x0000 00 000 00005 0x2003

Then, I do grep -w 2 file2 to get:
0x0024 2 0x0000 00 009 00005 0x2002

My questions:
1. How can I get the final row from a single grep command?
2. Now, how do I get the last value now i.e. 0x2002. Can I do the whole thing in a single command?

Thanks,
Yogesh.

Reply With Quote
  #2  
Old October 18th, 2004, 11:20 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 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: 5 Days 8 h 44 m 33 sec
Reputation Power: 9
yes, the better grep is called sed.

Reply With Quote
  #3  
Old October 18th, 2004, 11:33 AM
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
I got the answer to my first question. I did a grep -w 2 <filename> | grep -w 00005 and got the line I wanted.

Now, how do I get the last field. What should I use: sed, awk??? Please help me with the command.

Thanks,
Yogesh.

Reply With Quote
  #4  
Old October 18th, 2004, 12:04 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
Guggach,

I used awk to get the last field:
grep -w 2 msbout | grep -w 00005 | awk '{print $7}'

So, finally I got the value that I wanted. Now, I want to store this value to a variable. I am writing a ksh script and I want this value to be stored in a variable for future use in the script. How can I save the output of the above command as a variable?

Thanks,
Yogesh.

Reply With Quote
  #5  
Old October 18th, 2004, 01:18 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
Hi,

I figured out how to save the output to a variable:
Id=`grep -w 2 msbout | grep -w 00005 | awk '{print $7}'`

The output of my script is running on another machine. The above Id is obtained in one mode and then I switch modes wherein I have to use this Id. But I believe the value doesn't stay.

Do I have to save this value to a file. Is there a better way to do it?

Thanks,
Yogesh.

Reply With Quote
  #6  
Old October 18th, 2004, 01:18 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
Hi,

I figured out how to save the output to a variable:
Id=`grep -w 2 <filename> | grep -w 00005 | awk '{print $7}'`

The output of my script is running on another machine. The above Id is obtained in one mode and then I switch modes wherein I have to use this Id. But I believe the value doesn't stay.

Do I have to save this value to a file. Is there a better way to do it?

Thanks,
Yogesh.

Reply With Quote
  #7  
Old October 18th, 2004, 05:14 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
I think I was doing some error. The Id value is stored and I can use it for running other scripts.

Reply With Quote
  #8  
Old October 19th, 2004, 07:43 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 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: 5 Days 8 h 44 m 33 sec
Reputation Power: 9
yogi27

your () regexp
Code:
grep -w 2 <filename> | grep -w 00005

prints:
...... 2 .... 00005
...... 2 .... 00005 .... abc
...... 00005 ..... 2
...... 00005 ..... abc .. 2 ...qqq
but i see, like a lot other, you are happy because it's working
in your example, and only there

assumed '2' appears before '00005' in the file
try just for fun
sed -n '/.*\<2\>.*\<00005\>.*/p' input

feel free to change the statement to print EXACTLY what
you need, sed can it (better as grep does)

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
Stay green...Green IT