Text file: find string, save string field to var, find 2nd string, replace field with var, repeat to end












0















I think this would best be done with AWK, but not sure. Its been stumping me all day how to do this. I have a text file with * delimiters between the fields on the lines. I need to search for lines beginning with L11*1Z and save the value to a variable or buffer starting with 1Z up to but not including the next * (ie the 2nd field on the line), in the first case this would be 1ZXDF430. Then I need to go to the next line that begins with BGN and replace the string QVD (ie the 3rd field on that line) with the value of the variable. I need to do this for all L11*1Z and following BGN lines found in the file. It would be good to output a new file as a result, if possible rather than overlay the input file.



Input file



xxx  
L11*123456*CR
yyy
L11*1ZXDF430*2I*04
zzz
BGN*00*QVD*123456
fff
L11*768907*CR
L11*12345678*CR
xxx
L11*1ZXDF499*2I*04
zzz
BGN*00*QVD*123456
xxx


Resulting output file



xxx  
L11*123456*CR
yyy
L11*1ZXDF430*2I*04
zzz
BGN*00*1ZXDF430*123456
fff
L11*768907*CR
L11*12345678*CR
xxx
L11*1ZXDF499*2I*04
zzz
BGN*00*1ZXDF499*123456
xxx









share|improve this question





























    0















    I think this would best be done with AWK, but not sure. Its been stumping me all day how to do this. I have a text file with * delimiters between the fields on the lines. I need to search for lines beginning with L11*1Z and save the value to a variable or buffer starting with 1Z up to but not including the next * (ie the 2nd field on the line), in the first case this would be 1ZXDF430. Then I need to go to the next line that begins with BGN and replace the string QVD (ie the 3rd field on that line) with the value of the variable. I need to do this for all L11*1Z and following BGN lines found in the file. It would be good to output a new file as a result, if possible rather than overlay the input file.



    Input file



    xxx  
    L11*123456*CR
    yyy
    L11*1ZXDF430*2I*04
    zzz
    BGN*00*QVD*123456
    fff
    L11*768907*CR
    L11*12345678*CR
    xxx
    L11*1ZXDF499*2I*04
    zzz
    BGN*00*QVD*123456
    xxx


    Resulting output file



    xxx  
    L11*123456*CR
    yyy
    L11*1ZXDF430*2I*04
    zzz
    BGN*00*1ZXDF430*123456
    fff
    L11*768907*CR
    L11*12345678*CR
    xxx
    L11*1ZXDF499*2I*04
    zzz
    BGN*00*1ZXDF499*123456
    xxx









    share|improve this question



























      0












      0








      0








      I think this would best be done with AWK, but not sure. Its been stumping me all day how to do this. I have a text file with * delimiters between the fields on the lines. I need to search for lines beginning with L11*1Z and save the value to a variable or buffer starting with 1Z up to but not including the next * (ie the 2nd field on the line), in the first case this would be 1ZXDF430. Then I need to go to the next line that begins with BGN and replace the string QVD (ie the 3rd field on that line) with the value of the variable. I need to do this for all L11*1Z and following BGN lines found in the file. It would be good to output a new file as a result, if possible rather than overlay the input file.



      Input file



      xxx  
      L11*123456*CR
      yyy
      L11*1ZXDF430*2I*04
      zzz
      BGN*00*QVD*123456
      fff
      L11*768907*CR
      L11*12345678*CR
      xxx
      L11*1ZXDF499*2I*04
      zzz
      BGN*00*QVD*123456
      xxx


      Resulting output file



      xxx  
      L11*123456*CR
      yyy
      L11*1ZXDF430*2I*04
      zzz
      BGN*00*1ZXDF430*123456
      fff
      L11*768907*CR
      L11*12345678*CR
      xxx
      L11*1ZXDF499*2I*04
      zzz
      BGN*00*1ZXDF499*123456
      xxx









      share|improve this question
















      I think this would best be done with AWK, but not sure. Its been stumping me all day how to do this. I have a text file with * delimiters between the fields on the lines. I need to search for lines beginning with L11*1Z and save the value to a variable or buffer starting with 1Z up to but not including the next * (ie the 2nd field on the line), in the first case this would be 1ZXDF430. Then I need to go to the next line that begins with BGN and replace the string QVD (ie the 3rd field on that line) with the value of the variable. I need to do this for all L11*1Z and following BGN lines found in the file. It would be good to output a new file as a result, if possible rather than overlay the input file.



      Input file



      xxx  
      L11*123456*CR
      yyy
      L11*1ZXDF430*2I*04
      zzz
      BGN*00*QVD*123456
      fff
      L11*768907*CR
      L11*12345678*CR
      xxx
      L11*1ZXDF499*2I*04
      zzz
      BGN*00*QVD*123456
      xxx


      Resulting output file



      xxx  
      L11*123456*CR
      yyy
      L11*1ZXDF430*2I*04
      zzz
      BGN*00*1ZXDF430*123456
      fff
      L11*768907*CR
      L11*12345678*CR
      xxx
      L11*1ZXDF499*2I*04
      zzz
      BGN*00*1ZXDF499*123456
      xxx






      linux text-processing awk sed command-line






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      steeldriver

      36.8k45287




      36.8k45287










      asked 1 hour ago









      user3193012user3193012

      31




      31






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Assuming there's a BGN after each L11*1Z, then you should be able to use



          $ awk 'BEGIN{OFS=FS="*"} /^L11*1Z/ {x = $2} /^BGN/ {$3 = x} 1' file
          xxx
          L11*123456*CR
          yyy
          L11*1ZXDF430*2I*04
          zzz
          BGN*00*1ZXDF430*123456
          fff
          L11*768907*CR
          L11*12345678*CR
          xxx
          L11*1ZXDF499*2I*04
          zzz
          BGN*00*1ZXDF499*123456
          xxx





          share|improve this answer























            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f505983%2ftext-file-find-string-save-string-field-to-var-find-2nd-string-replace-field%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














            Assuming there's a BGN after each L11*1Z, then you should be able to use



            $ awk 'BEGIN{OFS=FS="*"} /^L11*1Z/ {x = $2} /^BGN/ {$3 = x} 1' file
            xxx
            L11*123456*CR
            yyy
            L11*1ZXDF430*2I*04
            zzz
            BGN*00*1ZXDF430*123456
            fff
            L11*768907*CR
            L11*12345678*CR
            xxx
            L11*1ZXDF499*2I*04
            zzz
            BGN*00*1ZXDF499*123456
            xxx





            share|improve this answer




























              1














              Assuming there's a BGN after each L11*1Z, then you should be able to use



              $ awk 'BEGIN{OFS=FS="*"} /^L11*1Z/ {x = $2} /^BGN/ {$3 = x} 1' file
              xxx
              L11*123456*CR
              yyy
              L11*1ZXDF430*2I*04
              zzz
              BGN*00*1ZXDF430*123456
              fff
              L11*768907*CR
              L11*12345678*CR
              xxx
              L11*1ZXDF499*2I*04
              zzz
              BGN*00*1ZXDF499*123456
              xxx





              share|improve this answer


























                1












                1








                1







                Assuming there's a BGN after each L11*1Z, then you should be able to use



                $ awk 'BEGIN{OFS=FS="*"} /^L11*1Z/ {x = $2} /^BGN/ {$3 = x} 1' file
                xxx
                L11*123456*CR
                yyy
                L11*1ZXDF430*2I*04
                zzz
                BGN*00*1ZXDF430*123456
                fff
                L11*768907*CR
                L11*12345678*CR
                xxx
                L11*1ZXDF499*2I*04
                zzz
                BGN*00*1ZXDF499*123456
                xxx





                share|improve this answer













                Assuming there's a BGN after each L11*1Z, then you should be able to use



                $ awk 'BEGIN{OFS=FS="*"} /^L11*1Z/ {x = $2} /^BGN/ {$3 = x} 1' file
                xxx
                L11*123456*CR
                yyy
                L11*1ZXDF430*2I*04
                zzz
                BGN*00*1ZXDF430*123456
                fff
                L11*768907*CR
                L11*12345678*CR
                xxx
                L11*1ZXDF499*2I*04
                zzz
                BGN*00*1ZXDF499*123456
                xxx






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                steeldriversteeldriver

                36.8k45287




                36.8k45287






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f505983%2ftext-file-find-string-save-string-field-to-var-find-2nd-string-replace-field%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

                    Loup dans la culture

                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                    ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux