0

I have a variable that is populated from a log file. it is capturing the host name of a server for use in the awk printout. im making a CSV file that prints the MAC address, port number then the host name.

here is that variable

hostVar=$(awk '/#/' macAddress.log)

this works fine what I need is to print the variable in my next awk statement so that it prints the var last. below works fine and prints MAC,HOST

grep -Ff masterList macAddress.log | awk '{print $2 "," $4}' >> output.csv

I am using a master list to filter out the MAC addresses I need to capture. the end result needs to look like this MAC,PORT,HOST

grep -Ff masterList.csv macAddress.log | awk '{print $2 "," $4 "," $hostVar}' >> output.csv

obviously that doesnt work but I need the host name in the CSV file next to the port number (MAC then port num then host name)

0011.2233.4455,Gi1/0,Switch1

(switch1 being what $hostVar is populated with)

  • What are you expecting '$test' to equal? Right now, AWK is looking for a awk variable named 'test' and finds none, so it defaults to 0. Then AWK looks at the $ operator and evaluates it to be equal to the entire input because $0 equal the entire input. – Algorithmic Canary Mar 30 '15 at 18:38
  • sorry, typo. anything referenced to $test should be $hostVar – Marty Lemon Mar 30 '15 at 18:41

1 Answers1

0

Right now $hostvar is trying to be resolved by AWK. Try

awk '{print $2 "," $4 "," '$hostVar'}'

to use the $hostvar from bash. Single quotes block variable being stuck into strings, in bash.

  • that doesnt work. What I need is to print 2 matches then a variable. the var captures the host name and I need it to print 2 other findings. – Marty Lemon Mar 30 '15 at 23:10