Re: sed – need an example for ”\digit”
Johnny Rosenberg
gurus.knugum at gmail.com
Sun Feb 26 10:22:18 UTC 2012
2012/2/26 Michael Welle <mwe012008 at gmx.net>:
> Hello,
>
> Johnny Rosenberg <gurus.knugum at gmail.com> writes:
>
>> http://www.delorie.com/gnu/docs/sed/sed_5.html
>>
>> On that page saw this:
>>
>> ”\digit
>> Matches the digit-th \(...\) reference in the regular expression.”
>>
>> But I failed to create an example that works.
>> Anyone?
>
> echo 'hello crude world' | sed -e 's/^\([ehlo]\+\) \([^ ]*\)\(.*\)$/\1
> x \2 x \3/
>
> \n is a back reference to the n. occurrence of a pair of braces.
>
> Regards
> hmw
Oh… I'm so sorry for having bothered you with that question. Why?
Because I use that syntax all the time without any problems.
So what was the problem this time? I don't know, maybe I went up from
bed too early or something, but I totally misunderstood the text
”Matches the digit-th \(...\) reference in the regular expression.”. I
was so focused on using ”\digit” after the first slash so I didn't
realise that this is the usual ”/digit after the second slash” that I
use all the time…
But now, a couple of hours later, when I read that text again, I can't
really see why I misunderstood that sentence in the first place… I
really feel stupid now, I can assure you that.
Okay, sorry for that. Next time I'll make sure I've been awake for a
couple of hours before I ask any questions.
By the way, I have an almost similar question, let's call it a follow
up question ant let's how it's not as stupid as the last one…
This works as expected:
~$ echo "This is a short sentence" | sed 's/[^ ]* [^ ]* //'
a short sentence
~$
However, I tried to make it somewhat shorter, by using the {digit}
syntax, but I failed again… I am not sure how to group ”[^ ]* ” in a
proper way. This does not work:
~$ echo "jag är en ko" | sed 's/\([^ ]* \){2}//'
jag är en ko
~$
What I'm trying to do is to remove the two first words from a
sentence, if that wasn't obvious. This should work no matter how many
words there are in the sentence. Originally I tried to solve this with
awk, but I failed…
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
More information about the ubuntu-users
mailing list