Shell script to match row number from one file and put it as comments in another file when match

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-1
down vote

favorite












I have two files. The content of both files has dynamic and generated by the system when required.



The first file contains the meaning for specific row number as below :



head simdb.txt 
MSISDN
Account_ID
COSP_ID
Currency
Language
Home_Zone
SIM_PIN
Screening_PIN
Third_ParAnothercess_PIN
Cumulative_Incorrect_PIN


Other file contains the dynamic data as below



head subscriber.txt
0='917598936722' 4='ENG' 6='1234'


Output should be like :



0='917598936722' //MSISDN
4='ENG' //Language
6='1234' //SIM_PIN


Question updated========



Adding to the above query, if subscriber.txt would have multiple lines then how can we have a script to print line first then it required output? For example, if we subscriber.txt file like below



head subscriber.txt
0='917598936722' 4='ENG' 6='1234'
0='919654680634' 4='ENG' 6='1234'


Then desired output be like :



0='917598936722' 4='ENG' 6='1234'

0='917598936722' //MSISDN
4='ENG' //Language
6='1234' //SIM_PIN


=========================================



0='919654680634' 4='ENG' 6='1234'

0='919654680634' //MSISDN
4='ENG' //Language
6='1234' //SIM_PIN






share|improve this question



























    up vote
    -1
    down vote

    favorite












    I have two files. The content of both files has dynamic and generated by the system when required.



    The first file contains the meaning for specific row number as below :



    head simdb.txt 
    MSISDN
    Account_ID
    COSP_ID
    Currency
    Language
    Home_Zone
    SIM_PIN
    Screening_PIN
    Third_ParAnothercess_PIN
    Cumulative_Incorrect_PIN


    Other file contains the dynamic data as below



    head subscriber.txt
    0='917598936722' 4='ENG' 6='1234'


    Output should be like :



    0='917598936722' //MSISDN
    4='ENG' //Language
    6='1234' //SIM_PIN


    Question updated========



    Adding to the above query, if subscriber.txt would have multiple lines then how can we have a script to print line first then it required output? For example, if we subscriber.txt file like below



    head subscriber.txt
    0='917598936722' 4='ENG' 6='1234'
    0='919654680634' 4='ENG' 6='1234'


    Then desired output be like :



    0='917598936722' 4='ENG' 6='1234'

    0='917598936722' //MSISDN
    4='ENG' //Language
    6='1234' //SIM_PIN


    =========================================



    0='919654680634' 4='ENG' 6='1234'

    0='919654680634' //MSISDN
    4='ENG' //Language
    6='1234' //SIM_PIN






    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have two files. The content of both files has dynamic and generated by the system when required.



      The first file contains the meaning for specific row number as below :



      head simdb.txt 
      MSISDN
      Account_ID
      COSP_ID
      Currency
      Language
      Home_Zone
      SIM_PIN
      Screening_PIN
      Third_ParAnothercess_PIN
      Cumulative_Incorrect_PIN


      Other file contains the dynamic data as below



      head subscriber.txt
      0='917598936722' 4='ENG' 6='1234'


      Output should be like :



      0='917598936722' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN


      Question updated========



      Adding to the above query, if subscriber.txt would have multiple lines then how can we have a script to print line first then it required output? For example, if we subscriber.txt file like below



      head subscriber.txt
      0='917598936722' 4='ENG' 6='1234'
      0='919654680634' 4='ENG' 6='1234'


      Then desired output be like :



      0='917598936722' 4='ENG' 6='1234'

      0='917598936722' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN


      =========================================



      0='919654680634' 4='ENG' 6='1234'

      0='919654680634' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN






      share|improve this question













      I have two files. The content of both files has dynamic and generated by the system when required.



      The first file contains the meaning for specific row number as below :



      head simdb.txt 
      MSISDN
      Account_ID
      COSP_ID
      Currency
      Language
      Home_Zone
      SIM_PIN
      Screening_PIN
      Third_ParAnothercess_PIN
      Cumulative_Incorrect_PIN


      Other file contains the dynamic data as below



      head subscriber.txt
      0='917598936722' 4='ENG' 6='1234'


      Output should be like :



      0='917598936722' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN


      Question updated========



      Adding to the above query, if subscriber.txt would have multiple lines then how can we have a script to print line first then it required output? For example, if we subscriber.txt file like below



      head subscriber.txt
      0='917598936722' 4='ENG' 6='1234'
      0='919654680634' 4='ENG' 6='1234'


      Then desired output be like :



      0='917598936722' 4='ENG' 6='1234'

      0='917598936722' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN


      =========================================



      0='919654680634' 4='ENG' 6='1234'

      0='919654680634' //MSISDN
      4='ENG' //Language
      6='1234' //SIM_PIN








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 30 at 11:07
























      asked Jul 30 at 5:37









      Prince Garg

      11




      11




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          awk 'NR==FNR
          Arr[NR-1]=$0;
          next
          for(i=1;i<=NF;i++)
          split($i,a,"=");
          print $i " //"Arr[a[1]]
          'simdb.txt subscriber.txt


          • read all the lines from simdb.txt and keep in array. Index starts from 0

          • read the second file and check what is the value before equal symbol

          • read the value from Arr and print it.





          share|improve this answer

















          • 1




            Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
            – Kusalananda
            Jul 30 at 7:44










          • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
            – Prince Garg
            Jul 30 at 8:54


















          up vote
          0
          down vote













          awk 'FNR==NR for (i=1; i<=NF; ++i) 
          split($i, a, "=")
          s[a[1]+1] = $i ; next
          FNR in s printf("%-20s//%sn", s[FNR], $0) ' subscriber.txt simdb.txt


          This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).



          When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.



          Output given the data in the question:



          0='917598936722' //MSISDN
          4='ENG' //Language
          6='1234' //SIM_PIN





          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',
            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%2f459255%2fshell-script-to-match-row-number-from-one-file-and-put-it-as-comments-in-another%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            awk 'NR==FNR
            Arr[NR-1]=$0;
            next
            for(i=1;i<=NF;i++)
            split($i,a,"=");
            print $i " //"Arr[a[1]]
            'simdb.txt subscriber.txt


            • read all the lines from simdb.txt and keep in array. Index starts from 0

            • read the second file and check what is the value before equal symbol

            • read the value from Arr and print it.





            share|improve this answer

















            • 1




              Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
              – Kusalananda
              Jul 30 at 7:44










            • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
              – Prince Garg
              Jul 30 at 8:54















            up vote
            0
            down vote













            awk 'NR==FNR
            Arr[NR-1]=$0;
            next
            for(i=1;i<=NF;i++)
            split($i,a,"=");
            print $i " //"Arr[a[1]]
            'simdb.txt subscriber.txt


            • read all the lines from simdb.txt and keep in array. Index starts from 0

            • read the second file and check what is the value before equal symbol

            • read the value from Arr and print it.





            share|improve this answer

















            • 1




              Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
              – Kusalananda
              Jul 30 at 7:44










            • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
              – Prince Garg
              Jul 30 at 8:54













            up vote
            0
            down vote










            up vote
            0
            down vote









            awk 'NR==FNR
            Arr[NR-1]=$0;
            next
            for(i=1;i<=NF;i++)
            split($i,a,"=");
            print $i " //"Arr[a[1]]
            'simdb.txt subscriber.txt


            • read all the lines from simdb.txt and keep in array. Index starts from 0

            • read the second file and check what is the value before equal symbol

            • read the value from Arr and print it.





            share|improve this answer













            awk 'NR==FNR
            Arr[NR-1]=$0;
            next
            for(i=1;i<=NF;i++)
            split($i,a,"=");
            print $i " //"Arr[a[1]]
            'simdb.txt subscriber.txt


            • read all the lines from simdb.txt and keep in array. Index starts from 0

            • read the second file and check what is the value before equal symbol

            • read the value from Arr and print it.






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jul 30 at 7:06









            Kamaraj

            2,5341312




            2,5341312







            • 1




              Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
              – Kusalananda
              Jul 30 at 7:44










            • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
              – Prince Garg
              Jul 30 at 8:54













            • 1




              Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
              – Kusalananda
              Jul 30 at 7:44










            • Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
              – Prince Garg
              Jul 30 at 8:54








            1




            1




            Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
            – Kusalananda
            Jul 30 at 7:44




            Due to the linebreak after NR==FNR, this would print all lines from the first file (and nothing else). You also miss a space before the first filename.
            – Kusalananda
            Jul 30 at 7:44












            Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
            – Prince Garg
            Jul 30 at 8:54





            Thanks Kamaraj. After implementing suggestion from Kusalananda, the command is working fine
            – Prince Garg
            Jul 30 at 8:54













            up vote
            0
            down vote













            awk 'FNR==NR for (i=1; i<=NF; ++i) 
            split($i, a, "=")
            s[a[1]+1] = $i ; next
            FNR in s printf("%-20s//%sn", s[FNR], $0) ' subscriber.txt simdb.txt


            This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).



            When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.



            Output given the data in the question:



            0='917598936722' //MSISDN
            4='ENG' //Language
            6='1234' //SIM_PIN





            share|improve this answer

























              up vote
              0
              down vote













              awk 'FNR==NR for (i=1; i<=NF; ++i) 
              split($i, a, "=")
              s[a[1]+1] = $i ; next
              FNR in s printf("%-20s//%sn", s[FNR], $0) ' subscriber.txt simdb.txt


              This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).



              When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.



              Output given the data in the question:



              0='917598936722' //MSISDN
              4='ENG' //Language
              6='1234' //SIM_PIN





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                awk 'FNR==NR for (i=1; i<=NF; ++i) 
                split($i, a, "=")
                s[a[1]+1] = $i ; next
                FNR in s printf("%-20s//%sn", s[FNR], $0) ' subscriber.txt simdb.txt


                This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).



                When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.



                Output given the data in the question:



                0='917598936722' //MSISDN
                4='ENG' //Language
                6='1234' //SIM_PIN





                share|improve this answer













                awk 'FNR==NR for (i=1; i<=NF; ++i) 
                split($i, a, "=")
                s[a[1]+1] = $i ; next
                FNR in s printf("%-20s//%sn", s[FNR], $0) ' subscriber.txt simdb.txt


                This first reads the fields in the shorter subscriber.txt file and assigns their values to an array s. The s array is keyed on the number in front of = in each field (plus one).



                When the simdb.txt file is read, a test is made on the line number in that file. If that line number is a key in our array s, the desired output is created. The output is done with printf and a formatting string that allocates 20 characters for a left-justified string (the data from simdb.txt) followed by // and the data saved from subscriber.txt for that particular row.



                Output given the data in the question:



                0='917598936722' //MSISDN
                4='ENG' //Language
                6='1234' //SIM_PIN






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 30 at 7:28









                Kusalananda

                101k13199311




                101k13199311






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459255%2fshell-script-to-match-row-number-from-one-file-and-put-it-as-comments-in-another%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)