Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this:

cn=user.blr.ou=blr.india.o=company

The line should be converted like this:

cn=user.blr,ou=blr.india,o=comapny

Was wondering how to do that using shell script.

When replacing use tr, if there are conditions to the replace try sed, if the conditions are too complex try awk, and if you need to generate a different format Perl is your freind.

 echo "cn=user.blr.ou=blr.india.o=company" | sed 's/\.\([a-zA-Z]\+=\)/,\1/g'

Another way with sed :

 $ echo "cn=user.blr.ou=blr.india.o=company.test"|sed ':x;s/\(=[^=]*\)\.\([^=.,]*=\)/\1,\2/;t x'
cn=user.blr,ou=blr.india,o=company.test
$

Jean-Pierre.

Hi Skrynesaver/aigles,

can you tell me the logic please. I am a beginner in the shell script area and it seems to very complex :frowning: . Any useful link would also be helpful.

The command simply tests the sed script by echoing the string through it.

echo "cn=user.blr.ou=blr.india.o=company" | sed 's/\.\([a-zA-Z]\+=\)/,\1/g'

So, taking a look at the sed substitution command:
sed invoke the stream editor, sed.
's/ prevent the shell interpreting anything with the single quote and invoke a substitution of everything between the first / slashes with what's between the second / slashes.
\. a literal dot character (un-escaped . matches any character)
\( begin a capture group, everything between the escaped parenthesis is stored as \1 (subsequent parenthesised groups would be saved as \2 etc...)
[a-zA-Z] any letter between a and z or A and Z (ie a letter of any case), the [ ] is known as a range and the expression will match any character in this range
\+= match the previous token (the range) more than once followed by a literal "="
\)/ End capture group and end expression to be substituted and begin expression to replace it with.
,\1 a comma and the group we captured in the LHS of the substitution.
/g end of substitution expression and global modifier so that this is done throughout the string.

The Linux documentation project has a sed primer available