Operating System - HP-UX
1753466 Members
5199 Online
108794 Solutions
New Discussion юеВ

Re: Can awk help in the below situation?

 
SOLVED
Go to solution
Cynthia_5
New Member

Can awk help in the below situation?

Below is the output statistics from a snmpwalk script file.

3/1
3/2
3/3
up
down
up
2 days
5 days
10 day

May I know how I can produce the above results to be shown as below? Please advise, thank you.

port status time
3/1 up 2 days
3/2 down 5 days
3/2 up 10 days

9 REPLIES 9
Steven Sim Kok Leong
Honored Contributor

Re: Can awk help in the below situation?

Hi,

Here's one lazy and quick solution, assuming that you don't mind the x-axis and y-axis being swapped:

# echo port `cat datafile | head -3` ; echo status `cat datafile | head -6 | tail -3` ; echo time `cat datafile | head -9 | tail -3`
port 3/1 3/2 3/3
status up down up
time 2 days 5 days 10 day

Hope this helps. Regards.

Steven Sim Kok Leong
V. V. Ravi Kumar_1
Respected Contributor
Solution

Re: Can awk help in the below situation?

hi,

cat <script file>|head -3 > testfile1
cat <script file>|head -6|tail -3 >testfile2
cat <script file>|tail -3 >testfile3

paste testfile1 testfile2 testfile3

regds
Never Say No
Cynthia_5
New Member

Re: Can awk help in the below situation?

Hi,
I think the solution will be suitable if there is only one single set of data and also provided I know the number of ports.

However, in the actual scenarion, the script will capture about 100 over sets of similar data as above, and each box has random number of port. In above example, it only shows 3 rows (port 3/1,3/2,3/3), but in real case, I may have one box having 48 ports and another box having 96 ports.

I hope this can help to clarify the situation.

Box 1
======
3/1
3/2
3/3
up
up
up
3 days
4 days
4 days

Box 2
=====
1/1
1/2
1/3
1/4
1/5
1/6
..........
..........
Patrick Chim
Trusted Contributor

Re: Can awk help in the below situation?

Hi,

Can you try the following script to generate the results ??

=== BEGIN ===
#!/bin/ksh

i=1
num=`wc -l yourfile.txt | awk '{print $1}`

cat yourfile.txt | while read a
do
name[$i]=$a
let i=$i+1
done

j=1
while [ $j -le `expr $num / 3` ]
do
echo ${name[$j]} ${name[$j+3]} ${name[$j+6]}
let j=$j+1
done
=== END ===

If you have to group 4 rows, just replace 3,6 with 4,8 and add one more variable ${name[$j+12]}.

Regards,
Patrick
Patrick Chim
Trusted Contributor

Re: Can awk help in the below situation?

Hi,

Sorry forget that "2 days" is the whole line.

Just change the script to the following,

i=1
num=`wc -l yourfile.txt | awk '{print $1}`

cat yourfile.txt | while read LINE
do
name[$i]=$LINE
let i=$i+1
done

j=1
while [ $j -le `expr $num / 4` ]
do
echo ${name[$j]} ${name[$j+3]} ${name[$j+6]} ${name[$j+9]}
let j=$j+1
done

And '4' is the value to group how many data in a row of the result.
If you have 4 sets of data, just replace 3,6,9 with 4,8,12

Regards,
Patrick
Steven Sim Kok Leong
Honored Contributor

Re: Can awk help in the below situation?

Hi,

>> Below is the output statistics from a snmpwalk script file.

I believe it will be easier for you to work directly on the snmpwalk script itself to output appropriate than to work around the existing output.

The other thing is perhaps it might be easier to use snmpget and format each snmpget record individually than using snmpwalk and format the entire table.

Hope this helps. Regards.

Steven Sim Kok Leong
H.Merijn Brand (procura
Honored Contributor

Re: Can awk help in the below situation?

As long as all groups are of equal length, this'll work

# perl -ne 'm:^\d+/:&&push@x,$_ or/^\d+ day/&&push@y,$_ or push@z,$_;}END{chomp@x;chomp@z;$,="\t";print$_,shift(@z),shift@y for@x' xx
3/1 up 2 days
3/2 down 5 days
3/3 up 10 day
# cat xx
3/1
3/2
3/3
up
down
up
2 days
5 days
10 day
#
Enjoy, Have FUN! H.Merijn
Joseph A Benaiah_1
Regular Advisor

Re: Can awk help in the below situation?

Cynthia,

Here is a small awk script that should do what you want:

#!/usr/bin/sh

awk '

BEGIN { print "Port Status Time" }

{ buf[NR] = $0 }

END { size = NR
# Work out how many ports we have
no_ports = 0
for ( i = 1 ; i <= size ; i++ )
{
if ( buf[i] == "up" || buf[i] == "down" )
{
break
}
++no_ports
}
for ( i = 1 ; i <= no_ports ; i++ )
{
print buf[i], buf[i+no_ports], buf[i+(2*no_ports)]
}
}' /tmp/snmpwalk

It will work N number of ports as long as the format of the smnpwalk file remains as in your posting.

Cheers,

Joseph
Cynthia_5
New Member

Re: Can awk help in the below situation?

Hi all,

Thanks for all the contributions.

I finally managed to get the output by modifying the script file and incorporating with the commands recommended. Using the paste as suggested by kumar can produce the required results.

Cheers,
Cynthia