Trying to get rid of a duplicate output line...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to get rid of a duplicate output line...
# 1  
Old 04-22-2013
Trying to get rid of a duplicate output line...

Hi folks,

I'm trying to work on a script that will grab a router interface report and generate the numbers of "in use" and "un-used" ports per device. Right now, I've got a cut down of the report as follows:

Code:
   sing /usr/apps/siteName/etc/DCAFT-9K.cmds for send text
   Connecting using ssh to [XXX removed data XXX]as user [XXX removed data XXX]
   spawn ssh [XXX removed data XXX]@[XXX removed data XXX]
   Using Process ID: 28512
   [XXX removed data XXX]@[XXX removed data XXX]'s password:

   RP/0/RSP0/CPU0:CHRXNCLH92A-P-CI-9010-02#
   SUCCESS: Logged in and ready to send commands

   show interfaces summary
   Thu Mar 21 14:16:26.517 EDT
   Interface Type          Total    UP       Down     Admin Down
   --------------          -----    --       ----     ----------
   ALL TYPES               955      820      72       63
   --------------
   IFT_BVI                 7        7        0        0
   IFT_ETHERBUNDLE         21       19       0        2
   IFT_VLAN_SUBIF          733      698      15       20
   IFT_GETHERNET           120      73       17       30
   IFT_LOOPBACK            5        5        0        0
   IFT_ETHERNET            4        2        0        2
   IFT_NULL                1        1        0        0
   IFT_TENGETHERNET        64       15       40       9

   RP/0/RSP0/CPU0:CHRXNCLH92A-P-CI-9010-02#
   --SUCCESS for normal login prompt--

   *** Finished with script

My script takes that data and outputs this:
Code:
   Device Name:  CHRXNCLH92A-P-CI-9010-02

   Port type:       IFT_ETHERBUNDLE :
    21     Capacity
    19     Ports In Use
     2      Ports Not in Use
   Port type:       IFT_GETHERNET :
    120    Capacity
    90     Ports In Use
    30     Ports Not in Use
   Port type:       IFT_TENGETHERNET :
    64     Capacity
    55     Ports In Use
    9      Ports Not in Use

   Device Name:  CHRXNCLH92A-P-CI-9010-02

What I want to get rid of is the second occurrence of:
Device Name: CHRXNCLH92A-P-CI-9010-02

Here is my code:
Code:
   #!/bin/ksh
   ###########################################
   # Parser.sh version 0.1
   # Author: Marc G
   #
   ###########################################

   # read file - Hard coded.  File to be read can be determined via logic later
   file="capturesummary.txt"
   while read line
   do
   # parse device name
     echo $line | grep ^RP | awk -F: '{print $2}'| awk -F# '{print "\nDevice Name: ",$1,"\n"}'
  # Parse Data
  #     total is parsed as "Capacity", Up and Down are added into "Ports in Use" and 
     administratively Down is "Ports Not In use"

echo $line | egrep -i    "IFT_ETHERBUNDLE|IFT_GETHERNET|IFT_TENGETHERNET" | awk '{print "Port type:\t",$1,":\n",$2,"\tCapacity\n",$3 + $4,"\tPorts In U
se\n",$5,"\tPorts Not in Use"}'

done <"$file"

# end

Can anyone give me a method to prevent the second printing of the device name?

