Read line by line from a text file and print how I want in shell scripting



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I want to read below file line by line from a text file and print how I want in shell scripting



Text file content: 
zero#123456
one#123
two#12345678


I want to print this as:



zero@1-6 
one@1-3
two@1-8


I tried the following:



file="readFile.txt" 
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"









share|improve this question



















  • 1





    And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

    – Dominique
    Nov 15 '18 at 10:54

















2















I want to read below file line by line from a text file and print how I want in shell scripting



Text file content: 
zero#123456
one#123
two#12345678


I want to print this as:



zero@1-6 
one@1-3
two@1-8


I tried the following:



file="readFile.txt" 
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"









share|improve this question



















  • 1





    And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

    – Dominique
    Nov 15 '18 at 10:54













2












2








2








I want to read below file line by line from a text file and print how I want in shell scripting



Text file content: 
zero#123456
one#123
two#12345678


I want to print this as:



zero@1-6 
one@1-3
two@1-8


I tried the following:



file="readFile.txt" 
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"









share|improve this question
















I want to read below file line by line from a text file and print how I want in shell scripting



Text file content: 
zero#123456
one#123
two#12345678


I want to print this as:



zero@1-6 
one@1-3
two@1-8


I tried the following:



file="readFile.txt" 
while IFS= read -r line
do echo "$line"
done <printf '%sn' "$file"






shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:00









tso

3,17821125




3,17821125










asked Nov 15 '18 at 8:57









Santosh KumarSantosh Kumar

161




161







  • 1





    And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

    – Dominique
    Nov 15 '18 at 10:54












  • 1





    And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

    – Dominique
    Nov 15 '18 at 10:54







1




1





And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

– Dominique
Nov 15 '18 at 10:54





And if you have three#123789? Should the result be three@1-9 or something like three@1-3,7-9?

– Dominique
Nov 15 '18 at 10:54












3 Answers
3






active

oldest

votes


















1














Create a script like below: my_print.sh



file="readFile.txt" 

while IFS= read -r line
do
one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
done <"$file"


Run it like:



mayankp@mayank:~/$ sh my_print.sh 
mayankp@mayank:~/$ cat output.txt
zero@1-6
one@1-3
two@1-8


Let me know of this helps.






share|improve this answer

























  • It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

    – Santosh Kumar
    Nov 15 '18 at 9:45












  • I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

    – Mayank Porwal
    Nov 15 '18 at 9:46











  • Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

    – Santosh Kumar
    Nov 15 '18 at 9:59












  • I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

    – Mayank Porwal
    Nov 15 '18 at 10:03











  • Thanks mayank. Could you please explain the steps. It will be helpfull

    – Santosh Kumar
    Nov 15 '18 at 10:22


















1














It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:



$ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
Text file content:
zero#1-6
one#1-3
two#1-8


Explained some:




  • s//-/ replace with a -


  • (?<=[0-9]) positive lookbehind, if preceeded by a number


  • (?=[0-9]) positive lookahead, if followed by a number





