awk Script

I am trying a script where i awk the output text file to a csv file

Attached are the input file , I am a newbie at bash scripting

Input Text file




awk '
/NE name:/ {
        n = $4
}		
/System Name:/ {
        r = $4
}		
/System Name: / {
        p = $3  $4  $5  $6		
		print n , r , p


		
}' OFS=, /cygdrive/c/output/LLDP/output.txt >>$i

Desired CSV output


AA2M3A 	172.17.168.226	 ER_3898_1_FON	Huawei RTN 980
AA2M3A        172.17.168.226	ER_3898_1_FON	        Huawei RTN 980
ABHM7a 	172.17.167.2	         HCX16.01          	        Huawei Versatile Routing Platform Software
ABHM7a 	172.17.167.2	         HCX16.02	                Huawei Versatile Routing Platform Software



Hi

enca output.txt
7bit ASCII characters
  Mixed line terminators

Does that bother you?

1 Like

Your spec seems a bit incomplete, pls add more details:

  • how to select / reject the devices (e.g. 172.17.168.227 is missing in your desired output)?
  • how to fill items not yet found when encountering the print trigger ("System Name:")?
  • why not use "System Description" as the print trigger?
  • why <TAB> chars in your desired output, not the commas set as OFS in your script?

Is it possible to get a somewhat more structured data file to analyse?

With what we have got now, how far would this get you:

awk '
                {sub ("\r+$", _)
                }
/spawn/         {sub (/^.*@/, _)
                 IP = $0
                 NENM = DESC = ""
                }               
/NE name:/      {NENM = $4
                }
/System Name: / {sub (/^System Name: /, _)
                 SYNM = $0
                }
/System Desc/   {getline
                 sub ("\r+$", _)
                 print NENM, IP, SYNM, $0
                }
' OFS=, /tmp/output.txt
AA2M3A,172.17.168.226,ER_3898_1_FON,Huawei RTN 980
AA2M3A,172.17.168.226,ER_3898_1_FON,Huawei RTN 980
AB4M7a,172.17.168.227,ABH4.UPE.HCX16.01,Huawei Versatile Routing Platform Software
AB4M7a,172.17.168.227,ABH4.UPE.HCX16.02,Huawei Versatile Routing Platform Software
ABHM7a,172.17.167.2,HCX16.01,Huawei Versatile Routing Platform Software
ABHM7a,172.17.167.2,HCX16.02,Huawei Versatile Routing Platform Software
1 Like

Thanks alot this worked perfectly but i have one issue you can see in my output the script is skipping the first result for System Capabilities: and IP:

awk '
                {sub ("\r+$", _)
                }
/spawn/         {sub (/^.*@/, _)
                 IP = $0
                 NENM = DESC = ""
                }               
/NE name:/      {NENM = $4
                }
/System Capabilities: / {sub (/^System Capabilities: /, _)
                 SYCP = $0
                }
/System Name: / {sub (/^System Name: /, _)
                 SYNM = $0
                }
/Local Intf: / {sub (/^Local Intf: /, _)
                 LCINF = $0
                }
/IP: / {sub (/^IP:  /, _)
                 MGIP = $2
                }
/Port id: / {sub (/^Port id: /, _)
                 RMPR = $0
                }
/Chassis id: / {sub (/^Chassis id: /, _)
                 CHID = $0
                }					
