Issue while processing a line using awk in unix










0














I am running realpath command on each line of a file. Two sample lines of file are



$HOME:1:2
$HOME:1:2 3


I am expecting output of above two lines after running my command as:



/home/mjain8:1:2
/home/mjain8:1:2 3


The awk command I am running is awk 'BEGINcmd="realpath "getline;print $0;' FS=':' OFS=':'



Now, when I run the command on first line it runs fine and gives me the desired output. But for line 2 of file (shown above) the output is /home/mjain8:1:2 (and NOT /home/mjain8:1:2 3). That is the output only contains line before space.



Can someone please point what am I doing wrong. Also, in case you have suggestion to use any other command please let me know same too. I have been struggling to do same using awk since last 2 days.



I want to make it portable so that it run on as many shells as possible.










share|improve this question























  • Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
    – RavinderSingh13
    Nov 12 '18 at 4:48






  • 1




    Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
    – Mayank Jain
    Nov 12 '18 at 4:50















0














I am running realpath command on each line of a file. Two sample lines of file are



$HOME:1:2
$HOME:1:2 3


I am expecting output of above two lines after running my command as:



/home/mjain8:1:2
/home/mjain8:1:2 3


The awk command I am running is awk 'BEGINcmd="realpath "getline;print $0;' FS=':' OFS=':'



Now, when I run the command on first line it runs fine and gives me the desired output. But for line 2 of file (shown above) the output is /home/mjain8:1:2 (and NOT /home/mjain8:1:2 3). That is the output only contains line before space.



Can someone please point what am I doing wrong. Also, in case you have suggestion to use any other command please let me know same too. I have been struggling to do same using awk since last 2 days.



I want to make it portable so that it run on as many shells as possible.










share|improve this question























  • Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
    – RavinderSingh13
    Nov 12 '18 at 4:48






  • 1




    Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
    – Mayank Jain
    Nov 12 '18 at 4:50













0












0








0







I am running realpath command on each line of a file. Two sample lines of file are



$HOME:1:2
$HOME:1:2 3


I am expecting output of above two lines after running my command as:



/home/mjain8:1:2
/home/mjain8:1:2 3


The awk command I am running is awk 'BEGINcmd="realpath "getline;print $0;' FS=':' OFS=':'



Now, when I run the command on first line it runs fine and gives me the desired output. But for line 2 of file (shown above) the output is /home/mjain8:1:2 (and NOT /home/mjain8:1:2 3). That is the output only contains line before space.



Can someone please point what am I doing wrong. Also, in case you have suggestion to use any other command please let me know same too. I have been struggling to do same using awk since last 2 days.



I want to make it portable so that it run on as many shells as possible.










share|improve this question















I am running realpath command on each line of a file. Two sample lines of file are



$HOME:1:2
$HOME:1:2 3


I am expecting output of above two lines after running my command as:



/home/mjain8:1:2
/home/mjain8:1:2 3


The awk command I am running is awk 'BEGINcmd="realpath "getline;print $0;' FS=':' OFS=':'



Now, when I run the command on first line it runs fine and gives me the desired output. But for line 2 of file (shown above) the output is /home/mjain8:1:2 (and NOT /home/mjain8:1:2 3). That is the output only contains line before space.



Can someone please point what am I doing wrong. Also, in case you have suggestion to use any other command please let me know same too. I have been struggling to do same using awk since last 2 days.



I want to make it portable so that it run on as many shells as possible.







shell unix awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 4:56







Mayank Jain

















asked Nov 12 '18 at 4:44









Mayank JainMayank Jain

1,17642141




1,17642141











  • Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
    – RavinderSingh13
    Nov 12 '18 at 4:48






  • 1




    Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
    – Mayank Jain
    Nov 12 '18 at 4:50
















  • Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
    – RavinderSingh13
    Nov 12 '18 at 4:48






  • 1




    Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
    – Mayank Jain
    Nov 12 '18 at 4:50















Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
– RavinderSingh13
Nov 12 '18 at 4:48




Where is the Input_file in awk command? Is it typo or you missed it while running it too? Kindly confirm. Also it will be good if you could mention example output of realpath and your sample Input_file with sample expected output it will be good for us to get complete question's requirement.
– RavinderSingh13
Nov 12 '18 at 4:48




1




1




Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
– Mayank Jain
Nov 12 '18 at 4:50




Sorry for wording the question poorly. I am running the command on file but for clarity I have just mentioned one sample input which is ''$HOME:1:2 3". I will edit question accordingly.
– Mayank Jain
Nov 12 '18 at 4:50












2 Answers
2






active

oldest

votes


















2














With shell's while loop it will be much simpler, could you please try following. It worked fine for me.



while IFS=':' read -r path rest
do
real=$(realpath "$path")
echo "$real:$rest"
done < "Input_file"


Above code has real variable to first have realpath command's value and then it prints its output along with rest variable, in case you want to directly print them as per tripleee's comment use following then.



while IFS=':' read -r path rest
do
echo "$(realpath "$path"):$rest"
done < "Input_file"





