Re: quick scripting question - finding occurrence in many lines
On Thursday 09 November 2006 03:40, Andrew Sackville-West wrote:
> On Thu, Nov 09, 2006 at 12:52:57AM +1100, John O'Hagan wrote:
[...]
> > ----------------
> > while read i ; do
> >
> > if [[ $(echo "$i" | grep \\-\$ ) ]]; then
> >
> > i=$( echo "$i" | sed s/-\$//)
> > echo "$i"
> > else echo "$i"' '
> > fi
> >
> > done < IN | tr -d '\n' | tr ' ' '\n' | grep -B1 'Processor' |
> > grep -v 'Processor\|--'
> > --------------------------------
> >
> > This removes hyphens at the end of lines or else adds a space, which
> > converts
>
> here's a simpler way to do that, I think ;)
>
> tr '\n' ' ' | sed 's/- //g' | tr ' ' '\n' | grep -B1 'Processor' | grep
> -v 'Processor\|--'
>
> replace the newlines with spaces. then use sed as it matches better
> than tr to strip occurences of '- '. The assumption here is that
> hypens don't appear at the end of words and they only occur because we
> created them with our tr '\n' ' '.
I was worried (well, that's a bit strong!) about words ending with hyphens, so
I went with your sed approach:
#sed s/$/' '/g <IN | sed s/'\- $'//g | tr -d '\n' | tr ' ' '\n' |
grep -B1 'Processor' | grep -v 'Processor\|--'
i.e. append a space to each line, remove any hyphen-plus-space combinations at
the end of lines, then grep as usual.
My reasoning was that a human reader (who would have no problem with this
task), sees a newline as a space and an end-of-line hyphen as not a space.
> SO I wonder what happened to the OP? Is he just watching waiting for
> the right solution, or is he long gone?
>
[...]
OP? What OP? :)
Regards,
John
Reply to: