1834481 Members
3437 Online
110067 Solutions
New Discussion

Script Help

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

Script Help

I have a file that looks like.

172.16.8.154:hostname

I want 20 spaces between the IP and hostname.
How can I do that.

UNIX IS GOOD
5 REPLIES 5
Ninad_1
Honored Contributor
Solution

Re: Script Help

Robert,

Assuming you have either only a single line or lines similar to the one you have showed, do as below.

cat filename | sed 's/:/: /g' > filename.out
[Note the 20 spaces]

Regards,
Ninad
A. Clay Stephenson
Acclaimed Contributor

Re: Script Help

awk -F ':' '{printf("%s%-20.20s%s\n",$1," ",$2}' < infile > outfile


You could also modify the other %s's to specify a fixed width output for $1 and $2.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Script Help

Yet another way to do the same would be:

# echo "172.16.8.154:hostname" | awk -F: '{
> ln=length($2)+20;printf("%s%"ln"s\n",$1,$2)}'
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi Robert:

For fun, here's another variation:

# perl -nle '($a,$b)=m/(.+):(.+)/;printf "%s%20s%s\n",$1," ",$2' filein > fileout

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi (again):

Ooops! I meant to write my last post as simply:

# perl -nle 'm/(.+):(.+)/;printf "%s%20s%s\n",$1," ",$2' filein > fileout

Regards!

...JRF...