Prev: bash 4.1 interix
Next: Thinking about a new shell
From: Loki Harfagr on 7 Aug 2010 05:04 Fri, 06 Aug 2010 22:21:33 +0100, pk did cat : > On Fri, 6 Aug 2010 14:10:05 -0700 (PDT) The Magnet <art(a)unsu.com> wrote: > >> I have data in a file that I need to do a global replace on. Each line >> starts like this: 1, 2, >> >> Basically a number then a comma then a space then a number then a comma >> then a space. >> >> I need to do a global replace throughout the file so it looks like >> this: >> >> 1||||2|||| (So number then 4 pipes then number then 4 pipes). >> >> I'm playing with SED but cannot get the regular expression. Any help >> please? > > sed 's/^\([0-9]\{1,\}\), \([0-9]\{1,\}\)/\1||||\2||||/' file as the OP did *not* precise what happened to end of lines there could be some "features" ;-) for instance, let's play with a test file including 3+1 endings variants: ------- $ cat -A MISCFILES/numbers 1, 2, 3, $ 1, 2, 3,$ 1, 2, 3$ 1, 2, $ 1, 2,$ 1, 2$ ------- then, your solution would give: ------- $ sed 's/^\([0-9]\{1,\}\), \([0-9]\{1,\}\)/\1||||\2||||/' MISCFILES/numbers 1||||2||||, 3, 1||||2||||, 3, 1||||2||||, 3 1||||2||||, 1||||2||||, 1||||2|||| ------- let's propose two other expression introducing different "features" ;-) ------- sed 's/\(.\)\([^0-9]*\)/\1|||/g' MISCFILES/numbers 1|||2|||3||| 1|||2|||3||| 1|||2|||3||| 1|||2||| 1|||2||| 1|||2||| $ sed 's/\(.\)../\1|||/g' MISCFILES/numbers 1|||2|||3||| 1|||2|||3, 1|||2|||3 1|||2||| 1|||2, 1|||2 ------- note that I don't say that a solution is 'better' than another, the answer is in the hands of on one end the data provider and on the other end in the eye of the target data processor ;-) |