share|improve this answer






























    0














    With Perl-one liner also, you could do it easily



    > export HOME=/home/mjain8
    > cat home.txt
    $HOME:1:2
    $HOME:1:2 3
    > perl -F: -lane ' $F[0]=$ENVHOME ;print join(":",@F) ' home.txt
    /home/mjain8:1:2
    /home/mjain8:1:2 3
    > perl -F: -lane ' $F[0]=$ENVHOME if $F[0]=~/$HOME/;print join(":",@F) ' home.txt # if you need to explicity check if it is HOME
    /home/mjain8:1:2
    /home/mjain8:1:2 3
    >





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      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',
      autoActivateHeartbeat: false,
      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%2fstackoverflow.com%2fquestions%2f53256083%2fissue-while-processing-a-line-using-awk-in-unix%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









      2














      With shell's while loop it will be much simpler, could you please try following. It worked fine for me.



      while IFS=':' read -r path rest
      do
      real=$(realpath "$path")
      echo "$real:$rest"
      done < "Input_file"


      Above code has real variable to first have realpath command's value and then it prints its output along with rest variable, in case you want to directly print them as per tripleee's comment use following then.



      while IFS=':' read -r path rest
      do
      echo "$(realpath "$path"):$rest"
      done < "Input_file"





      share|improve this answer



























        2














        With shell's while loop it will be much simpler, could you please try following. It worked fine for me.



        while IFS=':' read -r path rest
        do
        real=$(realpath "$path")
        echo "$real:$rest"
        done < "Input_file"


        Above code has real variable to first have realpath command's value and then it prints its output along with rest variable, in case you want to directly print them as per tripleee's comment use following then.



        while IFS=':' read -r path rest
        do
        echo "$(realpath "$path"):$rest"
        done < "Input_file"





        share|improve this answer

























          2












          2








          2






          With shell's while loop it will be much simpler, could you please try following. It worked fine for me.



          while IFS=':' read -r path rest
          do
          real=$(realpath "$path")
          echo "$real:$rest"
          done < "Input_file"


          Above code has real variable to first have realpath command's value and then it prints its output along with rest variable, in case you want to directly print them as per tripleee's comment use following then.



          while IFS=':' read -r path rest
          do
          echo "$(realpath "$path"):$rest"
          done < "Input_file"





          share|improve this answer














          With shell's while loop it will be much simpler, could you please try following. It worked fine for me.



          while IFS=':' read -r path rest
          do
          real=$(realpath "$path")
          echo "$real:$rest"
          done < "Input_file"


          Above code has real variable to first have realpath command's value and then it prints its output along with rest variable, in case you want to directly print them as per tripleee's comment use following then.



          while IFS=':' read -r path rest
          do
          echo "$(realpath "$path"):$rest"
          done < "Input_file"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 7:04









          Inian

          38.8k63770




          38.8k63770










          answered Nov 12 '18 at 5:35









          RavinderSingh13RavinderSingh13

          25.7k41438




          25.7k41438























              0














              With Perl-one liner also, you could do it easily



              > export HOME=/home/mjain8
              > cat home.txt
              $HOME:1:2
              $HOME:1:2 3
              > perl -F: -lane ' $F[0]=$ENVHOME ;print join(":",@F) ' home.txt
              /home/mjain8:1:2
              /home/mjain8:1:2 3
              > perl -F: -lane ' $F[0]=$ENVHOME if $F[0]=~/$HOME/;print join(":",@F) ' home.txt # if you need to explicity check if it is HOME
              /home/mjain8:1:2
              /home/mjain8:1:2 3
              >





              share|improve this answer

























                0














                With Perl-one liner also, you could do it easily



                > export HOME=/home/mjain8
                > cat home.txt
                $HOME:1:2
                $HOME:1:2 3
                > perl -F: -lane ' $F[0]=$ENVHOME ;print join(":",@F) ' home.txt
                /home/mjain8:1:2
                /home/mjain8:1:2 3
                > perl -F: -lane ' $F[0]=$ENVHOME if $F[0]=~/$HOME/;print join(":",@F) ' home.txt # if you need to explicity check if it is HOME
                /home/mjain8:1:2
                /home/mjain8:1:2 3
                >





                share|improve this answer























                  0












                  0








                  0






                  With Perl-one liner also, you could do it easily



                  > export HOME=/home/mjain8
                  > cat home.txt
                  $HOME:1:2
                  $HOME:1:2 3
                  > perl -F: -lane ' $F[0]=$ENVHOME ;print join(":",@F) ' home.txt
                  /home/mjain8:1:2
                  /home/mjain8:1:2 3
                  > perl -F: -lane ' $F[0]=$ENVHOME if $F[0]=~/$HOME/;print join(":",@F) ' home.txt # if you need to explicity check if it is HOME
                  /home/mjain8:1:2
                  /home/mjain8:1:2 3
                  >





                  share|improve this answer












                  With Perl-one liner also, you could do it easily



                  > export HOME=/home/mjain8
                  > cat home.txt
                  $HOME:1:2
                  $HOME:1:2 3
                  > perl -F: -lane ' $F[0]=$ENVHOME ;print join(":",@F) ' home.txt
                  /home/mjain8:1:2
                  /home/mjain8:1:2 3
                  > perl -F: -lane ' $F[0]=$ENVHOME if $F[0]=~/$HOME/;print join(":",@F) ' home.txt # if you need to explicity check if it is HOME
                  /home/mjain8:1:2
                  /home/mjain8:1:2 3
                  >






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 12:11









                  stack0114106stack0114106

                  2,3011417




                  2,3011417



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53256083%2fissue-while-processing-a-line-using-awk-in-unix%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo