Awk of string followed by 'n'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I try to parse a plain text config file A.conf with the following content:



bitrate 500000
use_can 1


I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
Here is the corresponding part of my shell script:



bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate


It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
In that case I get this error output:



" is wrong: invalid "bitrate" value


What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I try to parse a plain text config file A.conf with the following content:



    bitrate 500000
    use_can 1


    I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
    Here is the corresponding part of my shell script:



    bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
    sudo /sbin/ip link set can0 up type can bitrate $bitrate


    It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
    In that case I get this error output:



    " is wrong: invalid "bitrate" value


    What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I try to parse a plain text config file A.conf with the following content:



      bitrate 500000
      use_can 1


      I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
      Here is the corresponding part of my shell script:



      bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
      sudo /sbin/ip link set can0 up type can bitrate $bitrate


      It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
      In that case I get this error output:



      " is wrong: invalid "bitrate" value


      What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.










      share|improve this question















      I try to parse a plain text config file A.conf with the following content:



      bitrate 500000
      use_can 1


      I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
      Here is the corresponding part of my shell script:



      bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
      sudo /sbin/ip link set can0 up type can bitrate $bitrate


      It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
      In that case I get this error output:



      " is wrong: invalid "bitrate" value


      What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.







      text-processing awk grep newlines






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 13 at 10:04









      Jeff Schaller

      32.5k849110




      32.5k849110










      asked Aug 13 at 9:58









      oh.dae.su

      171118




      171118




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.



          bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"


          Notice also how this does away with the useless cat and the useless grep.






          share|improve this answer


















          • 2




            Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
            – Kamil Maciorowski
            Aug 13 at 10:13






          • 2




            Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
            – tripleee
            Aug 13 at 10:19










          • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
            – oh.dae.su
            Aug 13 at 10:28










          • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
            – steeldriver
            Aug 13 at 11:07










          • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
            – tripleee
            Aug 13 at 11:20










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462263%2fawk-of-string-followed-by-n%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.



          bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"


          Notice also how this does away with the useless cat and the useless grep.






          share|improve this answer


















          • 2




            Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
            – Kamil Maciorowski
            Aug 13 at 10:13






          • 2




            Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
            – tripleee
            Aug 13 at 10:19










          • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
            – oh.dae.su
            Aug 13 at 10:28










          • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
            – steeldriver
            Aug 13 at 11:07










          • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
            – tripleee
            Aug 13 at 11:20














          up vote
          3
          down vote



          accepted










          Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.



          bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"


          Notice also how this does away with the useless cat and the useless grep.






          share|improve this answer


















          • 2




            Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
            – Kamil Maciorowski
            Aug 13 at 10:13






          • 2




            Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
            – tripleee
            Aug 13 at 10:19










          • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
            – oh.dae.su
            Aug 13 at 10:28










          • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
            – steeldriver
            Aug 13 at 11:07










          • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
            – tripleee
            Aug 13 at 11:20












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.



          bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"


          Notice also how this does away with the useless cat and the useless grep.






          share|improve this answer














          Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.



          bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"


          Notice also how this does away with the useless cat and the useless grep.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 13 at 10:09

























          answered Aug 13 at 10:06









          tripleee

          4,84911626




          4,84911626







          • 2




            Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
            – Kamil Maciorowski
            Aug 13 at 10:13






          • 2




            Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
            – tripleee
            Aug 13 at 10:19










          • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
            – oh.dae.su
            Aug 13 at 10:28










          • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
            – steeldriver
            Aug 13 at 11:07










          • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
            – tripleee
            Aug 13 at 11:20












          • 2




            Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
            – Kamil Maciorowski
            Aug 13 at 10:13






          • 2




            Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
            – tripleee
            Aug 13 at 10:19










          • Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
            – oh.dae.su
            Aug 13 at 10:28










          • It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
            – steeldriver
            Aug 13 at 11:07










          • Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
            – tripleee
            Aug 13 at 11:20







          2




          2




          Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
          – Kamil Maciorowski
          Aug 13 at 10:13




          Without tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
          – Kamil Maciorowski
          Aug 13 at 10:13




          2




          2




          Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
          – tripleee
          Aug 13 at 10:19




          Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
          – tripleee
          Aug 13 at 10:19












          Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
          – oh.dae.su
          Aug 13 at 10:28




          Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
          – oh.dae.su
          Aug 13 at 10:28












          It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
          – steeldriver
          Aug 13 at 11:07




          It would be more natural imho to set the record separator to DOS style (e.g. -vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.
          – steeldriver
          Aug 13 at 11:07












          Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
          – tripleee
          Aug 13 at 11:20




          Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
          – tripleee
          Aug 13 at 11:20

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462263%2fawk-of-string-followed-by-n%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)