简体   繁体   中英

Replace a word using sed from a file linux

My case is here.

I have file that contains #file_user

mark
sebastian
lewis
shumacher
hulkenburg

I have another file contains Add.ldif

dn: cn=salespartner,ou=Groups,o=company
changetype: modify
add: member
member: uid=user,ou=external,c=de,ou=People,o=company
member: uid=user,ou=external,c=de,ou=People,o=company
member: uid=user,ou=external,c=de,ou=People,o=company
member: uid=user,ou=external,c=de,ou=People,o=company

In the above i want to change the uid=user to the respective file user names

example

dn: cn=typo3_frontend_global_salespartner,ou=Groups,o=company
changetype: modify
add: member
member: uid=mark,ou=external,c=de,ou=People,o=company
member: uid=sebastian,ou=external,c=de,ou=People,o=company
member: uid=lewis,ou=external,c=de,ou=People,o=company

I tried using this but i couldn't acheive the above example.

#!/bin/bash
set -x
for q in `cat user`
do  sed 's/user/'${q}'/g' add.ldif > out
done 

Please suggest me how to do this. Meanwhile i'll understand advanced bash scripting

This very urgent for my work in ldap.

Thank in advance!!!!! Puspharaj

sed is probably not the best tool for this, since you don't want to make the same substitution on all lines. awk can do it better:

awk 'BEGIN { i = 0; j = 0; }
     FNR == NR { users[i++] = $0 }
     FNR != NR && /^member:/ && users[j] { sub("uid=user", "uid=" users[j++]); print }
     FNR != NR && !/^member:/ {print}' user add.ldif > out

This awk should work:

awk -F, 'FNR==NR{a[NR]=$1;next} $1~"user"{sub(/=user/, "=" a[++i], $1)} 1' OFS=, user add.ltif
changetype: modify
add: member
member: uid=mark,ou=external,c=de,ou=People,o=company
member: uid=sebastian,ou=external,c=de,ou=People,o=company
member: uid=lewis,ou=external,c=de,ou=People,o=company
member: uid=shumacher,ou=external,c=de,ou=People,o=company

You have two problems with your script:

The first is that you use the > operator which will produce unwanted results for your use in a loop. It means that for each user in the file, you run sed and overwrite the contents of out , so I'm guessing that in the above example all the 'member:' lines will contain user=hulkenburg. You may want to use the >> operator instead, which appends to a file instead of overwriting it. But it is still useless, for the second reason:

You are using sed, which runs through the entire file and replaces all instances of user to whatever's in $q . Even if you run with the append operator, you'll end up with a out file which looks like this:

dn: cn=typo3_frontend_global_salespartner,ou=Groups,o=company
changetype: modify
add: member
member: uid=mark,ou=external,c=de,ou=People,o=company
member: uid=mark,ou=external,c=de,ou=People,o=company
member: uid=mark,ou=external,c=de,ou=People,o=company

dn: cn=typo3_frontend_global_salespartner,ou=Groups,o=company
changetype: modify
add: member
member: uid=sebastian,ou=external,c=de,ou=People,o=company
member: uid=sebastian,ou=external,c=de,ou=People,o=company
member: uid=sebastian,ou=external,c=de,ou=People,o=company

Which is probably not what you want. I suggest 2 solutions:

keep a ldif file for each user, by changing the sed line as follows:

do  sed 's/user/'${q}'/g' add.ldif > out.${q}

You'll have to change add.ldif as well to contain only 1 member: line.

Or, build the ldif file in a script. Have the contents of add.ldif look like this:

dn: cn=salespartner,ou=Groups,o=company
changetype: modify
add: member

Then, run the following script:

#!/bin/bash -x
for q in `cat user`
    do echo "member: uid=${q},ou=external,c=de,ou=People,o=company" >> add.ldif
done

I believe that the second solution is more elegant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM