You are not logged in.

#1 2010-09-13 20:53:31

jiehong
Member
Registered: 2009-03-19
Posts: 63
Website

[Solved] Bash scripting issue

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 2010-09-13 20:59:11

pseudonomous
Member
Registered: 2008-04-23
Posts: 349

Re: [Solved] Bash scripting issue

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

#3 2010-09-13 21:12:32

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Bash scripting issue

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

#4 2010-09-13 21:17:04

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Bash scripting issue

Moved to Programming...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2010-09-13 21:23:17

jiehong
Member
Registered: 2009-03-19
Posts: 63
Website

Re: [Solved] Bash scripting issue

Thanks and sorry for the moving…

I take care of using a tmp file for sed just in case wink

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

#6 2010-09-13 21:24:49

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: [Solved] Bash scripting issue

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

#7 2010-09-13 21:40:55

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Bash scripting issue

frabjous wrote:

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

#8 2010-09-13 21:42:34

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] Bash scripting issue

frabjous wrote:

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. smile


"...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

Board footer

Powered by FluxBB