0

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
3
  • 2
    do you want to loop it through users and expiry dates. Or only one is enough? Commented Jan 1, 2017 at 5:52
  • Which operating system do you use? Commented Jan 1, 2017 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). Commented Jan 1, 2017 at 11:51

2 Answers 2

0

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: \1\nUser 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: \1\nUser 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.

0

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)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.