Click to See Complete Forum and Search --> : awk/printf help?


AlphaFemale
03-27-2002, 01:34 PM
Im writing a much larger script, but I need some help on some awk/prtinf (as I dont use them that often).

Basically part of the script is to login to a machine and grab the ethernet device, which is either elxl0 or iprb0 (this is solaris)...

I have thus, so far:

rsh computer_name /usr/sbin/ifconfig -a | awk '/elxl/ || /iprb/ {printf $1}'

This will return:
elxl0:
or
iprb0:


This is almost perfect, except I need to chop off the : charactor. I dont want to limit the field size or anything in case I ever add differnt type of ethernet devices. I know its really simple, but for the life of me I cannot remember what I need to pass to printf to cut that ':' off.

[ 27 March 2002: Message edited by: AlphaFemale ]

bwkaz
03-27-2002, 03:29 PM
You could just pipe the whole thing to sed, like this:

rsh computer_name /usr/sbin/ifconfig -a | awk '/elxl/ {printf $1} /iprb/ {printf $1}' | sed -e 's/://'

That way sed will search each line for the first colon and remove it from the output. If there is a way to do it with printf in awk, I don't know it; this is how I'd do this if I was writing the script (but then again, no one said it was anywhere near the best way to write it, either).

What I do know is that it will do what you want, assuming only one colon per line.

[ 27 March 2002: Message edited by: bwkaz ]

AlphaFemale
03-27-2002, 03:44 PM
I dont like pipeing if I dont have too.

I was going to do:
rsh computer_name /usr/sbin/ifconfig -a | awk '/elxl/ || /iprb/ {printf $1}' | cut -d : -f 1

But I know that "printf" should be able to do what I want it to without pipeing.

Ive tried:
printf $1"\b"
this sort of works, but not properlly.


p.s. This script Im writing is pretty big, but its ideal for it to rely on as little as possible 'outside' programs (like sed) as possible. Which is another reason I dont like piping... Im already relying on awk/printf, I dont want to rely on much anything else,

[ 27 March 2002: Message edited by: AlphaFemale ]

AlphaFemale
03-27-2002, 07:59 PM
This produces the result I want :)

rsh computer_name /usr/sbin/ifconfig -a | awk -F : '/elxl/ || /iprb/ {printf $1}'