SED command - MAC address











up vote
0
down vote

favorite
1












I use the MAC address of the machine as userid and would like to update a file (display.txt) that will display the userid/expiry. How do I replace the expdate in display.txt with the date in expdate.txt corresponding to the MAC address.



I succeed with the userid part, with:



sed -i "s/user/$(ifconfig eth0 |
grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')/"
/user/id/display.txt > /dev/null




display.txt:



UserID: user
User expiring on expdate




expdate.txt:



user                          = 00:09:34:2C:66:AB
expdate = 2017-05-20
user = 00:09:34:29:86:6C
expdate = 2017-08-23




I would like to have:



display.txt:



UserID: 00:09:34:29:86:6C
User expiring on 2017-08-23









share|improve this question




















  • 2




    do you want to loop it through users and expiry dates. Or only one is enough?
    – Pappu
    Jan 1 '17 at 5:52










  • Which operating system do you use?
    – Cyrus
    Jan 1 '17 at 10:32










  • all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
    – Dick
    Jan 1 '17 at 11:51















up vote
0
down vote

favorite
1












I use the MAC address of the machine as userid and would like to update a file (display.txt) that will display the userid/expiry. How do I replace the expdate in display.txt with the date in expdate.txt corresponding to the MAC address.



I succeed with the userid part, with:



sed -i "s/user/$(ifconfig eth0 |
grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')/"
/user/id/display.txt > /dev/null




display.txt:



UserID: user
User expiring on expdate




expdate.txt:



user                          = 00:09:34:2C:66:AB
expdate = 2017-05-20
user = 00:09:34:29:86:6C
expdate = 2017-08-23




I would like to have:



display.txt:



UserID: 00:09:34:29:86:6C
User expiring on 2017-08-23









share|improve this question




















  • 2




    do you want to loop it through users and expiry dates. Or only one is enough?
    – Pappu
    Jan 1 '17 at 5:52










  • Which operating system do you use?
    – Cyrus
    Jan 1 '17 at 10:32










  • all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
    – Dick
    Jan 1 '17 at 11:51













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I use the MAC address of the machine as userid and would like to update a file (display.txt) that will display the userid/expiry. How do I replace the expdate in display.txt with the date in expdate.txt corresponding to the MAC address.



I succeed with the userid part, with:



sed -i "s/user/$(ifconfig eth0 |
grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')/"
/user/id/display.txt > /dev/null




display.txt:



UserID: user
User expiring on expdate




expdate.txt:



user                          = 00:09:34:2C:66:AB
expdate = 2017-05-20
user = 00:09:34:29:86:6C
expdate = 2017-08-23




I would like to have:



display.txt:



UserID: 00:09:34:29:86:6C
User expiring on 2017-08-23









share|improve this question















I use the MAC address of the machine as userid and would like to update a file (display.txt) that will display the userid/expiry. How do I replace the expdate in display.txt with the date in expdate.txt corresponding to the MAC address.



I succeed with the userid part, with:



sed -i "s/user/$(ifconfig eth0 |
grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')/"
/user/id/display.txt > /dev/null




display.txt:



UserID: user
User expiring on expdate




expdate.txt:



user                          = 00:09:34:2C:66:AB
expdate = 2017-05-20
user = 00:09:34:29:86:6C
expdate = 2017-08-23




I would like to have:



display.txt:



UserID: 00:09:34:29:86:6C
User expiring on 2017-08-23






sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 23:45









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Jan 1 '17 at 4:34









Dick

6




6








  • 2




    do you want to loop it through users and expiry dates. Or only one is enough?
    – Pappu
    Jan 1 '17 at 5:52










  • Which operating system do you use?
    – Cyrus
    Jan 1 '17 at 10:32










  • all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
    – Dick
    Jan 1 '17 at 11:51














  • 2




    do you want to loop it through users and expiry dates. Or only one is enough?
    – Pappu
    Jan 1 '17 at 5:52










  • Which operating system do you use?
    – Cyrus
    Jan 1 '17 at 10:32










  • all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
    – Dick
    Jan 1 '17 at 11:51








2




2




do you want to loop it through users and expiry dates. Or only one is enough?
– Pappu
Jan 1 '17 at 5:52




do you want to loop it through users and expiry dates. Or only one is enough?
– Pappu
Jan 1 '17 at 5:52












Which operating system do you use?
– Cyrus
Jan 1 '17 at 10:32




Which operating system do you use?
– Cyrus
Jan 1 '17 at 10:32












all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
– Dick
Jan 1 '17 at 11:51




all machines will download the same file and upon booting it will pull uses the mac address as the user and then find its expiring date from this file (expdate.txt).
– Dick
Jan 1 '17 at 11:51










2 Answers
2






active

oldest

votes

















up vote
0
down vote













