How do I transfer certain parts of the file to another file?












1














I have a FILE1



NAMES START

Hi, How are you?
Good to see you

start
Aaron
Kyle
Robert
stop

Official use only
Stamps here

start
Riley
Gayle
stop

Bicycles here
United Pawns

start
Alex
Ford
Sergio
stop

NAMES STOP


Here's what I wanna do:



If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.



So FILE2 should look like this:



Aaron
Kyle
Robert
Riley
Gayle
Alex
Ford
Sergio


Help please!










share|improve this question



























    1














    I have a FILE1



    NAMES START

    Hi, How are you?
    Good to see you

    start
    Aaron
    Kyle
    Robert
    stop

    Official use only
    Stamps here

    start
    Riley
    Gayle
    stop

    Bicycles here
    United Pawns

    start
    Alex
    Ford
    Sergio
    stop

    NAMES STOP


    Here's what I wanna do:



    If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.



    So FILE2 should look like this:



    Aaron
    Kyle
    Robert
    Riley
    Gayle
    Alex
    Ford
    Sergio


    Help please!










    share|improve this question

























      1












      1








      1


      0





      I have a FILE1



      NAMES START

      Hi, How are you?
      Good to see you

      start
      Aaron
      Kyle
      Robert
      stop

      Official use only
      Stamps here

      start
      Riley
      Gayle
      stop

      Bicycles here
      United Pawns

      start
      Alex
      Ford
      Sergio
      stop

      NAMES STOP


      Here's what I wanna do:



      If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.



      So FILE2 should look like this:



      Aaron
      Kyle
      Robert
      Riley
      Gayle
      Alex
      Ford
      Sergio


      Help please!










      share|improve this question













      I have a FILE1



      NAMES START

      Hi, How are you?
      Good to see you

      start
      Aaron
      Kyle
      Robert
      stop

      Official use only
      Stamps here

      start
      Riley
      Gayle
      stop

      Bicycles here
      United Pawns

      start
      Alex
      Ford
      Sergio
      stop

      NAMES STOP


      Here's what I wanna do:



      If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.



      So FILE2 should look like this:



      Aaron
      Kyle
      Robert
      Riley
      Gayle
      Alex
      Ford
      Sergio


      Help please!







      grep






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 4 '18 at 22:04









      ShellyBelly

      656




      656






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Getting content between named blocks of text



          Lets address getting the contents inside start..stop block because that's the major task.



          grep is line-matching tool, hence matching across multiple lines is difficult and grep is usually not used for such task, though it's not impossible. However, best approach in such case is to use awk to get patterns between specific lines, and then filter out the start and stop flags:



          $ awk '/start/,/stop/' input.txt | grep -v 'start|stop'
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          If we want to get rid of pipeline, we can do:



          # {print} is actually unnecessary, as default for awk is to print if
          # expression evaluates to true, so it's enough to have
          # awk '/start/{flag=1;next};/stop/{flag=0};flag' input.txt
          $ awk '/start/{flag=1;next};/stop/{flag=0};flag == 1 {print}' input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          There's of course other methods, such as sed or perl. As for grep, it's often suggested to use grep -Pzo flags, however probably due to multiple occurrences of start..block it doesn't work properly ( only one match returned):



          $ grep -zPo --color 'startn(.*n.*)nstop' input.txt
          start
          Riley
          Gayle
          stop




          For the sake of simplicity (though arguably awk example is the simplest) and to avoid dealing with regex patterns, we can also resort to basic scripting:



          #!/bin/bash

          printline=0
          while IFS= read -r line; do

          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1"


          And here's how the script works:



          $ chmod +x readblocks.sh
          $ ./readblocks.sh input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio




          Checking for NAMES START and transfering




          If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.




          Well, that's just grep 'NAMES START' input.txt. So we can check for that via



          if grep -q 'NAMES START' input.txt; then
          # whatever method you like goes here
          fi


          Looking at your example, NAMES START is the first line of the file. So we can also check for that - check first line as we read the file, instead of opening the file inside if statement suggested above.



          Transferring the contents to FILE2 - that's just adding > FILE2.txt to the original command or script you use.



          With these suggestions, awk command becomes:



          $ awk 'NR==1 && $0 != "NAMES START"{exit};/start/{flag=1;next};/stop/{flag=0};flag' input.txt > FILE2.txt


          And the script:



          #!/bin/bash

          printline=0
          linecounter=0
          while IFS= read -r line; do
          linecounter=$((linecounter+1))

          case "$line" in
          "NAMES START") continue;;
          *) exit 1;
          esac


          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1" > FILE2.txt





          share|improve this answer























          • You rock! Thank you for your help. I could solve the issue
            – ShellyBelly
            Dec 6 '18 at 14:31













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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%2faskubuntu.com%2fquestions%2f1098503%2fhow-do-i-transfer-certain-parts-of-the-file-to-another-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Getting content between named blocks of text



          Lets address getting the contents inside start..stop block because that's the major task.



          grep is line-matching tool, hence matching across multiple lines is difficult and grep is usually not used for such task, though it's not impossible. However, best approach in such case is to use awk to get patterns between specific lines, and then filter out the start and stop flags:



          $ awk '/start/,/stop/' input.txt | grep -v 'start|stop'
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          If we want to get rid of pipeline, we can do:



          # {print} is actually unnecessary, as default for awk is to print if
          # expression evaluates to true, so it's enough to have
          # awk '/start/{flag=1;next};/stop/{flag=0};flag' input.txt
          $ awk '/start/{flag=1;next};/stop/{flag=0};flag == 1 {print}' input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          There's of course other methods, such as sed or perl. As for grep, it's often suggested to use grep -Pzo flags, however probably due to multiple occurrences of start..block it doesn't work properly ( only one match returned):



          $ grep -zPo --color 'startn(.*n.*)nstop' input.txt
          start
          Riley
          Gayle
          stop




          For the sake of simplicity (though arguably awk example is the simplest) and to avoid dealing with regex patterns, we can also resort to basic scripting:



          #!/bin/bash

          printline=0
          while IFS= read -r line; do

          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1"


          And here's how the script works:



          $ chmod +x readblocks.sh
          $ ./readblocks.sh input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio




          Checking for NAMES START and transfering




          If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.




          Well, that's just grep 'NAMES START' input.txt. So we can check for that via



          if grep -q 'NAMES START' input.txt; then
          # whatever method you like goes here
          fi


          Looking at your example, NAMES START is the first line of the file. So we can also check for that - check first line as we read the file, instead of opening the file inside if statement suggested above.



          Transferring the contents to FILE2 - that's just adding > FILE2.txt to the original command or script you use.



          With these suggestions, awk command becomes:



          $ awk 'NR==1 && $0 != "NAMES START"{exit};/start/{flag=1;next};/stop/{flag=0};flag' input.txt > FILE2.txt


          And the script:



          #!/bin/bash

          printline=0
          linecounter=0
          while IFS= read -r line; do
          linecounter=$((linecounter+1))

          case "$line" in
          "NAMES START") continue;;
          *) exit 1;
          esac


          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1" > FILE2.txt





          share|improve this answer























          • You rock! Thank you for your help. I could solve the issue
            – ShellyBelly
            Dec 6 '18 at 14:31


















          1














          Getting content between named blocks of text



          Lets address getting the contents inside start..stop block because that's the major task.



          grep is line-matching tool, hence matching across multiple lines is difficult and grep is usually not used for such task, though it's not impossible. However, best approach in such case is to use awk to get patterns between specific lines, and then filter out the start and stop flags:



          $ awk '/start/,/stop/' input.txt | grep -v 'start|stop'
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          If we want to get rid of pipeline, we can do:



          # {print} is actually unnecessary, as default for awk is to print if
          # expression evaluates to true, so it's enough to have
          # awk '/start/{flag=1;next};/stop/{flag=0};flag' input.txt
          $ awk '/start/{flag=1;next};/stop/{flag=0};flag == 1 {print}' input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          There's of course other methods, such as sed or perl. As for grep, it's often suggested to use grep -Pzo flags, however probably due to multiple occurrences of start..block it doesn't work properly ( only one match returned):



          $ grep -zPo --color 'startn(.*n.*)nstop' input.txt
          start
          Riley
          Gayle
          stop




          For the sake of simplicity (though arguably awk example is the simplest) and to avoid dealing with regex patterns, we can also resort to basic scripting:



          #!/bin/bash

          printline=0
          while IFS= read -r line; do

          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1"


          And here's how the script works:



          $ chmod +x readblocks.sh
          $ ./readblocks.sh input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio




          Checking for NAMES START and transfering




          If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.




          Well, that's just grep 'NAMES START' input.txt. So we can check for that via



          if grep -q 'NAMES START' input.txt; then
          # whatever method you like goes here
          fi


          Looking at your example, NAMES START is the first line of the file. So we can also check for that - check first line as we read the file, instead of opening the file inside if statement suggested above.



          Transferring the contents to FILE2 - that's just adding > FILE2.txt to the original command or script you use.



          With these suggestions, awk command becomes:



          $ awk 'NR==1 && $0 != "NAMES START"{exit};/start/{flag=1;next};/stop/{flag=0};flag' input.txt > FILE2.txt


          And the script:



          #!/bin/bash

          printline=0
          linecounter=0
          while IFS= read -r line; do
          linecounter=$((linecounter+1))

          case "$line" in
          "NAMES START") continue;;
          *) exit 1;
          esac


          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1" > FILE2.txt





          share|improve this answer























          • You rock! Thank you for your help. I could solve the issue
            – ShellyBelly
            Dec 6 '18 at 14:31
















          1












          1








          1






          Getting content between named blocks of text



          Lets address getting the contents inside start..stop block because that's the major task.



          grep is line-matching tool, hence matching across multiple lines is difficult and grep is usually not used for such task, though it's not impossible. However, best approach in such case is to use awk to get patterns between specific lines, and then filter out the start and stop flags:



          $ awk '/start/,/stop/' input.txt | grep -v 'start|stop'
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          If we want to get rid of pipeline, we can do:



          # {print} is actually unnecessary, as default for awk is to print if
          # expression evaluates to true, so it's enough to have
          # awk '/start/{flag=1;next};/stop/{flag=0};flag' input.txt
          $ awk '/start/{flag=1;next};/stop/{flag=0};flag == 1 {print}' input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          There's of course other methods, such as sed or perl. As for grep, it's often suggested to use grep -Pzo flags, however probably due to multiple occurrences of start..block it doesn't work properly ( only one match returned):



          $ grep -zPo --color 'startn(.*n.*)nstop' input.txt
          start
          Riley
          Gayle
          stop




          For the sake of simplicity (though arguably awk example is the simplest) and to avoid dealing with regex patterns, we can also resort to basic scripting:



          #!/bin/bash

          printline=0
          while IFS= read -r line; do

          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1"


          And here's how the script works:



          $ chmod +x readblocks.sh
          $ ./readblocks.sh input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio




          Checking for NAMES START and transfering




          If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.




          Well, that's just grep 'NAMES START' input.txt. So we can check for that via



          if grep -q 'NAMES START' input.txt; then
          # whatever method you like goes here
          fi


          Looking at your example, NAMES START is the first line of the file. So we can also check for that - check first line as we read the file, instead of opening the file inside if statement suggested above.



          Transferring the contents to FILE2 - that's just adding > FILE2.txt to the original command or script you use.



          With these suggestions, awk command becomes:



          $ awk 'NR==1 && $0 != "NAMES START"{exit};/start/{flag=1;next};/stop/{flag=0};flag' input.txt > FILE2.txt


          And the script:



          #!/bin/bash

          printline=0
          linecounter=0
          while IFS= read -r line; do
          linecounter=$((linecounter+1))

          case "$line" in
          "NAMES START") continue;;
          *) exit 1;
          esac


          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1" > FILE2.txt





          share|improve this answer














          Getting content between named blocks of text



          Lets address getting the contents inside start..stop block because that's the major task.



          grep is line-matching tool, hence matching across multiple lines is difficult and grep is usually not used for such task, though it's not impossible. However, best approach in such case is to use awk to get patterns between specific lines, and then filter out the start and stop flags:



          $ awk '/start/,/stop/' input.txt | grep -v 'start|stop'
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          If we want to get rid of pipeline, we can do:



          # {print} is actually unnecessary, as default for awk is to print if
          # expression evaluates to true, so it's enough to have
          # awk '/start/{flag=1;next};/stop/{flag=0};flag' input.txt
          $ awk '/start/{flag=1;next};/stop/{flag=0};flag == 1 {print}' input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio


          There's of course other methods, such as sed or perl. As for grep, it's often suggested to use grep -Pzo flags, however probably due to multiple occurrences of start..block it doesn't work properly ( only one match returned):



          $ grep -zPo --color 'startn(.*n.*)nstop' input.txt
          start
          Riley
          Gayle
          stop




          For the sake of simplicity (though arguably awk example is the simplest) and to avoid dealing with regex patterns, we can also resort to basic scripting:



          #!/bin/bash

          printline=0
          while IFS= read -r line; do

          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1"


          And here's how the script works:



          $ chmod +x readblocks.sh
          $ ./readblocks.sh input.txt
          Aaron
          Kyle
          Robert
          Riley
          Gayle
          Alex
          Ford
          Sergio




          Checking for NAMES START and transfering




          If "NAMES START" string is present, transfer the contents each "start" and "stop", to a new FILE2, leaving out the start and stop themselves in the new FILE2.




          Well, that's just grep 'NAMES START' input.txt. So we can check for that via



          if grep -q 'NAMES START' input.txt; then
          # whatever method you like goes here
          fi


          Looking at your example, NAMES START is the first line of the file. So we can also check for that - check first line as we read the file, instead of opening the file inside if statement suggested above.



          Transferring the contents to FILE2 - that's just adding > FILE2.txt to the original command or script you use.



          With these suggestions, awk command becomes:



          $ awk 'NR==1 && $0 != "NAMES START"{exit};/start/{flag=1;next};/stop/{flag=0};flag' input.txt > FILE2.txt


          And the script:



          #!/bin/bash

          printline=0
          linecounter=0
          while IFS= read -r line; do
          linecounter=$((linecounter+1))

          case "$line" in
          "NAMES START") continue;;
          *) exit 1;
          esac


          # set flag for printing or not printing
          case $line in
          "start") printline=1; continue;;
          "stop") printline=0; continue;;
          esac

          # check the flag
          if [ "$printline" -eq 1 ]; then
          printf "%sn" "$line"
          fi

          # first positional parameter is the file we read
          done < "$1" > FILE2.txt






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 5 '18 at 22:10

























          answered Dec 5 '18 at 21:09









          Sergiy Kolodyazhnyy

          69.9k9144307




          69.9k9144307












          • You rock! Thank you for your help. I could solve the issue
            – ShellyBelly
            Dec 6 '18 at 14:31




















          • You rock! Thank you for your help. I could solve the issue
            – ShellyBelly
            Dec 6 '18 at 14:31


















          You rock! Thank you for your help. I could solve the issue
          – ShellyBelly
          Dec 6 '18 at 14:31






          You rock! Thank you for your help. I could solve the issue
          – ShellyBelly
          Dec 6 '18 at 14:31




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%2faskubuntu.com%2fquestions%2f1098503%2fhow-do-i-transfer-certain-parts-of-the-file-to-another-file%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

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?