Passing Variable into awk gsub

6,147

You used vovar outside single quotes for the first time, but not the second time:

... awk '/'"$vovar"'/,/}/ {gsub("$vovar"," ") ...

If you'd been consistent:

$ awk '/'"$vovar"'/,/}/ {gsub("'"$vovar"'"," "); gsub("}",": %s,"); gsub("{"," ");gsub(",",": %s,");print}' foo


                ciscoFlashCopyStatus  
                : %s: %s,
Share:
6,147

Related videos on Youtube

SamFlynn
Author by

SamFlynn

TRON LIVES

Updated on September 18, 2022

Comments

  • SamFlynn
    SamFlynn over 1 year

    I have this command
    Here

    vovar=OBJECTS
    prvar="$(awk '/'"$vovar"'/,/}/ {gsub("$vovar"," "); gsub("}",": %s,"); gsub("{"," ");gsub(",",": %s,");print}' temp1)"
    

    Contents of temp1

    ciscoFlashCopyCompletionTrap NOTIFICATION-TYPE
            OBJECTS   
                    { 
                    ciscoFlashCopyStatus  
                    }
            STATUS  current
            DESCRIPTION
                    "A ciscoFlashCopyCompletionTrap is sent at the 
                    completion of a flash copy operation if such a trap 
                    was requested when the operation was initiated.
                    "
            ::= { ciscoFlashMIBTraps 1 }
    

    What I want the command to do

                    ciscoFlashCopyStatus  
                    : %s: %s,
    

    What it actually does

            OBJECTS   
    
                    ciscoFlashCopyStatus  
                    : %s: %s,
    

    How can I change my awk command so that I can get what I want?

    • Avinash Raj
      Avinash Raj almost 9 years
      use -v param to pass variables into awk.
    • SamFlynn
      SamFlynn almost 9 years
      Yep! tried that already. Still getting the same output with OBJECTS written in the first line.
  • SamFlynn
    SamFlynn almost 9 years
    Ah damn it. Sorry about this.
  • A.B.
    A.B. almost 9 years
    You are too fast for me ;)