I found a one-liner solution using grep and sed:



mac=$(ifconfig eth1 | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'); grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display.txt


and to validate it works with any mac address I tested with:



mac=$(echo "00:09:34:29:86:6C") ; grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display2.txt


I generated the output from scratch, if you need a replacement in an existing file, I suggest to use a templating language like mustache. The Bash version is on GitHub



However because it seems you need to re-use the mac address (for searching first and then for the output) I suggest to make a bash script
and it would be more re-usable.






share|improve this answer




























    up vote
    0
    down vote













    By concatenating sed outputs:



    sed -e 's/user/UserID/g' expdate.txt | sed -e 's/ //g' | sed -e 's/=/: /g' | sed -e 's/expdate:/User expiring on/g' > display.txt


    (It's pretty sure that this could be done also with only one sed)






    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: 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%2f334067%2fsed-command-mac-address%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








      up vote
      0
      down vote













      I found a one-liner solution using grep and sed:



      mac=$(ifconfig eth1 | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'); grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display.txt


      and to validate it works with any mac address I tested with:



      mac=$(echo "00:09:34:29:86:6C") ; grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display2.txt


      I generated the output from scratch, if you need a replacement in an existing file, I suggest to use a templating language like mustache. The Bash version is on GitHub



      However because it seems you need to re-use the mac address (for searching first and then for the output) I suggest to make a bash script
      and it would be more re-usable.






      share|improve this answer

























        up vote
        0
        down vote













        I found a one-liner solution using grep and sed:



        mac=$(ifconfig eth1 | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'); grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display.txt


        and to validate it works with any mac address I tested with:



        mac=$(echo "00:09:34:29:86:6C") ; grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display2.txt


        I generated the output from scratch, if you need a replacement in an existing file, I suggest to use a templating language like mustache. The Bash version is on GitHub



        However because it seems you need to re-use the mac address (for searching first and then for the output) I suggest to make a bash script
        and it would be more re-usable.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I found a one-liner solution using grep and sed:



          mac=$(ifconfig eth1 | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'); grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display.txt


          and to validate it works with any mac address I tested with:



          mac=$(echo "00:09:34:29:86:6C") ; grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display2.txt


          I generated the output from scratch, if you need a replacement in an existing file, I suggest to use a templating language like mustache. The Bash version is on GitHub



          However because it seems you need to re-use the mac address (for searching first and then for the output) I suggest to make a bash script
          and it would be more re-usable.






          share|improve this answer












          I found a one-liner solution using grep and sed:



          mac=$(ifconfig eth1 | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'); grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display.txt


          and to validate it works with any mac address I tested with:



          mac=$(echo "00:09:34:29:86:6C") ; grep "$mac" expdate.txt -A 1 | sed -r ':a;N;$!ba;s!user.*=.(..:..:..:..:..:..).*expdate.*=.(....-..-..)!UserID: 1nUser expiring on 2!g' > display2.txt


          I generated the output from scratch, if you need a replacement in an existing file, I suggest to use a templating language like mustache. The Bash version is on GitHub



          However because it seems you need to re-use the mac address (for searching first and then for the output) I suggest to make a bash script
          and it would be more re-usable.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 '17 at 11:46









          рüффп

          75831529




          75831529
























              up vote
              0
              down vote













              By concatenating sed outputs:



              sed -e 's/user/UserID/g' expdate.txt | sed -e 's/ //g' | sed -e 's/=/: /g' | sed -e 's/expdate:/User expiring on/g' > display.txt


              (It's pretty sure that this could be done also with only one sed)






              share|improve this answer

























                up vote
                0
                down vote













                By concatenating sed outputs:



                sed -e 's/user/UserID/g' expdate.txt | sed -e 's/ //g' | sed -e 's/=/: /g' | sed -e 's/expdate:/User expiring on/g' > display.txt


                (It's pretty sure that this could be done also with only one sed)






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  By concatenating sed outputs:



                  sed -e 's/user/UserID/g' expdate.txt | sed -e 's/ //g' | sed -e 's/=/: /g' | sed -e 's/expdate:/User expiring on/g' > display.txt


                  (It's pretty sure that this could be done also with only one sed)






                  share|improve this answer












                  By concatenating sed outputs:



                  sed -e 's/user/UserID/g' expdate.txt | sed -e 's/ //g' | sed -e 's/=/: /g' | sed -e 's/expdate:/User expiring on/g' > display.txt


                  (It's pretty sure that this could be done also with only one sed)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 '17 at 11:58









                  Zumo de Vidrio

                  1,283319




                  1,283319






























                      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.





                      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%2funix.stackexchange.com%2fquestions%2f334067%2fsed-command-mac-address%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

                      Morgemoulin

                      Scott Moir

                      Souastre