473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Linux SED (help on replacement)

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
Feb 9 '09 #1
3 3506
micmast
144 100+
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
Feb 9 '09 #2
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.
Feb 9 '09 #3
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, 388 views)
Feb 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kay Schluehr | last post by:
Moin Maybe You can start somewhere with the Linux-kernel, the Python-Interpreter and the ctypes extension module and replace successively parts of the Linux-kernel by Python modules. So You...
6
by: gnu | last post by:
Rationale to use Linux ======================= - I can't afford paying for $199 for the license of an OS that's arguably better thank Linux for each of 10 computers I have. - I want to be...
21
by: Travis 'Bailo' Bickel | last post by:
Lately, I have been having a bear of a time trying to acquire a Linux driver for my S3/ProSavage chipset -- but that quest is now almost at an end as I have located a noble band of people who are...
3
by: Math55 | last post by:
hi, i have this piece of code: --- #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> /* baudrate settings are defined in...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
5
by: Alex | last post by:
Hi everyone, I'm looking into moving my desktop system at work to Linux from Windows XP, and one tool I've yet to find a suitable replacement for is MS SQL Enterprise Manager. I mainly need to...
6
by: mahesh | last post by:
Hi all, I am using Dev C++ IDE for the programming but for the time efficiency, I tried to use supercomputer (Linux) to run my program, but some of the function such as pow(double,double) ,...
7
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.