The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Simple newbie grep question doubleminus UNIX for Dummies Questions & Answers 5 04-06-2008 06:05 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM
Simple grep question, but I'm out of practice citygov Shell Programming and Scripting 0 08-02-2005 10:31 AM
simple grep question google UNIX for Dummies Questions & Answers 5 01-27-2003 11:00 PM
Simple grep questions nitin UNIX for Dummies Questions & Answers 2 10-15-2001 12:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-16-2008
elbombillo elbombillo is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
Simple grep Question

I tried searching for answers but didn't find any.

When I grep a file results read

4.2.2.2
4.4.4.2
4.5.6.7

But I just want to select each result individually. For Example I want to be able to say
variable1="first grep result"
variable2="second grep result"
variable3="third grep result"

Thanks in advance.

Last edited by elbombillo; 11-16-2008 at 01:00 PM..
  #2 (permalink)  
Old 11-16-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 240
Hi there,

It would help a lot to analyse Your problem if:
1: You gave an actual sample of the source, ie the "file" content
2. What Your grep or other command sequence looks like right now
3. You give an example of expected output, that is for example, is this variable assignment part of a some script to be used elsewhere?

Otherwise it's only guessing.

/Lakris
  #3 (permalink)  
Old 11-16-2008
elbombillo elbombillo is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
head /etc/resolv.conf |grep nameserver| awk '{print $2}'

I get:
68.28.58.92
68.28.50.91


but how can I create two variables
dns1=68.28.58.92
dns2=68.28.50.91

Thanks again.
  #4 (permalink)  
Old 11-16-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

under bash you can try

dns=( $(awk '/nameserver/{print $2}' /etc/resolv.conf ) )

This will give you the second field of all lines matching nameserver read into an array. You can access the data with: echo ${dns[0]}, ${dns[1]} etc.

Kind regards

Chris
  #5 (permalink)  
Old 11-16-2008
elbombillo elbombillo is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
Quote:
Originally Posted by Christoph Spohr View Post
Hi,

under bash you can try

dns=( $(awk '/nameserver/{print $2}' /etc/resolv.conf ) )

This will give you the second field of all lines matching nameserver read into an array. You can access the data with: echo ${dns[0]}, ${dns[1]} etc.

Kind regards

Chris
Thanks that works but it doesn't like it if the file is empty. I guess I can do my standard grep nameservers | wc -l to check if there is an entry first.
  #6 (permalink)  
Old 11-17-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

keep it simple. A little test is enough:

[[ -s /etc/resolv.conf ]] && dns=( $(awk '/nameserver/{print $2}' /etc/resolv.conf ) )

This means:

if /etc/resolv.conf exists and is not empty, then and only then execute the following command.

Kind regards

Chris
  #7 (permalink)  
Old 11-24-2008
elbombillo elbombillo is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 48
Thanks, looks like it works. Can you explain how this works? I just like to understand it and use it in the future. Also does this work if need to run a command and grab all the output from it....for example running the command /usr/sbin/networksetup -listallnetworkservices on a leopard machine I get all the network services. How would I use this command to only print anyone that contains with "Ethernet".

Since I'm used to using grep I run /usr/sbin/network -listallnetworkservices |grep Ethernet, but I get 3 responses and I need to select each on individually. Can you help?

Thanks again.

Last edited by elbombillo; 11-24-2008 at 05:23 PM..
Sponsored Links
Closed Thread

Bookmarks

Tags
grep or

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 08:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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