How can I get quotes to show up when I'm using AWK to find and replace?











up vote
0
down vote

favorite












Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. The string is formatted as such, and there are many of these strings in the file:



func(tempID="39849235",count='12');



Using this link, I was able to find a method of using AWK to find and replace the first three instances of the string. I changed it to what I needed it to do, and a snippet of my script is below:



id=12349876
awk 'BEGIN {matches=0}
matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ }
{ print $0 }' filName.py >filName.py.changed


The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id. The find and replace works well, but the one issue I seem to have is that no matter how I structure it, the output prints the $id without quotes. I've tried escaping quotes and putting single ticks, but regardless the line is changed to:



func(tempID=39849235,count='12');



I've tried removing the double quotes around the replace portion and structuring it as tempID="$id", but unfortunately this just replaces the ID number with the string $id.



Please let me know if there is a way to find and replace the tempID value and surround the value with quotes. I'm not stuck with AWK, so any other method with any other utility such as sed would work fine as well.










share|improve this question


























    up vote
    0
    down vote

    favorite












    Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. The string is formatted as such, and there are many of these strings in the file:



    func(tempID="39849235",count='12');



    Using this link, I was able to find a method of using AWK to find and replace the first three instances of the string. I changed it to what I needed it to do, and a snippet of my script is below:



    id=12349876
    awk 'BEGIN {matches=0}
    matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ }
    { print $0 }' filName.py >filName.py.changed


    The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id. The find and replace works well, but the one issue I seem to have is that no matter how I structure it, the output prints the $id without quotes. I've tried escaping quotes and putting single ticks, but regardless the line is changed to:



    func(tempID=39849235,count='12');



    I've tried removing the double quotes around the replace portion and structuring it as tempID="$id", but unfortunately this just replaces the ID number with the string $id.



    Please let me know if there is a way to find and replace the tempID value and surround the value with quotes. I'm not stuck with AWK, so any other method with any other utility such as sed would work fine as well.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. The string is formatted as such, and there are many of these strings in the file:



      func(tempID="39849235",count='12');



      Using this link, I was able to find a method of using AWK to find and replace the first three instances of the string. I changed it to what I needed it to do, and a snippet of my script is below:



      id=12349876
      awk 'BEGIN {matches=0}
      matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ }
      { print $0 }' filName.py >filName.py.changed


      The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id. The find and replace works well, but the one issue I seem to have is that no matter how I structure it, the output prints the $id without quotes. I've tried escaping quotes and putting single ticks, but regardless the line is changed to:



      func(tempID=39849235,count='12');



      I've tried removing the double quotes around the replace portion and structuring it as tempID="$id", but unfortunately this just replaces the ID number with the string $id.



      Please let me know if there is a way to find and replace the tempID value and surround the value with quotes. I'm not stuck with AWK, so any other method with any other utility such as sed would work fine as well.










      share|improve this question













      Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. The string is formatted as such, and there are many of these strings in the file:



      func(tempID="39849235",count='12');



      Using this link, I was able to find a method of using AWK to find and replace the first three instances of the string. I changed it to what I needed it to do, and a snippet of my script is below:



      id=12349876
      awk 'BEGIN {matches=0}
      matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ }
      { print $0 }' filName.py >filName.py.changed


      The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id. The find and replace works well, but the one issue I seem to have is that no matter how I structure it, the output prints the $id without quotes. I've tried escaping quotes and putting single ticks, but regardless the line is changed to:



      func(tempID=39849235,count='12');



      I've tried removing the double quotes around the replace portion and structuring it as tempID="$id", but unfortunately this just replaces the ID number with the string $id.



      Please let me know if there is a way to find and replace the tempID value and surround the value with quotes. I'm not stuck with AWK, so any other method with any other utility such as sed would work fine as well.







      linux bash shell-script bash-scripting awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 3 at 21:43









      AndreasKralj

      124




      124






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          In your command there are quotes interpreted and stripped by the shell, there are quotes interpreted and stripped by awk, then you need quotes that would survive. You should escape them:



          id=12349876
          awk 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ }
          { print $0 }' filName.py >filName.py.changed # ^^ here ^^




          Explanation. Your original command is like



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^


          And this is the improved command:



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # these quotes will appear in the output thanks to being escaped ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^




          To reduce quoting frenzy you may use -v option to pass the variable to awk. Then you don't need to close and reopen single quotes mid-sequence only to let the shell expand $id. Instead unquoted (as awk sees it) id is expanded by awk on its own. Double quotes we need to add should be escaped as before:



          id=12349876
          awk -v id="$id" 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID=""id"""); matches++ }
          { print $0 }' filName.py >filName.py.changed





          share|improve this answer





















          • Thank you very much! The awk -v was just what I needed!
            – AndreasKralj
            Dec 3 at 23:07


















          up vote
          0
          down vote













          To achieve what you want to do, you need to:



          For double quotes:




          • AWK: Escape double quotes " inside other double quotes.
            $1 $2 => foobar
            $1" __"$2"__" => foo __bar__
            $1" ""$2""" => foo "bar"


          For single quotes:




          • Shell: Exit '…' your whole awk script is in, using another set of '…' inside it.
            'escaped 'unescaped' escaped'
            '$LINUX '$OSTYPE' $CPUTYPE' => $LINUX linux-gnu $CPUTYPE


          • Shell: Escape ' you want to be printed literally.
            'apostrophe that'''s literal' => apostrophe that's literal



          For example



          echo foo bar | awk '{print """$1"" '''"$2"'''"}'
          "foo" 'bar'





          share|improve this answer





















          • Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
            – Paulo
            Dec 3 at 23:30











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1380528%2fhow-can-i-get-quotes-to-show-up-when-im-using-awk-to-find-and-replace%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          In your command there are quotes interpreted and stripped by the shell, there are quotes interpreted and stripped by awk, then you need quotes that would survive. You should escape them:



          id=12349876
          awk 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ }
          { print $0 }' filName.py >filName.py.changed # ^^ here ^^




          Explanation. Your original command is like



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^


          And this is the improved command:



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # these quotes will appear in the output thanks to being escaped ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^




          To reduce quoting frenzy you may use -v option to pass the variable to awk. Then you don't need to close and reopen single quotes mid-sequence only to let the shell expand $id. Instead unquoted (as awk sees it) id is expanded by awk on its own. Double quotes we need to add should be escaped as before:



          id=12349876
          awk -v id="$id" 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID=""id"""); matches++ }
          { print $0 }' filName.py >filName.py.changed





          share|improve this answer





















          • Thank you very much! The awk -v was just what I needed!
            – AndreasKralj
            Dec 3 at 23:07















          up vote
          0
          down vote



          accepted










          In your command there are quotes interpreted and stripped by the shell, there are quotes interpreted and stripped by awk, then you need quotes that would survive. You should escape them:



          id=12349876
          awk 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ }
          { print $0 }' filName.py >filName.py.changed # ^^ here ^^




          Explanation. Your original command is like



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^


          And this is the improved command:



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # these quotes will appear in the output thanks to being escaped ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^




          To reduce quoting frenzy you may use -v option to pass the variable to awk. Then you don't need to close and reopen single quotes mid-sequence only to let the shell expand $id. Instead unquoted (as awk sees it) id is expanded by awk on its own. Double quotes we need to add should be escaped as before:



          id=12349876
          awk -v id="$id" 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID=""id"""); matches++ }
          { print $0 }' filName.py >filName.py.changed





          share|improve this answer





















          • Thank you very much! The awk -v was just what I needed!
            – AndreasKralj
            Dec 3 at 23:07













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          In your command there are quotes interpreted and stripped by the shell, there are quotes interpreted and stripped by awk, then you need quotes that would survive. You should escape them:



          id=12349876
          awk 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ }
          { print $0 }' filName.py >filName.py.changed # ^^ here ^^




          Explanation. Your original command is like



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^


          And this is the improved command:



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # these quotes will appear in the output thanks to being escaped ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^




          To reduce quoting frenzy you may use -v option to pass the variable to awk. Then you don't need to close and reopen single quotes mid-sequence only to let the shell expand $id. Instead unquoted (as awk sees it) id is expanded by awk on its own. Double quotes we need to add should be escaped as before:



          id=12349876
          awk -v id="$id" 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID=""id"""); matches++ }
          { print $0 }' filName.py >filName.py.changed





          share|improve this answer












          In your command there are quotes interpreted and stripped by the shell, there are quotes interpreted and stripped by awk, then you need quotes that would survive. You should escape them:



          id=12349876
          awk 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ }
          { print $0 }' filName.py >filName.py.changed # ^^ here ^^




          Explanation. Your original command is like



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^


          And this is the improved command:



          awk 'BEGIN {matches=0} matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID="'"$id"'""); matches++ } { print $0 }'
          # ^ these quotes are seen by the shell and don't get to awk ^^ ^^ ^
          # these quotes get to awk and serve their purpose there ^ ^ ^ ^
          # these quotes will appear in the output thanks to being escaped ^ ^
          # this variable is expanded by the shell and gets to awk as its value ^^^




          To reduce quoting frenzy you may use -v option to pass the variable to awk. Then you don't need to close and reopen single quotes mid-sequence only to let the shell expand $id. Instead unquoted (as awk sees it) id is expanded by awk on its own. Double quotes we need to add should be escaped as before:



          id=12349876
          awk -v id="$id" 'BEGIN {matches=0}
          matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID=""id"""); matches++ }
          { print $0 }' filName.py >filName.py.changed






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 3 at 23:00









          Kamil Maciorowski

          23.3k155072




          23.3k155072












          • Thank you very much! The awk -v was just what I needed!
            – AndreasKralj
            Dec 3 at 23:07


















          • Thank you very much! The awk -v was just what I needed!
            – AndreasKralj
            Dec 3 at 23:07
















          Thank you very much! The awk -v was just what I needed!
          – AndreasKralj
          Dec 3 at 23:07




          Thank you very much! The awk -v was just what I needed!
          – AndreasKralj
          Dec 3 at 23:07












          up vote
          0
          down vote













          To achieve what you want to do, you need to:



          For double quotes:




          • AWK: Escape double quotes " inside other double quotes.
            $1 $2 => foobar
            $1" __"$2"__" => foo __bar__
            $1" ""$2""" => foo "bar"


          For single quotes:




          • Shell: Exit '…' your whole awk script is in, using another set of '…' inside it.
            'escaped 'unescaped' escaped'
            '$LINUX '$OSTYPE' $CPUTYPE' => $LINUX linux-gnu $CPUTYPE


          • Shell: Escape ' you want to be printed literally.
            'apostrophe that'''s literal' => apostrophe that's literal



          For example



          echo foo bar | awk '{print """$1"" '''"$2"'''"}'
          "foo" 'bar'





          share|improve this answer





















          • Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
            – Paulo
            Dec 3 at 23:30















          up vote
          0
          down vote













          To achieve what you want to do, you need to:



          For double quotes:




          • AWK: Escape double quotes " inside other double quotes.
            $1 $2 => foobar
            $1" __"$2"__" => foo __bar__
            $1" ""$2""" => foo "bar"


          For single quotes:




          • Shell: Exit '…' your whole awk script is in, using another set of '…' inside it.
            'escaped 'unescaped' escaped'
            '$LINUX '$OSTYPE' $CPUTYPE' => $LINUX linux-gnu $CPUTYPE


          • Shell: Escape ' you want to be printed literally.
            'apostrophe that'''s literal' => apostrophe that's literal



          For example



          echo foo bar | awk '{print """$1"" '''"$2"'''"}'
          "foo" 'bar'





          share|improve this answer





















          • Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
            – Paulo
            Dec 3 at 23:30













          up vote
          0
          down vote










          up vote
          0
          down vote









          To achieve what you want to do, you need to:



          For double quotes:




          • AWK: Escape double quotes " inside other double quotes.
            $1 $2 => foobar
            $1" __"$2"__" => foo __bar__
            $1" ""$2""" => foo "bar"


          For single quotes:




          • Shell: Exit '…' your whole awk script is in, using another set of '…' inside it.
            'escaped 'unescaped' escaped'
            '$LINUX '$OSTYPE' $CPUTYPE' => $LINUX linux-gnu $CPUTYPE


          • Shell: Escape ' you want to be printed literally.
            'apostrophe that'''s literal' => apostrophe that's literal



          For example



          echo foo bar | awk '{print """$1"" '''"$2"'''"}'
          "foo" 'bar'





          share|improve this answer












          To achieve what you want to do, you need to:



          For double quotes:




          • AWK: Escape double quotes " inside other double quotes.
            $1 $2 => foobar
            $1" __"$2"__" => foo __bar__
            $1" ""$2""" => foo "bar"


          For single quotes:




          • Shell: Exit '…' your whole awk script is in, using another set of '…' inside it.
            'escaped 'unescaped' escaped'
            '$LINUX '$OSTYPE' $CPUTYPE' => $LINUX linux-gnu $CPUTYPE


          • Shell: Escape ' you want to be printed literally.
            'apostrophe that'''s literal' => apostrophe that's literal



          For example



          echo foo bar | awk '{print """$1"" '''"$2"'''"}'
          "foo" 'bar'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 3 at 22:55









          A. Z.

          13




          13












          • Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
            – Paulo
            Dec 3 at 23:30


















          • Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
            – Paulo
            Dec 3 at 23:30
















          Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
          – Paulo
          Dec 3 at 23:30




          Another way is with octal or hexadecimal codes inside double quotes echo foo bar | awk '{print "x22"$1"42","x27"$2"47"}'
          – Paulo
          Dec 3 at 23:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1380528%2fhow-can-i-get-quotes-to-show-up-when-im-using-awk-to-find-and-replace%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mouse cursor on multiple screens with different PPI

          Agildo Ribeiro

          Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”