Reputation: 1
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; }
Upvotes: 0
Views: 83
Reputation: 2724
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.
Upvotes: 1
Reputation: 2471
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
Upvotes: 0
Reputation: 92854
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 farFNR
- the number of records that have been read so far from the current input fileThe 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; }
Upvotes: 0
Reputation: 189487
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
Upvotes: 0