Trap grep output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap grep output
# 1  
Old 08-21-2008
Trap grep output

We have a script that greps a file for an IP address. Once the address is found I'd like to trap the result and use it later in the script. For example I have a file called ips.txt, and it contains:

USA
http://123.123.123.123
CAN
http://210.210.210.210

When I grep for CAN, it returns CAN http://210.210.210.210. To get just the address, my grep statement is: grep CAN -A 1 ips.txt | grep http

If I could temporarily save the output of the grep command, I can use the http://210.210.210.210 address later in the script to connect to a site. Any ideas?
# 2  
Old 08-21-2008
can't you store it in some variable??
# 3  
Old 08-21-2008
That's what I'd like to do, but I can't get my head around it, I've tried...

var='grep CAN -A 1 ips.txt | grep http
echo $var

Which of course returns: grep CAN -A 1 ips.txt | grep http

But this is not what I want, I want the results.
# 4  
Old 08-21-2008
use back quotes `command`
# 5  
Old 08-21-2008
Figured it out, I was using the single quote instead of the backtick
# 6  
Old 08-21-2008
are you new to unix world??
# 7  
Old 08-21-2008
Pretty obvious I guess. I am actually a storage engineer, but I need to automate some cli scripts that will add storage to a Hitachi array.

BTW: It turns out I have to get this to work in Perl. I do the following and it retruns what I want, but it looks like it must have a lot of whitesapce after it, so I tried to strip it off with...

CODE:
#!/usr/bin/perl
print `clear` , "\n";
print "Site: ";
chomp ($site= <>);
$ asite=`grep $site -A 1 ips.txt | grep http`;
$ nw=`echo $asite | sed 's/^[ ]*//'`;
print "$nw\n";

EXEC CODE AND RESULTS
testsys> ./test.pl

Site: USA
sh: -c: line 1: syntax error near unexpected token `|'
sh: -c: line 1: ` | sed 's/^[ ]*//''
http://123.123.123.123

testsys>

I know there is a lot of space after the results, as I tested this by adding a couple of other vars and it moved them to the next line. Any ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep output

Hi. I have a command line that will generate exif data from .jpg files... So to get the Camera Model I use: find -iname "*.jpg" -print0 | xargs -0 exiftool -a | grep "Camera Model" But I want the file name AND the camera model... How to I get the grep to give me TWO bits of information from... (2 Replies)
Discussion started by: TuftyDave
2 Replies

2. UNIX for Dummies Questions & Answers

How to have the output in grep?

Hi guys - I am trying to have a certain file display information horizontally divided by a pipe. for example file name: foo.dat has information like this: name1 name2 name3 namenamename4 namene5 I would like it to display like this: name1|name2|name3|namenamename4|namene5 ... (6 Replies)
Discussion started by: DallasT
6 Replies

3. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

4. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

5. Shell Programming and Scripting

grep output

From below mentioned line,i have to display output as last word only ie:arch_1_105366_720809746.dbf -rw-r----- 1 oracle dba 98887680 02 Mar 12:01 arch_1_105366_720809746.dbf Please .. (5 Replies)
Discussion started by: Sanal
5 Replies

6. UNIX for Advanced & Expert Users

How to prevent grep command from throwing a system trap if No match is found.

Hi How to prevent grep command from throwing a system trap(or returning error status) if No match is found in the specified file(s) ? Consider this simple shell script: #!/usr/bin/ksh trap 'STATUS=$?;set +x;echo;echo error $STATUS at line nb $LINENO executing :\ `sed -n... (2 Replies)
Discussion started by: cool.aquarian
2 Replies

7. Shell Programming and Scripting

GREP with contain output

How can I perform a grep and it will give me a contain word (maintenance) instead of a string or the whole line of info. grep with exclusive output? or is there a CONTAIN with IF statement ? I have a file call yast2_vhost.conf and it contain multiple entries call maintenance.html... (2 Replies)
Discussion started by: yoom@hostwebase
2 Replies

8. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

output of grep

hi, why the following is giving entire /etc/passwd file as output even when it does not contain ^ or $ . grep ' ' /etc/passwd whereas the following is not giving any output grep ' ^$ ' /etc/passwd (3 Replies)
Discussion started by: useless79
3 Replies

10. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies
Login or Register to Ask a Question