![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using GREP to extract variable following a string | modey3 | UNIX for Dummies Questions & Answers | 10 | 02-10-2009 06:00 AM |
| using sed to conditionally extract stanzas of a file based on a search string | aitayemi | Shell Programming and Scripting | 0 | 11-25-2008 06:16 PM |
| How to extract variable number from a String | vitesse | Shell Programming and Scripting | 19 | 09-22-2008 10:03 AM |
| appending string to text file based on search string | malaymaru | Shell Programming and Scripting | 1 | 06-09-2006 09:53 AM |
| extract from string variable into new variables | Sniper Pixie | UNIX for Dummies Questions & Answers | 2 | 03-03-2006 10:49 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Search for string in a file and extract another string to a variable
Hi, guys. I have one question:
I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ... I want to search for string "testing", then extract group id (2001) on the same line and assign it to a variable. Can anybody help me with me question? Thank you very much for your time in advance -K.D |
|
||||
|
Please include your solution in the future to help others out...
FYI... the following should work. Note that the -F parameter is to specify a field separator, in this case, ":". In a Bourne shell Code:
MYVAR=`awk -F: '/testing/ { print $3 }' filename`
export MYVAR
|
|
||||
|
Quote:
Meh... I'm too old school.. I always export for completeness... not necessary in this case, since all variables are internal to the script itself... Good catch and good point. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|