Connecting Tech Pros Worldwide Forums | Help | Site Map

Linux SED (help on replacement)

Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#1: Feb 9 '09
Hello everyone,

I am trying to convert characters in a file using SED, I have already got the script working there is one very big problem. I would like to convert characters in the given file golobally i.e. all of its occurence, thus I am using /g tag at the end. Bu I have multiple combinations for replacement I would like to apply all the replaces one by one on the characters in the file rather than A replace command on the whole file one by one. To clear the question a bit, here is the example:

file contains text = "AAAA BBBB"

>sed "s/AA/BB/g;s/BB/GG/g" file
> GGGG GGGG

I would like to get "BBBB GGGG" instead, I can see that the replace is applied one by one to the whole file. I would like it not to apply the replace on already replaced matches. Is this possible at all?

I would really like to get your suggestions as I am stuck with a huge amount of work on this, if it is not possible in SED could someone suggest another way of tackling this issue please.

Thanks in advance

micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 137
#2: Feb 9 '09

re: Linux SED (help on replacement)


I don't know the answer, I just thought off a possible way to get to a solution.

what if you turn the sed string around?
> sed "s/BB/GG/g;s/AA/BB/g" file

Result:
micmast@plato:/tmp$ echo "AAAA BBBB" > file
micmast@plato:/tmp$ sed "s/BB/GG/g;s/AA/BB/g" file
BBBB GGGG
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#3: Feb 9 '09

re: Linux SED (help on replacement)


Hello, thanks for replying

I know this can work with moving around replace strings, but I ahve many replace strings and the the files I would like to update are also multiple. The above example was to simplify the behaviour I would like to achieve. I was wondering if there is anyway for sed to only replace the content once rather than replacing already replace matches, as I think it runs each match and replace case with the whole file one at a time.

I have no idea if the solutions exists for this or not. Almost all other utilities do the same thing with replacements as it is easiest way to do.

BTW, I am only considering character replacements in my actual work, so at each point only one character is required to be matched from all the cases i.e:

>sed "s/a/b/g" file

So the omplexity is less as all the match cases only have a character to match from the listed cases, Rather than a mixture of strings of different length to match.

If I do not find anything I may have to implement one myself :), hopefully I will find something.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#4: Feb 9 '09

re: Linux SED (help on replacement)


At last, I have to go for my own solution.

Although my implementation does not cover any fancy stuff, it does write to file with the updated characters and on the console as well. I have decided to keep the syntazx similar to SED i.e. if given -i as an option it updates the file otherwise writes on console and works like a charm.

I am attatching the code with this post, for others to use if they want.

Note: I have not done a lot of work on this tool so it might be full of bugs but I have tested it on my own cases and it works fine - "IT ONLY REPLACES SINGLE CHARACTERS"

The script file should have the following syntax:
characterToReplace>newCharacter
so for example: replacing m with g
m>g

Because I have not put many restrictions in this, I have allowed to use any chracter in the middle, even a space would be considered as a character. So treat yourself with anything in the middle of the two characters in the script file but make sure it is also a single character.

Any problems, I will try to help but no support guaranteed on this. ;-)

I forgot to mention, this C# code and it is console application.
Attached Files
File Type: txt SearchAndReplace.txt (3.9 KB, 38 views)
Reply


Similar Unix / Linux / BSD bytes