![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
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 |
| 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 | 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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.. |
|
||||
|
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 |
|
||||
|
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.
|
|
||||
|
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 |
|
||||
|
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 | ||
|
|