I have a file called test.txt. Inside this file is the following:
tcenh100.telkom.co.za
100.200.300.400
tcenh101.telkom.co.za
500.600.700.800
I want to take out the new lines and spaces, then I want to put the ip address of the host name next to the host name on the same line, as soon as this is done, I want to switch around the ip address and host name with a space between them. The file must look as follows after script is done running:
Dang! WIntellect beat me to the punch. Oh well, I'm gonna post my sed script anyway...
#! /usr/bin/sed -f
#
N ;# suck in a second line
N ;# suck in a third line
s/\n/ /g ;# change new-line to space
s/ */ /g ;# change many spaces to one space
s/\([^ ]*\) \([^ ]*\)/\2 \1 / ;# swap the fields
But it's about the same thing as the awk version, so an awk user can work it out from there.
How about a Bourne shell version, using expr. I have no idea how portable expr stuff is.
#!/bin/sh
while read word rest ; do
if [ `expr "$word" : '.*\.telkom\.'` -gt 0 ] ; then
hostname=$word
elif [ `expr "$word" : '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'` -gt 0 ] ; then
echo "$word $hostname"
fi
done
For each line starting with an alphabetic character, store it in variable d.
For each line starting with a numeric character, print it and varialble d.