Thanks!
Marc
# 2  
Old 04-22-2013
You could do the whole thing simply using an awk program:
Code:
awk '
        /^RP/ {
                if ( device && port_type )
                {
                        print "Device Name: ", device
                        print "Port Type: ", port_type
                        print capacity, "Capacity"
                        print port_in_u, "Port in use"
                        print port_nt_u, "Port not in use"
                }
                device = port_type = capacity = port_in_u = port_nt_u = ""
                gsub (/.*:|#/, X)
                device = $0
        }
        /IFT_ETHERBUNDLE/ {
                port_type = $1
                capacity  = $2
                port_in_u = $3 + $4
                port_nt_u = $5
        }
' OFS='\t' file

You can add rules for IFT_GETHERNET & IFT_TENGETHERNET. I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-22-2013
Quote:
Originally Posted by Marc G
Hi folks,

I'm trying to work on a script that will grab a router interface report and generate the numbers of "in use" and "un-used" ports per device. Right now, I've got a cut down of the report as follows:

Code:
   sing /usr/apps/siteName/etc/DCAFT-9K.cmds for send text
   Connecting using ssh to [XXX removed data XXX]as user [XXX removed data XXX]
   spawn ssh [XXX removed data XXX]@[XXX removed data XXX]
   Using Process ID: 28512
   [XXX removed data XXX]@[XXX removed data XXX]'s password:

   RP/0/RSP0/CPU0:CHRXNCLH92A-P-CI-9010-02#
   SUCCESS: Logged in and ready to send commands

   show interfaces summary
   Thu Mar 21 14:16:26.517 EDT
   Interface Type          Total    UP       Down     Admin Down
   --------------          -----    --       ----     ----------
   ALL TYPES               955      820      72       63
   --------------
   IFT_BVI                 7        7        0        0
   IFT_ETHERBUNDLE         21       19       0        2
   IFT_VLAN_SUBIF          733      698      15       20
   IFT_GETHERNET           120      73       17       30
   IFT_LOOPBACK            5        5        0        0
   IFT_ETHERNET            4        2        0        2
   IFT_NULL                1        1        0        0
   IFT_TENGETHERNET        64       15       40       9

   RP/0/RSP0/CPU0:CHRXNCLH92A-P-CI-9010-02#
   --SUCCESS for normal login prompt--

   *** Finished with script

My script takes that data and outputs this:
Code:
   Device Name:  CHRXNCLH92A-P-CI-9010-02

   Port type:       IFT_ETHERBUNDLE :
    21     Capacity
    19     Ports In Use
     2      Ports Not in Use
   Port type:       IFT_GETHERNET :
    120    Capacity
    90     Ports In Use
    30     Ports Not in Use
   Port type:       IFT_TENGETHERNET :
    64     Capacity
    55     Ports In Use
    9      Ports Not in Use

   Device Name:  CHRXNCLH92A-P-CI-9010-02

What I want to get rid of is the second occurrence of:
Device Name: CHRXNCLH92A-P-CI-9010-02

Here is my code:
Code:
   #!/bin/ksh
   ###########################################
   # Parser.sh version 0.1
   # Author: Marc G
   #
   ###########################################

   # read file - Hard coded.  File to be read can be determined via logic later
   file="capturesummary.txt"
   while read line
   do
   # parse device name
     echo $line | grep ^RP | awk -F: '{print $2}'| awk -F# '{print "\nDevice Name: ",$1,"\n"}'
  # Parse Data
  #     total is parsed as "Capacity", Up and Down are added into "Ports in Use" and 
     administratively Down is "Ports Not In use"

echo $line | egrep -i    "IFT_ETHERBUNDLE|IFT_GETHERNET|IFT_TENGETHERNET" | awk '{print "Port type:\t",$1,":\n",$2,"\tCapacity\n",$3 + $4,"\tPorts In U
se\n",$5,"\tPorts Not in Use"}'

done <"$file"

# end

Can anyone give me a method to prevent the second printing of the device name?

Thanks!
Marc
import your report in to some file (say report_file)and try using this awk commad to remove duplicate lines
Code:
awk '!x[$0]++'  report_file

if your are using solaris use

nawk '!x[$0]++'  report_file

# 4  
Old 04-22-2013
Here is a complete program that might work for you:
Code:
awk '
        /^RP/ {
                if ( device && E_port_type )
                {
                        print "Device Name:", device
                        print RS "Port Type: ", E_port_type
                        print E_capacity, "Capacity"
                        print E_port_in_u, "Port in use"
                        print E_port_nt_u, "Port not in use"
                }
                if ( device && G_port_type )
                {
                        print RS "Port Type:", G_port_type
                        print G_capacity, "Capacity"
                        print G_port_in_u, "Port in use"
                        print G_port_nt_u, "Port not in use"
                }
                if ( device && T_port_type )
                {
                        print RS "Port Type:", T_port_type
                        print T_capacity, "Capacity"
                        print T_port_in_u, "Port in use"
                        print T_port_nt_u, "Port not in use"
                }
                device = ""
                E_port_type = E_capacity = E_port_in_u = E_port_nt_u = ""
                G_port_type = G_capacity = G_port_in_u = G_port_nt_u = ""
                T_port_type = T_capacity = T_port_in_u = T_port_nt_u = ""
                gsub (/.*:|#/, X)
                device = $0
        }
        /IFT_ETHERBUNDLE/ {
                E_port_type = $1
                E_capacity  = $2
                E_port_in_u = $3 + $4
                E_port_nt_u = $5
        }
        /IFT_GETHERNET/ {
                G_port_type = $1
                G_capacity  = $2
                G_port_in_u = $3 + $4
                G_port_nt_u = $5
        }
        /IFT_TENGETHERNET/ {
                T_port_type = $1
                T_capacity  = $2
                T_port_in_u = $3 + $4
                T_port_nt_u = $5
        }
' OFS='\t' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-22-2013
The issue with this approach is that..as I said...the data I've provided is a cut down of the actual report. I a real report, there will be data from multiple devices listed.

So constantly writing extra files so I can then clean them up is a lot of extra cycles I don't want to have to do unless I'm forced to.

But thanks for the input!
Marc

Quote:
Originally Posted by zozoo
import your report in to some file (say report_file)and try using this awk commad to remove duplicate lines
Code:
awk '!x[$0]++'  report_file

if your are using solaris use

nawk '!x[$0]++'  report_file

---------- Post updated at 03:28 PM ---------- Previous update was at 03:16 PM ----------

Ok, Some of this is completely new to me.
I know it's a lot happening, but can you explain the logic?
I've never seen what appears to be awk being used like a switch?

Quote:
Originally Posted by Yoda
Here is a complete program that might work for you:
Code:
awk '
        /^RP/ {
                if ( device && E_port_type )
                {
                        print "Device Name:", device
                        print RS "Port Type: ", E_port_type
                        print E_capacity, "Capacity"
                        print E_port_in_u, "Port in use"
                        print E_port_nt_u, "Port not in use"
                }
                if ( device && G_port_type )
                {
                        print RS "Port Type:", G_port_type
                        print G_capacity, "Capacity"
                        print G_port_in_u, "Port in use"
                        print G_port_nt_u, "Port not in use"
                }
                if ( device && T_port_type )
                {
                        print RS "Port Type:", T_port_type
                        print T_capacity, "Capacity"
                        print T_port_in_u, "Port in use"
                        print T_port_nt_u, "Port not in use"
                }
                device = ""
                E_port_type = E_capacity = E_port_in_u = E_port_nt_u = ""
                G_port_type = G_capacity = G_port_in_u = G_port_nt_u = ""
                T_port_type = T_capacity = T_port_in_u = T_port_nt_u = ""
                gsub (/.*:|#/, X)
                device = $0
        }
        /IFT_ETHERBUNDLE/ {
                E_port_type = $1
                E_capacity  = $2
                E_port_in_u = $3 + $4
                E_port_nt_u = $5
        }
        /IFT_GETHERNET/ {
                G_port_type = $1
                G_capacity  = $2
                G_port_in_u = $3 + $4
                G_port_nt_u = $5
        }
        /IFT_TENGETHERNET/ {
                T_port_type = $1
                T_capacity  = $2
                T_port_in_u = $3 + $4
                T_port_nt_u = $5
        }
' OFS='\t' file

# 6  
Old 04-22-2013
Shell approach without external utilities..
Code:
file=capturesummary.txt
# use a code block to direct the input file into the two consecutive while loops.
{ 
  # while read dev, read the first part
  while read dev
  do
    # if $dev starts with "RP"
    case $dev in
      RP*)
        # cut the trailing hash-sign
        dev=${dev%#} 
        # print the device name and cut the text upto and including the colon'
        printf "Device Name:  %s\n\n" "${dev#*:}"
        # break the for loop so we can start the next loop
        break
    esac
  done

  # read these 5 variables
  while read interface total up down admindown
  do
    # if $interface equals one of the values below
    case $interface in
      IFT_ETHERBUNDLE|IFT_GETHERNET|IFT_TENGETHERNET) 
        # print the port type
        printf 'Port type:\t %s :\n' "$interface"
        # and print the $total followed by the sum of $up and $down, and then $admindown
        printf '%3d\t%s\n' "$total" Capacity "$((up+down))" "Ports in Use" "$admindown" "Ports Not in Use"
    esac
  done
# feed "$file" into the code block
} < "$file"


Last edited by Scrutinizer; 04-22-2013 at 06:20 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-22-2013
Hummm

I copied your approach into a script test2.sh

Here are the results
Code:
"test2.sh" 19 lines, 482 characters
bash-3.2# chmod 774 test2.sh
bash-3.2# ./test2.sh
Device Name:  RP/0/RP0/CPU0

Port type:       IFT_ETHERBUNDLE :
  1     Capacity
  1     Ports in Use
  0     Ports Not in Use
Port type:       IFT_GETHERNET :
 16     Capacity
  5     Ports in Use
 11     Ports Not in Use
bash-3.2#

So this failed to parse the device name correctly and missed some data.
I'm not 100% on the logic and wonder if you can explain so I can troubleshoot?

Thanks!

Quote:
Originally Posted by Scrutinizer
Shell approach without external utilities..
Code:
file=capturesummary.txt
{
  while IFS=':#' read dev nr dummy
  do
    case $dev in
      *RP/*) printf "Device Name:  %s\n\n" "$dev"; break
    esac
  done

  while read interface total up down admindown
  do
    case $interface in
      IFT_ETHERBUNDLE|IFT_GETHERNET|IFT_TENGETHERNET)
        printf 'Port type:\t %s :\n' "$interface"
        printf '%3d\t%s\n' "$total" Capacity "$((up+down))" "Ports in Use" "$admindown" "Ports Not in Use"
    esac
  done
} < "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

3. Shell Programming and Scripting

how to duplicate an output in shell script

how to duplicate an output from a shell command? for example: `date` will give the current date to the console. I want this to be displayed in console and also parallely store it in file or variable. user1@solaris4:~> date Tue Feb 28 17:48:31 EST 2012 user1@solaris4:~> date > file ... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

4. Shell Programming and Scripting

How get rid of kill output message?

Hi, My problem is the following, I have a little shell script that has a couple of functions... in one function I start an app like this... runmqtrm -m $ourManager -q $ourMQSYS.EVTINITQ 1>/dev/null 2>&1 & In another function I kill that app using kill -s SIGKILL $triggerMonitorPID... (15 Replies)
Discussion started by: valiadi
15 Replies

5. Shell Programming and Scripting

Get rid of the 7th character of each line if this is a space

I have a text file like this ... B 16 1.340E+05 A 18 3.083E+02 Wu123 1.365E+02 ... I would like to get rid of the 7th character of each line if this is a space character. Thank you, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

6. Shell Programming and Scripting

Duplicate output without tee

Hi, Is there anyway to duplicate output without using tee? Let me explain the problem. We are use ssh to login to remote server and save output to a file using tee commands for auditing purposes. When we use vi editor in ssh session, letters get garbled and cant really use vi. Without tee it... (7 Replies)
Discussion started by: eagles1
7 Replies

7. Shell Programming and Scripting

How to get rid of last line

I have to process a data file in Ab Initio. This data file is pipe delimited. BUt the file may have a Disclaimer line at the end. So before picking it for processing, I need to check if this line is there I need to remove it. ANy suggestions. Thanks Shalu (1 Reply)
Discussion started by: shalua
1 Replies

8. Shell Programming and Scripting

getting rid of duplicate files

i have a bad problem with multiple occurances of the same file in different directories.. how this happened i am not sure! but I know that i can use awk to scan multiple directory trees to find an occurance of the same file... some of these files differ somwhat but that does not matter! the... (4 Replies)
Discussion started by: moxxx68
4 Replies

9. Shell Programming and Scripting

how to get rid of blank line in a flat text file

Hi, I have a flat text file which contains blank line between each text line. Is there any command to get rid of it? Thanks for your help (11 Replies)
Discussion started by: xfang
11 Replies

10. Shell Programming and Scripting

getting rid of $ when entered at the command line

Hi everyone, Can someone possibly help me with this problem I am having please. I wrote a Korn shell script to manipulate currency amounts in a way that a person could use this script to determine the minimum number of coins required to make a certain amount. for example when entered on the... (2 Replies)
Discussion started by: bashirpopal
2 Replies
Login or Register to Ask a Question