Add NA When String is not found

I am searching for these patterns in my file between commas, when I dont find a pattern match I would Like to add NA.

awk '/Device ID:/ || /address:/ || /Interface:/ || /VLAN:/ || /Platform:/ || /Software/ {print $0,"NA"}' $tempdir/shcdpnedet > $tempdir/newtt 

Provide a sample of your input file....

In addition to what shamrock has already said, please also tell us:

  • (every time you start a new thread) what operating system and shell you're using,
  • what output you hope to produce from the sample input you provide us, and
  • a clearer description of what you are trying to do (showing us code that doesn't work doesn't tell us much about what you are trying to do).

Sorry - Windows os running cygwin

Sample input:

Device ID: ciscoblah
,
  IP address: 1.1.1.1
,
Platform: C5548P,  Capabilities: Switch IGMP CVTA phone port 
,
Interface: TenGigabitEthernet1/1,  Port ID (outgoing port): Ethernet1/1
,
Cisco Nexus Operating System (NX-OS) Software, Version 7.1(4)N1(1)
,
  IP address: 1.1.1.1
,

Desired output:

Device ID: ciscoblah,
  IP address: 1.1.1.1, Platform: C5548P,  Capabilities: Switch IGMP CVTA phone port ,Interface: TenGigabitEthernet1/1,  Port ID (outgoing port): Ethernet1/1,Cisco Nexus Operating System (NX-OS) Software, Version 7.1(4)N1(1), NA,  IP address: 1.1.1.1,

Code Im using:

awk '/Device ID:/ || /address:/ || /Interface:/ || /VLAN:/ || /Platform:/ || /Software/ {print $0,"NA"}' $tempdir/shcdpnedet > $tempdir/newtt

When a field that I am pulling with the awk command

'/Device ID:/ || /address:/ || /Interface:/ || /VLAN:/ || /Platform:/ || /Software/

isnt present, I would Like NA to be output in the place of the missing field.

I am completely lost here. Your sample input file contains 12 lines (with each line being a record). There is no indication given anywhere in your sample input or in the description of your requirements specifying the order in which fields are supposed to be present. Since you specify that each output record should contain NA in the place of each missing input field in each record and since none of your input records contains more than one of the six strings you're looking for in various fields, why doesn't each of the 12 output records you would get from your 12 input records contain at least 5 occurrences of NA ?

I see no relationship between your description of your requirements and the desired output you say you want from your sample input.

Note that what your code seems to do is look for six strings on each input line and, if and only if, at least one of those string is found, that input record is copied to the output with NA added as the last output field on that line. All other input lines are discarded. I don't see any relationship between your code and your requirements and I don't see any relationship between your code and the desired output you say you want.

If you can't write down a clear description of your requirements, it isn't surprising that you can't write code that will do what you want. If you can't write down a clear description of your requirements, there is no chance that anyone else will be able to write code that does what you want.

I apologize, when I added the code I kept it to a minimum in an effort to keep it clearer.

awk '/-- show cdp neighbors detail --/,/- show diagnostic result module all detail --/' $tempdir/useful> $tempdir/shcdpnedet
awk '/Device ID:/ || /address:/ || /Interface:/ || /VLAN:/ || /Platform:/ || /Software/' $tempdir/shcdpnedet > $tempdir/newtt
tr -d '\015' < $tempdir/newtt > $tempdir/new
awk '/IP address/ { printf("%s\t", $0); next } 1' $tempdir/new> $tempdir/newt
awk '/Port ID / { printf("%s\t", $0); next } 1' $tempdir/newt > $tempdir/newt1
awk '/Native VLAN/ { printf("%s\t", $0); next } 1' $tempdir/newt1 > $tempdir/newt2
sed 's/Device/\n&/g' $tempdir/newt2  > $tempdir/newt3
awk '/Device ID:/ { printf("%s\t", $0); next } 1' $tempdir/newt3 > $tempdir/newt4
awk '/Platform:/ { printf("%s\t", $0); next } 1' $tempdir/newt4 > $tempdir/newt44
awk '/Software/ { printf("%s\t", $0); next } 1' $tempdir/newt44 > $tempdir/newt4

This is the code used to produce the output, I see the confusion with the additional fields in the desired output. If one of the input fields is not present, I would like to insert an NA

OK. I get it. You don't want to tell us what you're trying to do. You just want us to guess at what you're trying to do by showing us code that doesn't work. Showing us more code that apparently doesn't do what you're trying to do doesn't help.

Sorry, but I can't help you under these conditions. :confused:

2 Likes

But it all sounds so good in my head when I am typing it out! lol

I am truly sorry.

I am getting information from a cisco switch or router and the format of the output is different depending on the type of device connected. In some cases a match will be made and the field will be returned in other cases it doesnt exist and I would like NA to be printed in its place. This will keep all fields in the output file uniform which is key for the excel import that needs to occur. What is happening is some of the devices wont return a match for the " /VLAN:/" field. Well when this happens, it shifts everything in the output one column to the left.

Does that help to clarify? Im truly not trying to be cryptic but apparently not intelligent enough to properly communicate my problem. Thanks for your help, I do appreciate it

I don't have a Cisco switch or router handy to attempt to see what log output they produce.

I assume that the sample input you showed us in post #4 is not the contents of a switch or router log, but something that you have created by processing the log files to try to get data in the format that you want.

Please show us small samples of the data that would be found in your switch log file and the date that would be found in your router log file and explain the format of both of those.

Then explain the format of the output you hope to produce.

Then show us the output that you hope to produce by processing those two sample log files.

Sorry been away for a week. Ok, heres what Im doing:

There are several command outputs from routers and switches that have different information that is useful when looking to redesign. These are not log files. The format is all over the board between files. Ive been parsing through these files individually, extracting the data that I find valuable, formatting it for CSV.

I start with one file, parse through, create a file, then use one of the fields like a key field (VLAN or interface). When I make a match (depending on which field I am keying on, I add the correlating information to the end of the file in CSV format that I can import into a spreadsheet for quick reference).

As Ive been going through the files, when I match on one key word or another, if they keyword isnt present in the show file then it is throwing off the rest of the "complete file"
Im not trying to be cryptic or dodgy with my answers, I find that at times I am not great at explaining myself.
Here is an output example pulled from a spreadsheet: (first line had matches in all fields, second was missing a match which through all results to the left / incorrect fields in the output)

Hostname	Port	Name	Status	Vlan	Duplex	Speed	Type	VLAN Name	CDP NE Hostname	CDP NE IP	CDP NE Platform	CDP NE Capabilities	Local Interface	Remote Interface	CDP NE Software	CDP NE Software	CDP NE Version	CDP NE Release	CDP NE VLAN	CDP NE Admin IP	POE Admin	POE Oper	POE Power	POE Watts	POE Device	POE Class


Tech.log	Te4/4	inactive	1	full	auto	No XCVR	default	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA	NA
Tech.log	Gi5/1	W-01 Ge0/	connected	240	a-full	a-1000	10/100/1000-TX	Voice-Primary	.cisco.local	10..1	Cisco CISCO2911/K9	Router Switch IGMP	Gi5/1	Gi0/0	Cisco IOS Software	C2900 Software (C2900-UNIVERSALK9-M)	Version 15.2(3)T	RELEASE SOFTWARE (fc1)		auto	ff	0	0	n/a	n/a