LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2013, 08:29 PM   #1
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
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.
 
Old 11-07-2013, 08:35 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
for fname in *.ldif ; do sed '/userPassword/s/:/=/' $fname ; done
?
 
Old 11-07-2013, 08:51 PM   #3
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
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 =

Last edited by elalexluna83; 11-07-2013 at 08:52 PM.
 
Old 11-07-2013, 09:12 PM   #4
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-07-2013, 09:29 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Oops - sed uses anchors; "$" is end of line - maybe
Code:
sed '/userPassword/ s/:$/=/'
More generic would be to use ".$"
 
Old 11-07-2013, 11:17 PM   #6
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
Thank you. I will give it a shot. Let you know.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED help with first character in line. mactruck Linux - Newbie 3 01-11-2013 11:39 AM
[SOLVED] Sed insert before character VladC Linux - Newbie 3 03-15-2012 12:44 AM
[SOLVED] sed substitute everything until character sqn Programming 5 03-30-2010 10:27 AM
sed to change character in text file only once. pobrika Programming 7 09-09-2009 07:43 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration