The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
daikeyang daikeyang is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 24
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
  #2 (permalink)  
Old 03-18-2009
daikeyang daikeyang is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 24
Found the answer.
  #3 (permalink)  
Old 03-19-2009
wabard wabard is offline
Registered User
  
 

Join Date: Mar 2009
Location: Adelaide, SA & Perth, WA - Australia
Posts: 25
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
  #4 (permalink)  
Old 03-19-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by wabard View Post
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
why do you need to export?
Most likely the OPs meant this:
Code:
MYVAR=`awk -F: '$1 == "testing" { print $3;exit}' filename`
  #5 (permalink)  
Old 03-20-2009
daikeyang daikeyang is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 24
Hi, guys. Thanks for replying.

And I will add answer next. Apologize for that
  #6 (permalink)  
Old 03-20-2009
wabard wabard is offline
Registered User
  
 

Join Date: Mar 2009
Location: Adelaide, SA & Perth, WA - Australia
Posts: 25
Exclamation

Quote:
Originally Posted by vgersh99 View Post
why do you need to export?
Most likely the OPs meant this:
Code:
MYVAR=`awk -F: '$1 == "testing" { print $3;exit}' filename`

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.
  #7 (permalink)  
Old 03-20-2009
jroco jroco is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 13
Exactly, It work!
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0