3

I have a huge file with about 12300 lines that look something similar to this.

001.domain.com=001.somedomain.com:10001
002.domain.com=002.somedomain.com:10002
003.domain.com=003.somedomain.com:10003

I want the file to look like this when it is done

001.domain.com=IP_Address_of_001.somedomain.com:10001
002.domain.com=IP_Address_of_002.somedomain.com:10002
003.domain.com=IP_Address_of_003.somedomain.com:10003

So basically I need to find and replace the hostname after the = signs with the ip address.

If someone can point me in the right direction, I would appreciate it.

  • How do we know what is the IP address to use for each domain? – Julie Pelletier Jan 16 '17 at 20:19
  • sed can easily process the substitutions you need, the question here is what is the source of the IP address? DNS or a file? – Bruno9779 Jan 16 '17 at 20:28
7

This uses sed to extract the hostname, then uses dig to get its IP, then uses sed again for the replace. It outputs the replacements to a new file:

$ while read line; do 
    hostname=$(echo "$line" | sed "s/.*=\(.*\):.*/\1/g")
    ip=$(dig +short $hostname | head -n1)
    echo "$line" | sed "s/\(.*=\).*\(:.*\)/\1${ip}\2/g"
done < file.txt > new_file.txt
0

A similar option by using host instead of dig:

$ cat filename.txt | while read line; do 
IP=$(host $(echo $line | cut -d'=' -f1) | grep -m1 "has address" | rev | cut -d' ' -f1 | rev)
echo $line | sed "s/\(.*=\).*\(:.*\)/\1${IP}\2/g"
done

The parsing which takes the hostname may be substituted by awk:

$ cat filename.txt | while read line; do 
IP=$(host $(echo $line | cut -d= -f1) | awk -F'[ ]' '/has address/ {print $4;exit;}')
echo $line | sed "s/\(.*=\).*\(:.*\)/\1${IP}\2/g"
done
0

With awk:

awk -F'[:=]' '{ cmd="host "$2"| cut -d\" \" -f4"; cmd |getline $2; 
    print $1"="$2":"$3; close(cmd) }' infile

We set a shell command host $2"| cut -d\" \" -f4 to a variable called cmd. Then with cmd |getline $2 the awk execute the command saves the result into second field value itself from the result of cmd we open it Using getline from a Pipe, next we print the columns, then we need to close the command we opened, and we used close(cmd) here.

Or replace host with dig +short will make the command short:

awk -F'[:=]' '{ cmd="dig +short "$2; cmd |getline $2; 
    print $1"="$2":"$3; close(cmd) }' infile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.