[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#22943: sed: + intepreted twice
From: |
Davide Brini |
Subject: |
bug#22943: sed: + intepreted twice |
Date: |
Tue, 8 Mar 2016 17:39:36 +0100 |
On Tue, 8 Mar 2016 12:06:43 +0000, "Dam, Jesse van" <address@hidden>
wrote:
> Hi,
>
>
> The plus sign in the following sed command is interpreted twice. One time
> for '1 or more occurrences' and one time as match a plus sign. I think
> this is incorrect behavior. Correct me if I am wrong.
>
>
> echo '+710+1869' | sed 's/\(.[0-9]+\).*/\1/g'
>
> Results in
>
> +710+
>
> Expected result
>
> +710
By default sed uses basic regular expressions (BRE), where "+" is not a
special character, so in your example it's matched literally and you're
getting the expected result.
To do what you wanted you need extended REs (ERE), as in
$ echo '+710+1869' | sed -r 's/(.[0-9]+).*/\1/g'
+710
or you need to tell sed to enable "+" as a nonstandard BRE metacharacter by
escaping it:
$ echo '+710+1869' | sed 's/\(.[0-9]\+\).*/\1/g'
+710
HTH
--
D.