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
wabard
3
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
MYVAR=`awk -F: '/testing/ { print $3 }' filename`
export MYVAR
why do you need to export?
Most likely the OPs meant this:
MYVAR=`awk -F: '$1 == "testing" { print $3;exit}' filename`
Hi, guys. Thanks for replying.
And I will add answer next. Apologize for that:)
wabard
7
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.