0

i'm making this replace

sed 's/<n3:CustId>.*<\/n3:CustId>/<n3:CustId>'"${orgkey}"'<\/n3:CustId>/' CAMBIOMINI.txt > CAMBIOMINI2.txt

but now i want to replace line by line with a differente orgkey value, i want orgkey+=1 but i dont know how to make that in the same command for all the CAMBIOMINI.txt file

2
  • I don't quite understand what you're asking. Can you rephrase the question?
    – tink
    Dec 16, 2018 at 0:29
  • 1
    edit your Q to include small sample input and your required output from that input. Also include your changing (?) values for $orgkey. Good luck.
    – shellter
    Dec 16, 2018 at 0:34

1 Answer 1

1

Sed may not be suitable for the case that you want to alter the substitution for each occurance. If my undersanding of your requirement is correct, following would work:

awk 'FNR==NR {orgkey[++i]=$0; next}
 {print gensub(/<n3:CustId>[^<]*<\/n3:CustId>/,"<n3:CustId>" orgkey[++j] "</n3:CustId>", "g")} ' orgkey.txt CAMBIOMINI1.txt

where orgkey.txt holds the list of substitutions:

orgkey_a
orgkey_b
orgkey_c
orgkey_d

and CAMBIOMINI1.txt will look like:

<n3:CustId>id1</n3:CustId>
<n3:CustId>id2</n3:CustId>
<n3:CustId>id3</n3:CustId>
<n3:CustId>id4</n3:CustId>

then the result will be:

<n3:CustId>orgkey_a</n3:CustId>
<n3:CustId>orgkey_b</n3:CustId>
<n3:CustId>orgkey_c</n3:CustId>
<n3:CustId>orgkey_d</n3:CustId>

Note that it does not assume the tag in CAMBIOMINI1.txt appears multiple times in the same line as:

<n3:CustId>id1</n3:CustId> <n3:CustId>id2</n3:CustId>
<n3:CustId>id3</n3:CustId>
<n3:CustId>id4</n3:CustId>

In that case, use a Perl version instead:

perl -nle 'if (@ARGV) {push(@orgkey, $_); next}
    s#<n3:CustId>.*?</n3:CustId>#"<n3:CustId>" . $orgkey[$j++] . "</n3:CustId>"#ge; print' orgkey.txt CAMBIOMINI1.txt
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.