0

I've got a problem with adding variables to AWK.

I've got a file with the following input:

MainComputer>
Device ID: name1
Interface: 1
random text...
Device ID: name2
Interface: 2
random text....
Device ID: name3
Interface: 3
random text....

Now I want to print all the variables: What I already have:

#!/bin/bash
line=$(head -n 1 file)
var=$(echo $line | cut -d ">" -f1)
var2=$(awk '/Interface/  {print $2}' file)  
awk -v var3="$var" '/Device/ {print var3, "->", $2, "[Label: "$var2"]}' file

But the $var2 doesn't show an output, and if I put: var2 it gives an error.

Output I want:

MainComputer -> name1 [Label: 1]
MainComputer -> name2 [Label: 2]
MainComputer -> name3 [Label: 3]

And so on for the other 40 inputs....

But it only gives MainComputer -> name1 and an error on the interface label...

So I'm seeking a method for printing more than 1 var that I already initialized.

Thanks anyway!

3
  • What is the output you're looking for? Could you edit your question to show us? Commented Apr 23, 2015 at 8:56
  • Obviously, why have you assigned -v var3="$var" properly but then tried to use $var2 inside. Shell variable do not expand in single quotes, your entire awk command is in single quotes. Commented Apr 23, 2015 at 8:57
  • Is it just that one line you want? Do you not want to show anything for name2 and name3? Commented Apr 23, 2015 at 9:05

1 Answer 1

1

If I understand what you're attempting to do correctly, this could be better achieved in pure awk:

awk -F ': *' 'NR == 1 { sub(/>.*/, ""); name = $0; next } $1 == "Device ID" { dev = $2 } $1 == "Interface" { print name " -> " dev " [Label: " $2 "]" }' file

-F ': *' sets the field separator to the regex : * (matching a colon followed by an arbitrary number of spaces). The code works as follows:

NR == 1 {           # in the first line:
  sub(/>.*/, "")    # remove > and everything after it
  name = $0         # remember as machine name
  next              # do nothing else
}
$1 == "Device ID" { # after that: In device lines
  dev = $2          # remember device name
}
                    # In interface lines: print remembered info
$1 == "Interface" {
  print name " -> " dev " [Label: " $2 "]"
}

This will show all lines in the file formatted the way you show in the question. If you want to filter for a particular line, this could be amended as follows:

awk -v filter="name2" -F ': *' 'NR == 1 { sub(/>.*/, ""); name = $0; next } $1 == "Device ID" { dev = $2 } $1 == "Interface" && dev == filter { print name " -> " dev " [Label: " $2 "]" }' file

That is, make a variable filter known to awk with -v filter=value, and amend the code

NR == 1 {
  sub(/>.*/, "")
  name = $0
  next
}
$1 == "Device ID" {
  dev = $2     
}
$1 == "Interface" && dev == filter {         # <-- here
  print name " -> " dev " [Label: " $2 "]"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.