Sed back reference headache.
am 13.10.2007 23:01:18 von geoDoc
Hi
I am having a really headache with a sed substitution.
Basically I have a file with things in it like CS01 and CHD03 and I
want to change them to CS 1 and CHD 3
There are lots of other things in the file that I don't want to change
- lots of perfectly legitimate zeros in stand alone numbers and things
like J8324 which are fine also.
I am running sed with the -r option and the epression causing problems
runs
s/\([[:alpha:]]{2,}\)0\([[:digit:]]\)/\1 \2/g
(actually earlier tried has [1-9] in place of :digit: but the result
was the same)
I get the error sed: file sedproc line 2: invalid reference \2 on `s'
command's RHS. The error is a little odd as the command is on line one
of the file.
I have spent quite a bit of time trying to find the error but this
really has me beaten. Help.
Many thanks.
Re: Sed back reference headache.
am 13.10.2007 23:13:22 von Janis Papanagnou
geoDoc wrote:
> Hi
>
> I am having a really headache with a sed substitution.
>
> Basically I have a file with things in it like CS01 and CHD03 and I
> want to change them to CS 1 and CHD 3
> There are lots of other things in the file that I don't want to change
> - lots of perfectly legitimate zeros in stand alone numbers and things
> like J8324 which are fine also.
>
> I am running sed with the -r option and the epression causing problems
> runs
>
> s/\([[:alpha:]]{2,}\)0\([[:digit:]]\)/\1 \2/g
>
> (actually earlier tried has [1-9] in place of :digit: but the result
> was the same)
>
> I get the error sed: file sedproc line 2: invalid reference \2 on `s'
> command's RHS. The error is a little odd as the command is on line one
> of the file.
I don't get that error with GNU sed, but the following does the requested
substitution...
sed -e 's/\([[:alpha:]]\{2,\}\)0\([[:digit:]]\)/\1 \2/g'
In: Basically I have a file with things in it like CS01 and CHD03 and I
Out: Basically I have a file with things in it like CS 1 and CHD 3 and I
Janis
>
> I have spent quite a bit of time trying to find the error but this
> really has me beaten. Help.
>
> Many thanks.
>
Re: Sed back reference headache.
am 14.10.2007 22:02:42 von geoDoc
On Oct 13, 10:13 pm, Janis Papanagnou
wrote:
> geoDoc wrote:
> > Hi
>
> > I am having a really headache with a sed substitution.
>
> > Basically I have a file with things in it like CS01 and CHD03 and I
> > want to change them to CS 1 and CHD 3
> > There are lots of other things in the file that I don't want to change
> > - lots of perfectly legitimate zeros in stand alone numbers and things
> > like J8324 which are fine also.
>
> > I am running sed with the -r option and the epression causing problems
> > runs
>
> > s/\([[:alpha:]]{2,}\)0\([[:digit:]]\)/\1 \2/g
>
> > (actually earlier tried has [1-9] in place of :digit: but the result
> > was the same)
>
> > I get the error sed: file sedproc line 2: invalid reference \2 on `s'
> > command's RHS. The error is a little odd as the command is on line one
> > of the file.
>
> I don't get that error with GNU sed, but the following does the requested
> substitution...
>
> sed -e 's/\([[:alpha:]]\{2,\}\)0\([[:digit:]]\)/\1 \2/g'
>
> In: Basically I have a file with things in it like CS01 and CHD03 and I
> Out: Basically I have a file with things in it like CS 1 and CHD 3 and I
>
Many thanks. You are, of course right. I looked again at how I was
running it. It was within a pipe
sed -e :a -e "/[ -]$/N;s/\n//;ta" text.csv |sed -r -fsedproc -e 's/\
([[:alpha:]]\{2,\}\)0\([[:digit:]]\)/\1 \2/g'
This gives the error. Running separately does not (using a temporary
file) does not.
I have routed around the problem now.
--
Gavin