0

I am having a trouble replacing string coming from a set of string from a File So here's how I would like my script to run

I have these files: macadd.file

00:90:8F:56:63:88
00:90:8F:56:63:77
00:90:8F:56:63:6B
00:90:8F:56:63:86
00:90:8F:56:63:87
00:90:8F:56:64:E5

test.file

host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }

So I've tried replacing it using

for i in $(cat macadd); do sed -i "s/00:90:8F:56:63:88/$i/g" test;done

However, it only changes it from the first string in macadd.file You can see that 00:90:8F:56:63:88 is all the entry there. What I would like to happen is like this, the result:

host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:77; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:6B; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:86; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:87; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:64:E5; fixed-address 192.168.10.29; }
1

4 Answers 4

1

Actually for the example you present in the question something without the substitution would be more simple and clear:

<macadd.file xargs -I{} printf "host Accountant { hardware ethernet %s; fixed-address 192.168.10.29; }\n" {}

That is for every line in the input file call printf to substitute only the MAC address part. More trouble comes when you have more MAC addresses and more resulting IP addresses.

Sign up to request clarification or add additional context in comments.

Comments

0

The first pass across causes sed to replace all the hits with the first value. If that's not what you want, use a different sed script. Perhaps like this:

sed 's!.*!s/00:90:8F:56:63:88/&/;n;!' macadd.file

This produces on standard output a new sed script, which you can pass to (... pause ...) sed.

sed 's!.*!s/00:90:8F:56:63:88/&/;n;' macadd.file |
sed -f - -i test.file

This is a slightly demanding pattern the first time you see it (and sed -f - does not work on all platforms) but once you get the idea, having a script generate a script is a very powerful and versatile tool for your toolbox.

... On MacOS, you can't use sed -f - and also -i requires an argument. If you have Bash (or some other shell which supports process substitution), here's a workaround:

sed -f <(sed 's!.*!s/00:90:8F:56:63:88/&/;n;!' macadd.file) -i '' test.file

Comments

0

Straightforwardly with AWK:

awk 'NR==FNR{ a[NR]=$1; next }
     a[FNR]{ sub("00:90:8F:56:63:88", a[FNR]) }1' macadd tesfile > tmp_f && mv tmp_f testfile
  • NR - the total number of input records read so far
  • FNR - the number of records that have been read so far from the current input file

The final testfile contents:

host Accountant { hardware ethernet 00:90:8F:56:63:88; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:77; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:6B; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:86; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:63:87; fixed-address 192.168.10.29; }
host Accountant { hardware ethernet 00:90:8F:56:64:E5; fixed-address 192.168.10.29; }

3 Comments

The OP is unclear on whether the number of MAC addresses is lower than the number of lines in the test file. I have assumed that they should be replaced in a rotating fashion; this script just runs out of MAC addresses, and replaces them with an empty string if the target file is longer. (Easy to fix; just add a modulo calculation to the index subscript.)
@tripleee, if the target file is "longer", then no replacement will be made, it's obvious
I thought it obvious that that would be undesirable, but anyway, it's easy to select one behavior or the other with this answer.
0

If MAC adresses are not always the same in test.file You can try :

sed -E '
s/((([[:xdigit:]]){2}:){5}[[:xdigit:]]{2})(.*)((([[:xdigit:]]){2}:){5}[[:xdigit:]]{2})(.*)/\4\1\8/
# ((([[:xdigit:]]){2}:){5}[[:xdigit:]]{2}) catch the MAC addresse
s/^\t//
# remove tab which come from paste
/^((([[:xdigit:]]){2}:){5}[[:xdigit:]]{2})/d
# remove lines if maccad.file have more lines than test.file
'<<< $(paste macadd.file test.file)>test.file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.