Technical Discussion
  >> Linux Issues


Register (or login) on our website and you will not see this ad.


  Print Thread
Standard User Spiky
(newbie) Tue 26-Mar-13 16:38:30
Print Post

Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,$$$


[link to this post]
 
Hi all,

I want to do a little script in bash to change for example 00:00:05:880 --> 00:00:08:280 into 00:00:05,880 --> 00:00:08,280 with a comma at the end of each time as you can see.

I've an expression like this : sed "s/\(.*\)\:/\1,/" but it changes only the last comma, and I want it to do it twice on the same line, like in the example.

I've also try the tr command : tr ":$$$" ",$$$" but it changes all the colon into commas.

Thanks for your help.
Standard User metalhead41
(knowledge is power) Tue 26-Mar-13 16:52:24
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: Spiky] [link to this post]
 
at the end of your sed command, can't you just put a "g" for global? e.g.
sed "s/\(.*\)\:/\1,/g" That should apply to all instances then (I think) smile

----------------------------------------------------
We may be human But we're still animals

My Blog | follow me @metalhead41 | Surfin' Bird Dance
Standard User metalhead41
(knowledge is power) Tue 26-Mar-13 17:14:48
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: metalhead41] [link to this post]
 
Scratch that, it doesn't work...

----------------------------------------------------
We may be human But we're still animals

My Blog | follow me @metalhead41 | Surfin' Bird Dance


Register (or login) on our website and you will not see this ad.

Standard User Lethe
(fountain of knowledge) Wed 27-Mar-13 10:18:54
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: Spiky] [link to this post]
 
Interesting. I bet if we was all regex guru's this would be simple, but anyway, this works, but it is a bit 'hackish'.

I have the line:

00:00:05:880 --> 00:00:08:280

in a file called 'digits'.

This code:

Text
1
cat digits | sed "s/:/ /g" | awk {'print $1,":",$2,":",$3,",",$4,$5,$6,":",$7,":",$8,","$9'} | sed "s/ //g" | sed "s/-->/ --> /"


Produces:

~ $ cat digits | sed "s/:/ /g" | awk {'print $1,":",$2,":",$3,",",$4,$5,$6,":",$7,":",$8,","$9'} | sed "s/ //g" | sed "s/-->/ --> /"
00:00:05,880 --> 00:00:08,280


Nick
Standard User AEP
(eat-sleep-adslguide) Wed 27-Mar-13 11:24:15
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: Spiky] [link to this post]
 
How about:

sed "s/:\([0-9]\{3\}\)/,\1/g"
Standard User Lethe
(fountain of knowledge) Wed 27-Mar-13 11:40:31
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: AEP] [link to this post]
 
I knew there would be something easier...

Nick laugh
Standard User Spiky
(newbie) Wed 27-Mar-13 18:56:37
Print Post

Re: Bash - Change expression like $$:$$:$$:$$$ to $$:$$:$$$,


[re: Lethe] [link to this post]
 
Yeah, thanks all for your help ! laugh
  Print Thread

Jump to