grep reverses # output, but not text?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep reverses # output, but not text?
# 1  
Old 04-21-2010
grep reverses # output, but not text?

I have a working script to find out the IP of a hostname like this:
Code:
#!/bin/bash
h="www.google.com"
echo $h
j=`host $h | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
echo "ip: $j"

output
Code:
ip: 74.125.19.99

the strange thing is if I use an IP instead of a hostname for $h, it inverts the order of the numbers on the output:
Code:
h="74.125.19.99"
echo $h
j=`host $h | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
echo "ip: $j"

outputs:
Code:
ip: 99.19.125.74

why? Also, is there a better way to do this?
# 2  
Old 04-21-2010
Quote:
Originally Posted by unclecameron
I have a working script to find out the IP of a hostname like this:
Code:
#!/bin/bash
h="www.google.com"
echo $h
j=`host $h | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
echo "ip: $j"

output
Code:
ip: 74.125.19.99

the strange thing is if I use an IP instead of a hostname for $h, it inverts the order of the numbers on the output:
Code:
h="74.125.19.99"
echo $h
j=`host $h | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
echo "ip: $j"

outputs:
Code:
ip: 99.19.125.74

why? ...
Seems like the IP is inverted in the output of the host command itself. grep is simply extracting the general pattern it matches.

If you run the following two commands:

Code:
host "www.google.com"
host "<one_of_the_ip_addresses_that_is_returned_by_the_command_above>"

then you'll see reversed IP addresses. I have no idea why that's the case.

tyler_durden
# 3  
Old 04-21-2010
Read the host man page. It should tell you that when you give it an ip address it does a reverse lookup.

http://en.wikipedia.org/wiki/Reverse_DNS_lookup

Last edited by alister; 04-21-2010 at 11:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

3. 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

4. Shell Programming and Scripting

grep output

hi i am running grep -c on a bunch of files some of which may be non existent. The output i get is in the same order as the files grep checks and this is what i want as i need to copy the output to a spreadsheet. However when i tried to pipe the output to sed to strip away the filenames and just... (5 Replies)
Discussion started by: chronics
5 Replies

5. Shell Programming and Scripting

grep output of a text file

Hi. I have a unix script 'if' condition that greps through a file and searches for the strings ERROR and WARNING. if egrep -q 'ERROR|WARNING' myfile.txt; then When this 'if' condition is true then I want to be able to capture 10 lines before the string and 10 lines after the string. So... (6 Replies)
Discussion started by: buechler66
6 Replies

6. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

7. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

8. Shell Programming and Scripting

Reading from a text file then /grep/output

I have a list of words that I want to grep in many files to see which ones have it and which ones dont. in the text file I have all the words listed line by line, ex: list.txt: check try this word1 word2 open space list .. I want to grep each line one by one. like I want it to... (7 Replies)
Discussion started by: s3rro
7 Replies

9. 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

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question