Simple grep Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple grep Question
# 1  
Old 11-16-2008
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  
Old 11-16-2008
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  
Old 11-16-2008
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  
Old 11-16-2008
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  
Old 11-16-2008
Quote:
Originally Posted by Christoph Spohr
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  
Old 11-17-2008
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  
Old 11-24-2008
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. UNIX for Dummies Questions & Answers

Simple grep question

I hope someone can help me. I have a folder e.g. /opt/application Under that are many sub folders e.g. Folder1 Folder2 Folder3 Folder4 Folder5 Folder6 etc In some of these fodlers (not all of them) is a file called errors.log I need to run a grep that will start at... (3 Replies)
Discussion started by: gunnahafta
3 Replies

3. Shell Programming and Scripting

Simple grep script

I'm trying to write a simple script to identify every user who tried to “sudo” on the system. I have the first portion down to grep the log file grep “sudo” /var/log/secure. What I want to do is have the script identify the person just one time not every instance the user tried... (4 Replies)
Discussion started by: bouncer
4 Replies

4. UNIX for Dummies Questions & Answers

Simple grep question

This should be so easy... I want to find all the apps in /Applications that start with the lower case i (e.g. iTunes.app, iSync.app, iCal.app) They should all have the .app extension. I've tried: ls /Applications |grep -o i*.app ls /Applications/i*.app Anyhow, I just want to see what apps... (2 Replies)
Discussion started by: glev2005
2 Replies

5. UNIX for Dummies Questions & Answers

Simple newbie grep question

How come grep testfile1 won't find anything in testfile1 (even though the characters sd are there in great quantity), but grep '' testfile1 will find plenty? Do the single quotes prevent the shell from interpreting the testfile1 is interpreted as: grep *test whether or not characters sd exist*... (5 Replies)
Discussion started by: doubleminus
5 Replies

6. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

7. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies

8. Shell Programming and Scripting

Simple grep question, but I'm out of practice

Never mind, I did more research, and now am using grep -v './temp/', dumping it into a new text file, then using mv -f to make that the original file. Thanks for reading! --------------- Hi folks, I haven't done any scripting in years, and now I have a problem. Our backup tapes are filling... (0 Replies)
Discussion started by: citygov
0 Replies

9. UNIX for Dummies Questions & Answers

simple grep question

I have seen this used several times but not really sure of what it actually does. I am confused with the second grep as the argument to the first. some commands | grep -v grep | some other commands Can anyone provide an explanation? Thanks, (5 Replies)
Discussion started by: google
5 Replies

10. UNIX for Dummies Questions & Answers

Simple grep questions

Hi all, My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question