1753488 Members
5178 Online
108794 Solutions
New Discussion юеВ

print a blank field $3

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

print a blank field $3

Hi all,

I want to print a blank field from a row:

ie:

ent0 Available 0G-20 4-Port
ent1 Available 0G-20 4-Port
ent2 Available 0G-20 4-Port
ent3 Available 0G-20 4-Port
ent8 Available Virtual
ent9 Available Virtual
ent12 Available VLAN

I was thinking something along the line of:

cmd |awk 'NR>2 {if ($3 ~ /..-../) $3 = "[ ]"};{print $1,$2,$3,$4}'

ent0 Available 0G-20 4-Port
ent1 Available 0G-21 4-Port
ent2 Available 0G-30 4-Port
ent3 Available 0G-31 4-Port
ent8 Available Virtual
ent9 Available Virtual
ent12 Available VLAN

how do I assign a blank field to $3?

Thanks for any help!

Chris.
hello
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: print a blank field $3

Hi Chris:

I suppose you are looking for:

# cmd | awk 'NR>2 {if ($3 ~ /..-../) $4=$3;$3=""};{print $1,$2,$4}'

...that is you move field-3 to field-4 and empty field-3. IF there were more down-stream fields you would need to push them too.

What exactly are your trying to do?

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: print a blank field $3

Hmm,

Does 'cmd' give fixed column style output?
That gets's lost in the ITRC posting, but will also be lost to AWK as it splits fields treating multiple space as one.

But anyway...
For starters, you probably wanted NF (number of fields) > 2, not NR.
And you want to test whether the anticipated 3rd fields actually looks like one, and if it does NOT, then you need to make $4 wat is seen as $3 and add a fresh $3:

awk 'NF > 2 {if ($3 !~ /..-../) {$4 = $3; $3 = "[ ]"}};{print $1,$2,$3,$4}' tmp.txt

Be sure to attach a TEXT file with the sample input if further help is needed.

Enjoy!
Hein.


lawrenzo_1
Super Advisor

Re: print a blank field $3

ok thanks both ...

I am trying to process information from a command that has multiple fields but I am only interested in the first 4.

The I will pipe through to a while loop and run different commands depending on the device type etc:

eg

$ lstcpip -adapters

Ethernet adapters:
ent0 Available 0G-20 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent1 Available 0G-21 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent2 Available 0G-30 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent3 Available 0G-31 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent4 Available 07-20 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent5 Available 07-21 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent6 Available 07-30 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent7 Available 07-31 4-Port 10/100/1000 Base-TX PCI-X Adapter (14101103)
ent8 Available Virtual I/O Ethernet Adapter (l-lan)
ent9 Available Virtual I/O Ethernet Adapter (l-lan)
ent10 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent11 Available Shared Ethernet Adapter
ent12 Available VLAN
ent13 Available VLAN
ent14 Available VLAN
ent15 Available VLAN

lstcpip -adapters |awk 'NR>2 {print}' |while read ENT STATE HW LINE
do
if [[ -n ${HW} ]] ; then

SPEED=`lsdev -dev ${ENT} -attr media_speed`

fi

if [[ ${VLAN} -eq "VLAN" ]] ; then

VLANID=`lsdev -dev ${ENT} -attr |awk '/vlan_tag_id/ {print $2}'`

fi

and so on .....

done
~

This just the start of my code and your examples have helped.

Chris
hello
lawrenzo_1
Super Advisor

Re: print a blank field $3

although my first line has been corrected to:

lstcpip -adapters |awk 'NR>2 {if ($3 ~ /..-../) $4=$3;$3=""};{print $1,$2,$4}' |while read ENT STATE HW VLAN

...
hello
lawrenzo_1
Super Advisor

Re: print a blank field $3

although looking at it there is no need to use the awk because $3 would equal Virtual,EtherChannel,Shared etc

but the syntax will come in handy for the future

:-)
hello
lawrenzo_1
Super Advisor

Re: print a blank field $3

thanks
hello