Complicated string substitution
Hi,
I have a file with a lot of the following ocurrences: denmark.handa.1-10 denmark.handa.1-12344 denmark.handa.1-4 denmark.handa.1-56 ... distributed randomly in a file. I need to convert each of this ocurrences to: denmark.handa.1-10_1 denmark.handa.1-12344_1 denmark.handa.1-4_1 denmark.handa.1-56_1 so basically I add "_1" at the end of each ocurrence. I thought about using sed, but as each "root" is different I have no clue how to go through this. Any suggestion ? Thanks in advance. |
By "root" do you mean the "denmark.handa." part of the string? That part is all the same in your examples, so the
Quote:
Assuming, however, that you want to replace \.([[:digit:]]+-[[:digit:]]+)$, ignoring the "root", sed should have no problem doing so. |
Which part is the root you mentioned. "denmark" or "denmark.handa"?
Could you supply more "real" examples. For example, is the the ".[[:digit:]]-" always ".1-"? You need to be as precise as possible in defining an input pattern and it's position in the line to prevent false positives or prevent missing a matching pattern. Also what is the encoding scheme. You may need to use [[:alpha:]] instead of [a-z] for example to include accents. Code:
sed 's/ \([[:alpha:]][[:alpha:]]*\.[[:alpha:]][[:alpha:]]*\.[[:digit:]]-[[:digit:]][[:digit:]]*\) /\1_1/' file |
try the following shell script for rename all files in a dir with a following _1
#!/bin/bash
for X in `ls -1 ./temp/` do NEW_EXT=_1 mv ./temp/$X ./temp/$X$NEW_EXT done this would also work well with find. for X in `find ./ -name denmark.handa*` to just later the file with the starting chars of denmark.handa |
The OP wants to replace text in a file, not rename files.
|
opps replace in file not file names
the above is good I am assuming you just want to alter the lines sartting with the denmark.handa prefix if so just replace the alpha portions.
If you need to do this in place (without a file redirect) I would say run it once to make sure the result appear good on screen and then use -i switch. first/test run sed 's/ \(denmark\.handa*\.[[:digit:]]-[[:digit:]][[:digit:]]*\) /\1_1/' file final run sed -i 's/ \(denmark\.handa*\.[[:digit:]]-[[:digit:]][[:digit:]]*\) /\1_1/' file |
All times are GMT -5. The time now is 06:01 PM. |