/System Desc/   {getline
                 sub ("\r+$", _)
                 print NENM, IP, SYCP, LCINF, MGIP, SYNM, RMPR, CHID, $0awk '
                {sub ("\r+$", _)
                }
/spawn/         {sub (/^.*@/, _)
                 IP = $0
                 NENM = DESC = ""
                }               
/NE name:/      {NENM = $4
                }
/System Capabilities: / {sub (/^System Capabilities: /, _)
                 SYCP = $0
                }
/System Name: / {sub (/^System Name: /, _)
                 SYNM = $0
                }
/Local Intf: / {sub (/^Local Intf: /, _)
                 LCINF = $0
                }
/IP: / {sub (/^IP:  /, _)
                 MGIP = $2
                }
/Port id: / {sub (/^Port id: /, _)
                 RMPR = $0
                }
/Chassis id: / {sub (/^Chassis id: /, _)
                 CHID = $0
                }					
/System Desc/   {getline
                 sub ("\r+$", _)
                 print NENM, IP, SYCP, LCINF, MGIP, SYNM, RMPR, CHID, $0

Output


AA2M3A.SNFN,172.17.168.226, ,Gi0/2/1,ER_3898_1_FON,GigabitEthernet 22/255/1,80b5.7543.cbcb,Huawei RTN 980
AA2M3A.SNFN,172.17.168.226,B,Gi0/2/0	,129.9.118.189,ER_3898_1_FON,GigabitEthernet 17/255/1,80b5.7543.cbcb,Huawei RTN 980
AB4M7a.SNFN,172.17.167.229,B,Te0/7/6	,129.9.118.189,ABH4.UPE.HCX16.01,GigabitEthernet14/1/8	,0425.c58f.31cb,Huawei Versatile Routing Platform Software

If I do this :

grep -E 'System Capabilities: |System Desc' output.txt

Then I get:

System Description: 
System Capabilities: B
System Description: 
System Capabilities: B
System Description: 
System Capabilities: B&R
System Description: 
System Capabilities: B&R

Which means that the first "System Description" is before "System Capabilities" , so the first time SYCP is unset when the first line is printed. So the order appears to be wrong.. You would need to pick a point in the file, where you can be sure that all variables have been read.

Also it is best to clear all variables after printing.

--
BTW. You script appears to be twice in there and a bit mangled...

... at least the NE Name needs to survive a print out ...

@adgjmp: Your adapted script seems to target requirements not covered by your first spec. Be aware that it's best to prepare a decent, detailed spec covering the entire problem in order to avoid waste of resources and efforts.

Sorry for that actually after the script was working i was targeting a bigger output from the file, Like more details it can pull, It will be really helpful if i could get a solution for this, As i tried and was not able to figure out whats wrong.

@RudiC

Could you please point where i am going wrong i can work from there, As being newbie at this i am not able to figure out.

How about reading (, understanding), and following / answering post #3?

2 Likes

@RudiC

I am new to this forum, Still finding my way around the rules for posting in this forum, Apologizes for the formatting errors i have done.

In reference to your Post-3

  • how to select / reject the devices (e.g. 172.17.168.227 is missing in your desired output)?
    All devices have to accepted my output may have omitted the device while formatting

  • how to fill items not yet found when encountering the print trigger ("System Name:")?
    If the item is not found it can be set as null

  • why not use "System Description" as the print trigger?
    By mistake i guess i pasted the version of the script which i was working on , Print trigger was actually should be "System Description"

  • why <TAB> chars in your desired output, not the commas set as OFS in your script?
    I copied from the output from excel and tried to format it to paste in the forum, The Formatting got messed up.

No need to apologize, and it's not about formatting / posting rules either, although adhering to them is highly welcomed. Help people help you by creating a decent, detailed, in depth specification that doesn't leave questions nor uncertainties open.

Please look back into all your posts in this thread so far. I admit there may be a language barrier (I'm not a native speaker, either), but: They're incomplete, unstructured, messed up (sic), and contain mistakes (sic). How should someone start decent analysis or programming based on what's given? Everything given so far is based on guesswork and doomed to fail on your real request.

EDIT:
BTW, if the input data is as unstructured as you attached it, it might be wiser to look for a different approach... Show the process you use from the beginning.

@RudiC
I tried to format the whole issue.

Below is the code i am currently using

#!/bin/bash

cd /cygdrive/c/output/LLDP
#cat *.txt > output_temp.txt

i=/cygdrive/c/output/LLDP/output.csv

echo "Hostname, IP, Capabilities, Dest_Host, Local_Port, Mgmt_IP, Remote Platform, Remote_Port, SysName, Version, Chasis_ID" >$i


awk '
                {sub ("\r+$", _)
                }
/spawn/         {sub (/^.*@/, _)
                 IP = $0
                 NENM = DESC = ""
                }               
/NE name:/      {NENM = $4
                }
/System Capabilities: / {sub (/^System Capabilities: /, _)
                 SYCP = $0
                }
/System Name: / {sub (/^System Name: /, _)
                 SYNM = $0
                }
/Local Intf: / {sub (/^Local Intf: /, _)
                 LCINF = $0
                }
/IP: / {sub (/^IP:  /, _)
                 MGIP = $2
                }
/Port id: / {sub (/^Port id: /, _)
                 RMPR = $0
                }
/Port Description: / {sub (/^Port Description: /, _)
                 PODES = $0
                }	
/VRP (R) software& Version  / {sub (/^/, _)
                 VER = $0
                }					
/Chassis id: / {sub (/^Chassis id: /, _)
                 CHID = $0
                }					
/System Desc/   {getline
                 sub ("\r+$", _)
                 print NENM, IP, SYCP , SYNM , LCINF, MGIP, $0, PODES, RMPR,VER, CHID 			 
                }		
' OFS=, /cygdrive/c/output/LLDP/output.txt >>$i
 

Input text file is attached.

As you can see in the output "Capabilities" and "Mgmt_IP" output is printed in the second row instead of the first.

1 Like

Still i am stuck:)