|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
yes, the better grep is called sed.
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
I think I was doing some error. The Id value is stored and I can use it for running other scripts.
|
|
#8
|
|||
|
|||
|
yogi27
your ( ) regexpCode:
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) ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Help needed with grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|