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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace last instance of . between two consecutive = sign by ,
# 1  
Old 01-17-2012
Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this:
Code:
cn=user.blr.ou=blr.india.o=company

The line should be converted like this:
Code:
cn=user.blr,ou=blr.india,o=comapny

Was wondering how to do that using shell script.

Moderator's Comments:
Mod Comment Please use [CODE][/CODE] tags where appropriate, thank you

Last edited by vbe; 01-17-2012 at 04:08 AM.. Reason: code tags
# 2  
Old 01-17-2012
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.
Code:
 echo "cn=user.blr.ou=blr.india.o=company" | sed 's/\.\([a-zA-Z]\+=\)/,\1/g'

# 3  
Old 01-17-2012
Another way with sed :
Code:
 $ 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.
# 4  
Old 01-17-2012
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 Smilie . Any useful link would also be helpful.
# 5  
Old 01-17-2012
The command simply tests the sed script by echoing the string through it.
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

2. Shell Programming and Scripting

Replace every second instance of delimeter

Hi, Need help on replacing every second instance of delimeter. Scenario: var="Name1,Value1,Name2,Value2,Name3,Value3,Name4,Value" I want every second "," to replace with "|" I tried like below echo $var| sed 's/,/|/2' But, it's not working. Expected output: ... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Replace second to last but one consecutive occurence

Hi Unix Mates, I have requirement where in which I have to replace second to last -1 occurence of space with a word and last occurence of a space with a word and a space,input would be a file. Desired result: Second consecutive to last-1 occurence of space replace with: HRU LAST... (5 Replies)
Discussion started by: Kingcobra
5 Replies

4. Shell Programming and Scripting

sed - How to replace right part of equal sign (=) on a line

Hello. Using a bash script , I have a variable name for the file I want to modify FILE_TO_EDIT="/etc/my_config_file"And I have a variable name for the parameter to change PARAMETER="fallback_node" PARAMETER_NEW_VALUE="http://my_server_name.com/new_path" A config file may contain : 1°)... (2 Replies)
Discussion started by: jcdole
2 Replies

5. Shell Programming and Scripting

New to Perl Mail @ sign search and replace in a file

HI I'm terribly new to perl .. I;ve been trying to use this command to search and replace entries in a file I tried this and it works perl -p -i -e 's/old/new/' filename Problem is that I have a list of email addresses and I need to serach and replace the entire email address as my... (5 Replies)
Discussion started by: mnassiri
5 Replies

6. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

7. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies

8. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

9. Shell Programming and Scripting

Sign on/Sign off logging script

I'd like to make a script that I can execute every time I sign on to my linux box that keeps track of the time and allows to me to add a remark to a file. So basically once I log in, I run the script, and it outputs the date and time to a text file (log.txt). But that isn't my problem. I need... (1 Reply)
Discussion started by: Glider
1 Replies

10. HP-UX

Replace $ sign within a file using Vi editor

Can someone help me with this command? I want to replace amount fields which are in the format $99.99 to 99.99 in a large data file which is within a HP box. I use Vi editor and tried this command :1,$s//$///g And this does not seem to work Also, I want to replace ^M with spaces in the... (1 Reply)
Discussion started by: dtonse
1 Replies
Login or Register to Ask a Question