Replace a string with a corresponding string.

Commander FAT

2[H]4U
Joined
Nov 23, 2000
Messages
3,565
I need a little help at work.

I have to sift through a large amount of text looking for instances of a particular string and then replacing it with a different string. The blocks of text are in no particular form or fashion. It's not standardized and doesn't follow any particular template. It's just blobs of text.

In reality I have to update old circuit IDs with new circuit IDs. There are about 1,400 updates that need to be made.

I have a text file of the old ID in one column with the new ID in a second column.

I'm using Linux and BASH as my terminal.

My first attempt was really sloppy and it didn't work. It consisted of a separate sed for each and every circuit ID replacement. I tried to use a while read loop to make all the changes. Didn't work. Noob move I know. I normally don't code at work. When I do it works but it's usually ugly as sin.

I just need a few hints or clues on the best way to do this.

Thanks
 
What languages do you know? I'm sure you can find examples in most common ones online. Write a program that takes the the file as an input, lookup the OI API for whatever language, and maybe the String API and that should get you going. Also look into regex if you can't find an easy pattern matching or String replace API in whatever language you are using..

http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
http://www.tutorialspoint.com/java/lang/string_replaceall.htm

Just an idea I have no idea what langauge your using. Essentially it could be done in a small java program, although you would want to use a BufferedReader to loop through each line in the file.
 
Last edited:
Is it effectively plaintext? If so, use Sublime text to highlight the string and then do a replace on it in all locations. Use some regex if needed as well to ensure it's all proper. I do this sort of thing all the time. It works with entire folders of files as well.
 
I have a text file of the old ID in one column with the new ID in a second column.
I'd just run this file through sed, or your text editor's search/replace, to turn it into a list of sed commands.

i.e. Replace each "<old_id>,<new_id>" with "sed -i 's/<old_id>/<new_id>/g' <input_files>", and just paste the whole 1400-line script into the shell.
 
I'd just run this file through sed, or your text editor's search/replace, to turn it into a list of sed commands.

i.e. Replace each "<old_id>,<new_id>" with "sed -i 's/<old_id>/<new_id>/g' <input_files>", and just paste the whole 1400-line script into the shell.

Thanks for the hint. I did something like that only I used sed to run a script rather than 1400 individual sed executions.

I think I have enough to work on.

Thanks everyone.
 
Back
Top