share|improve this answer
































    1














    With sed:



    sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt



    • -r: using extented regular expressions (just to write some stuff without escaping them by a backslash)


    • s/expr1/expr2/: substitute expr1 by expr2


    • epxr1 is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).


    • epxr2 retrieves captured strings (1, 2, 3) and insert them in a formatted output (the one you wanted).

    Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.



    Update: Also you could do that with awk:



    awk -F'#' ' 
    gsub(/s*/,"", $2) ;
    print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
    ' < test.txt


    I added a gsub() call because your file seems to have trailing blank characters.






    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%2f53315645%2fread-line-by-line-from-a-text-file-and-print-how-i-want-in-shell-scripting%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Create a script like below: my_print.sh



      file="readFile.txt" 

      while IFS= read -r line
      do
      one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
      len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
      two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
      three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
      echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
      done <"$file"


      Run it like:



      mayankp@mayank:~/$ sh my_print.sh 
      mayankp@mayank:~/$ cat output.txt
      zero@1-6
      one@1-3
      two@1-8


      Let me know of this helps.






      share|improve this answer

























      • It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

        – Santosh Kumar
        Nov 15 '18 at 9:45












      • I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

        – Mayank Porwal
        Nov 15 '18 at 9:46











      • Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

        – Santosh Kumar
        Nov 15 '18 at 9:59












      • I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

        – Mayank Porwal
        Nov 15 '18 at 10:03











      • Thanks mayank. Could you please explain the steps. It will be helpfull

        – Santosh Kumar
        Nov 15 '18 at 10:22















      1














      Create a script like below: my_print.sh



      file="readFile.txt" 

      while IFS= read -r line
      do
      one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
      len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
      two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
      three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
      echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
      done <"$file"


      Run it like:



      mayankp@mayank:~/$ sh my_print.sh 
      mayankp@mayank:~/$ cat output.txt
      zero@1-6
      one@1-3
      two@1-8


      Let me know of this helps.






      share|improve this answer

























      • It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

        – Santosh Kumar
        Nov 15 '18 at 9:45












      • I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

        – Mayank Porwal
        Nov 15 '18 at 9:46











      • Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

        – Santosh Kumar
        Nov 15 '18 at 9:59












      • I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

        – Mayank Porwal
        Nov 15 '18 at 10:03











      • Thanks mayank. Could you please explain the steps. It will be helpfull

        – Santosh Kumar
        Nov 15 '18 at 10:22













      1












      1








      1







      Create a script like below: my_print.sh



      file="readFile.txt" 

      while IFS= read -r line
      do
      one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
      len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
      two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
      three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
      echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
      done <"$file"


      Run it like:



      mayankp@mayank:~/$ sh my_print.sh 
      mayankp@mayank:~/$ cat output.txt
      zero@1-6
      one@1-3
      two@1-8


      Let me know of this helps.






      share|improve this answer















      Create a script like below: my_print.sh



      file="readFile.txt" 

      while IFS= read -r line
      do
      one=$(echo $line| awk -F'#' 'print $1') ## This splits the line based on '#' and picks the 1st value. So, we get zero from 'zero#123456 '
      len=$(echo $line| awk -F'#' 'print $2'|wc -c) ## This takes the 2nd value which is 123456 and counts the number of characters
      two=$(echo $line| awk -F'#' 'print $2'| cut -c 1) ## This picks the 1st character from '123456' which is 1
      three=$(echo $line| awk -F'#' 'print $2'| cut -c $((len-1))) ## This picks the last character from '123456' which is 6
      echo $one@$two-$three ## This is basically printing the output in the format you wanted 'zero@1-6'
      done <"$file"


      Run it like:



      mayankp@mayank:~/$ sh my_print.sh 
      mayankp@mayank:~/$ cat output.txt
      zero@1-6
      one@1-3
      two@1-8


      Let me know of this helps.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 10:28

























      answered Nov 15 '18 at 9:28









      Mayank PorwalMayank Porwal

      5,0182725




      5,0182725












      • It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

        – Santosh Kumar
        Nov 15 '18 at 9:45












      • I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

        – Mayank Porwal
        Nov 15 '18 at 9:46











      • Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

        – Santosh Kumar
        Nov 15 '18 at 9:59












      • I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

        – Mayank Porwal
        Nov 15 '18 at 10:03











      • Thanks mayank. Could you please explain the steps. It will be helpfull

        – Santosh Kumar
        Nov 15 '18 at 10:22

















      • It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

        – Santosh Kumar
        Nov 15 '18 at 9:45












      • I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

        – Mayank Porwal
        Nov 15 '18 at 9:46











      • Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

        – Santosh Kumar
        Nov 15 '18 at 9:59












      • I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

        – Mayank Porwal
        Nov 15 '18 at 10:03











      • Thanks mayank. Could you please explain the steps. It will be helpfull

        – Santosh Kumar
        Nov 15 '18 at 10:22
















      It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

      – Santosh Kumar
      Nov 15 '18 at 9:45






      It is working fine but it is just printing one line and not printing the second line. Output: $ cat output.txt zero@1-5

      – Santosh Kumar
      Nov 15 '18 at 9:45














      I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

      – Mayank Porwal
      Nov 15 '18 at 9:46





      I am writing the output in a file output.txt. If you just want to print it, check my updated answer.

      – Mayank Porwal
      Nov 15 '18 at 9:46













      Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

      – Santosh Kumar
      Nov 15 '18 at 9:59






      Thanks mayank. I tried it but again I am getting it as just one single line. Even I tried to print it in output.txt

      – Santosh Kumar
      Nov 15 '18 at 9:59














      I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

      – Mayank Porwal
      Nov 15 '18 at 10:03





      I pasted the tested solution. It works fine on my machine. Are you running it on the same file as you pasted in your post? Please check, there has to be something very simple that you might be missing.

      – Mayank Porwal
      Nov 15 '18 at 10:03













      Thanks mayank. Could you please explain the steps. It will be helpfull

      – Santosh Kumar
      Nov 15 '18 at 10:22





      Thanks mayank. Could you please explain the steps. It will be helpfull

      – Santosh Kumar
      Nov 15 '18 at 10:22













      1














      It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:



      $ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
      Text file content:
      zero#1-6
      one#1-3
      two#1-8


      Explained some:




      • s//-/ replace with a -


      • (?<=[0-9]) positive lookbehind, if preceeded by a number


      • (?=[0-9]) positive lookahead, if followed by a number





      share|improve this answer





























        1














        It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:



        $ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
        Text file content:
        zero#1-6
        one#1-3
        two#1-8


        Explained some:




        • s//-/ replace with a -


        • (?<=[0-9]) positive lookbehind, if preceeded by a number


        • (?=[0-9]) positive lookahead, if followed by a number





        share|improve this answer



























          1












          1








          1







          It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:



          $ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
          Text file content:
          zero#1-6
          one#1-3
          two#1-8


          Explained some:




          • s//-/ replace with a -


          • (?<=[0-9]) positive lookbehind, if preceeded by a number


          • (?=[0-9]) positive lookahead, if followed by a number





          share|improve this answer















          It's no shell scripting (missed that first, sorry) but using perl with combined lookahead and lookbehind for a number:



          $ perl -pe 's/(?<=[0-9]).*(?=[0-9])/-/' file
          Text file content:
          zero#1-6
          one#1-3
          two#1-8


          Explained some:




          • s//-/ replace with a -


          • (?<=[0-9]) positive lookbehind, if preceeded by a number


          • (?=[0-9]) positive lookahead, if followed by a number






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 10:51

























          answered Nov 15 '18 at 10:45









          James BrownJames Brown

          20.5k42037




          20.5k42037





















              1














              With sed:



              sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt



              • -r: using extented regular expressions (just to write some stuff without escaping them by a backslash)


              • s/expr1/expr2/: substitute expr1 by expr2


              • epxr1 is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).


              • epxr2 retrieves captured strings (1, 2, 3) and insert them in a formatted output (the one you wanted).

              Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.



              Update: Also you could do that with awk:



              awk -F'#' ' 
              gsub(/s*/,"", $2) ;
              print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
              ' < test.txt


              I added a gsub() call because your file seems to have trailing blank characters.






              share|improve this answer





























                1














                With sed:



                sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt



                • -r: using extented regular expressions (just to write some stuff without escaping them by a backslash)


                • s/expr1/expr2/: substitute expr1 by expr2


                • epxr1 is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).


                • epxr2 retrieves captured strings (1, 2, 3) and insert them in a formatted output (the one you wanted).

                Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.



                Update: Also you could do that with awk:



                awk -F'#' ' 
                gsub(/s*/,"", $2) ;
                print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
                ' < test.txt


                I added a gsub() call because your file seems to have trailing blank characters.






                share|improve this answer



























                  1












                  1








                  1







                  With sed:



                  sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt



                  • -r: using extented regular expressions (just to write some stuff without escaping them by a backslash)


                  • s/expr1/expr2/: substitute expr1 by expr2


                  • epxr1 is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).


                  • epxr2 retrieves captured strings (1, 2, 3) and insert them in a formatted output (the one you wanted).

                  Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.



                  Update: Also you could do that with awk:



                  awk -F'#' ' 
                  gsub(/s*/,"", $2) ;
                  print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
                  ' < test.txt


                  I added a gsub() call because your file seems to have trailing blank characters.






                  share|improve this answer















                  With sed:



                  sed -r 's/^(.+)#([0-9])[0-9]*([0-9])s*$/1@2-3/' readFile.txt



                  • -r: using extented regular expressions (just to write some stuff without escaping them by a backslash)


                  • s/expr1/expr2/: substitute expr1 by expr2


                  • epxr1 is described by a regular expression, relevant matching patterns are caught by 3 capturing groups (parenthesized ones).


                  • epxr2 retrieves captured strings (1, 2, 3) and insert them in a formatted output (the one you wanted).

                  Regular-Expressions.info seems to be interesting to start with them. Also you can check your own regexp with Regx101.com.



                  Update: Also you could do that with awk:



                  awk -F'#' ' 
                  gsub(/s*/,"", $2) ;
                  print $1 "@" substr($2, 1, 1) "-" substr($2, length($2), 1)
                  ' < test.txt


                  I added a gsub() call because your file seems to have trailing blank characters.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 11:01

























                  answered Nov 15 '18 at 9:51









                  AmessihelAmessihel

                  2,7091826




                  2,7091826



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315645%2fread-line-by-line-from-a-text-file-and-print-how-i-want-in-shell-scripting%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