Re: do shell script- sed conundrum
Re: do shell script- sed conundrum
- Subject: Re: do shell script- sed conundrum
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 17 Feb 2006 20:46:05 -0500
Perl was, as far as I know, the first tool to implement lookahead and
lookbehind.
Perl took the regex features in ed (the first regex-based tool in the
UNIX tradition), grep (the first such tool designed for
non-interactive use), sed (the second, which started out as a
specialized tool like grep but grew into a full-fledged stream
editor), plus the extensions in egrep ("extended grep"), ex (the
BSD-improved ed), vi (the visual interface to ex), etc. and
rationalized all of the features into a coherent system. Since then,
most other tools have advertised "perl5 regular expressions".
On 2/17/06, Eric Geoffroy <email@hidden> wrote:
> Hope I'm not imposing too much more, but I just discovered another
> major limitation of sed— It will not allow me to combine lookahead
> and lookbehind.
>
> Is it time to move the whole 15 line sed script over to Perl or
> Python? Does Perl implement the lookahead/behind. I checked a regex
> reference book and it's a little vague.
>
> - Eric
>
> On Feb 17, 2006, at 12:44 PM, Christopher Nebel wrote:
>
> > That works (aside from the -p/-n slip that you caught already), but
> > your reasoning about \B is incorrect. \B is defined as a zero-
> > width assertion -- it never matches any characters, only the
> > boundary itself. This is sufficient:
> >
> > perl -pe 's/\BTitle:/\nTitle:/'
> >
> > Or, if you prefer:
> >
> > perl -pe 's/\B(Title:)/\n$1/'
> >
> >
> > --Chris Nebel
> > AppleScript and Automator Engineering
> >
> > On Feb 17, 2006, at 10:04 AM, Mark J. Reed wrote:
> >
> >> Uhm, so why use sed? Just use perl. perl -ne 's/.../.../' filename
> >> does what sed 's/.../.../' filename does.
> >>
> >> However, your regex looks wrong. s/\BTitle:/Title:\r" will turn
> >> "Title: King KongTitle: Godzilla" into
> >> "Title: King KonTitle:
> >> Godzilla".
> >>
> >> You want to 1. keep whatever the \B matches and 2. put the newline
> >> *before* the word Title.
> >>
> >> perl -ne 's/(\B)(Title)/$1\n$2/' title.txt
> >>
> >>
> >>
> >> On 2/17/06, Eric Geoffroy <email@hidden> wrote:
> >>> This is a tiny part of a sed script that I'm calling from 'do shell
> >>> script' This is the misbehaving part.
> >>>
> >>>
> >>> SOURCE
> >>> Title: King KongTitle: Godzilla
> >>> Title: Mothra
> >>>
> >>> DESIRED RESULT
> >>> Title: King Kong
> >>> Title: Godzilla
> >>> Title: Mothra
> >>>
> >>> I worked out the regex to match '\BTitle: '
> >>> to negate the word boundary. In sed though, the boundary is \<
> >>> but I can't figure out how to negate that.
> >>>
> >>> In my make-believe world, this would work-
> >>>
> >>> sed 's/\BTitle:/Title:\r/' title.txt
> >>>
> >>> I also tried ^\<(Title)
> >>>
> >>> In the real world, the \B is invalid for sed, and the shell in
> >>> OSX is
> >>> not handling the \r.
> >>>
> >>> thx
> >>> - Eric
> >>>
> >>> _______________________________________________
> >>> Do not post admin requests to the list. They will be ignored.
> >>> Applescript-users mailing list (Applescript-
> >>> email@hidden)
> >>> Help/Unsubscribe/Update your Subscription:
> >>> @mail.com
> >>>
> >>> This email sent to email@hidden
> >>>
> >>
> >>
> >> --
> >> Mark J. Reed <email@hidden>
> >> _______________________________________________
> >> Do not post admin requests to the list. They will be ignored.
> >> Applescript-users mailing list (Applescript-
> >> email@hidden)
> >> Help/Unsubscribe/Update your Subscription:
> >> 40apple.com
> >>
> >> This email sent to email@hidden
> >
> > _______________________________________________
> > Do not post admin requests to the list. They will be ignored.
> > Applescript-users mailing list (Applescript-
> > email@hidden)
> > Help/Unsubscribe/Update your Subscription:
> > email@hidden
> >
> > This email sent to email@hidden
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Applescript-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden