LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   change last character using sed (https://www.linuxquestions.org/questions/programming-9/change-last-character-using-sed-4175483803/)

elalexluna83 11-07-2013 08:29 PM

change last character using sed
 
Hi everyone.

I have a bunch of files like the following one:
Code:

dn: Identifier=3AM9,c=MX,l=world
objectclass: top
objectclass: IRPerson
uid: user1@mail.com
c: MX
ispasswordexpired: false
passwordlastchangedatetime: 20130211195022
userPassword: {SHA}fGphxo74ubawYbKMNIvB7Xkhy1M:
businesscategory: 1
Identifier: 3AM9
sn: last
cn: user1 last
mail: user1@mail.com

i need to change last character from userPassword : to =
e.g.
Code:

userPassword: {SHA}fGphxo74ubawYbKMNIvB7Xkhy1M=
so far i'm only able to change the first :

Code:

$ sed '/userPassword/s/:/=/' file.ldif

dn: Identifier=3AM9,c=MX,l=world
objectclass: top
objectclass: IRPerson
uid: user1@mail.com
c: MX
ispasswordexpired: false
passwordlastchangedatetime: 20130211195022
userPassword= {SHA}fGphxo74ubawYbKMNIvB7Xkhy1M:
businesscategory: 1
Identifier: 3AM9
sn: last
cn: user1 last
mail: user1@mail.com

Any idea?, btw, userPassword can be at the top/middle of botton of the file.

syg00 11-07-2013 08:35 PM

Code:

for fname in *.ldif ; do sed '/userPassword/s/:/=/' $fname ; done
?

elalexluna83 11-07-2013 08:51 PM

Hey syg00, thaks for your quick response.

I think i didn't explain myself well. the loop is not the problem, the problem here is that my sed command is not right.

if you look close to the output my sed command's output is:
Code:

userPassword= {SHA}fGphxo74ubawYbKMNIvB7Xkhy1M:
but i want
Code:

userPassword: {SHA}fGphxo74ubawYbKMNIvB7Xkhy1M=
i need to change the last character of the line from : to =

elalexluna83 11-07-2013 09:12 PM

I believe i found the answer
Code:

for i in *.ldif ; do sed '/userPassword/s/M:/M=/g' $i > "$i"_.txt; done
Pls let me know if anyone has a better idea.

syg00 11-07-2013 09:29 PM

Oops - sed uses anchors; "$" is end of line - maybe
Code:

sed '/userPassword/ s/:$/=/'
More generic would be to use ".$"

elalexluna83 11-07-2013 11:17 PM

Thank you. I will give it a shot. Let you know.


All times are GMT -5. The time now is 10:32 PM.