You are not logged in.
Pages: 1
Hello!!
So my purpose is to convert a String to another one but I can't do it for some strings.
I have 4 accents and I want to convert them to numbers (they are tones in chinese pinyin). So, let's have a look :
mā --> ma1
má --> ma2
mǎ --> ma3
mà --> ma4
Those accents are the only ones occuring but they can occure as "mǎng" or "míng" (and all over varients you can imagine) and, in both cases, I want to convert them with the number at the end of the String : mang3 and ming2
I've tried to use " sed 's/stringA/stringB/g' " but it's not working. And I don't really want to deal with each later (only voyelles) with all the 4 accents (well, why not because it's only 20 possibilities) on every less-than-6-letters words. (whichi is a LOT).
Does someone have some hints to do that ?
I wanna use it as a script in order to parse a text file and to change every occurence.
Last edited by jiehong (2010-09-13 21:23:39)
Offline
2.5 suggestions:
1) This is in the wrong sub-forum, it should be in the programming sub-forum
2) Maybe your problem with sed is you are trying to overwrite a file in place? Sed doesn't like to do that, you might want to write to a /tmp file and then eventually copy back to the original file. You might want to post your literal code segments. (that's the 0.5 part of my advice)
Based off what you want to do, it really seems like sed is the right tool for the job.
Offline
This won't match exactly what you've written above, but unless I'm missing something, should be on the right track....?
$sed '/ā/s/\(.*\)ā\(.*\)/\1a\21/g' -
ā
a1
First item is input, second item is output.
Also, if you are editing in place, then providing you're using GNU sed, using the -i option will allow that.
Last edited by skanky (2010-09-13 21:15:00)
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Moved to Programming...
Offline
Thanks and sorry for the moving…
I take care of using a tmp file for sed just in case
What Skanky propose works better than I thought and it almost only leads me to write about 20 rules… which is amazing!!
I feel like someone would tell me RTFM better…
Again, thanks!!
Offline
I just tested:
sed -i -e 's/mā/ma1/g' -e 's/má/ma2/g' -e 's/mǎ/ma3/g' -e 's/mà/ma4/g' test.txt
on a sample document, and it worked fine for me. Could you be more specific about what you mean when you say "it doesn't work"? What happens? Do you get an error? Are the substitutions just not made? Or what?
Edit:
D'Oh didn't see that the number was supposed to be at the end of the word. Well, you need to tell us exactly what you mean by "the end of the string"; what counts as an ending? Spaces, tabs and line feeds only? Punctuation?
Double edit: Now I just noticed the thread is solved. Well, I feel silly for posting.
Last edited by frabjous (2010-09-13 21:29:09)
Offline
I just tested:
sed -i -e 's/mā/ma1/g' -e 's/má/ma2/g' -e 's/mǎ/ma3/g' -e 's/mà/ma4/g' test.txt
on a sample document, and it worked fine for me. Could you be more specific about what you mean when you say "it doesn't work"? What happens? Do you get an error? Are the substitutions just not made? Or what?
Edit:
D'Oh didn't see that the number was supposed to be at the end of the word. Well, you need to tell us exactly what you mean by "the end of the string"; what counts as an ending? Spaces, tabs and line feeds only? Punctuation?
Double edit: Now I just noticed the thread is solved. Well, I feel silly for posting.
Just for future reference, you can put those rules in one set of quotes.
sed -i -e 's/mā/ma1/g
s/má/ma2/g
s/mǎ/ma3/g
s/mà/ma4/g' test.txt
Apologies if you already knew that.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Double edit: Now I just noticed the thread is solved. Well, I feel silly for posting.
Easily done. By the time you've written the script, tested it, got it working etc. it's easy to post up, then spot that someone's answered. Happened to me a few times - luckily I spotted one or two and didn't post.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Pages: 1