Prev: Fork Concept
Next: conditional grep-like command
From: Ed Morton on 6 Oct 2009 23:17 nobody wrote: > Given the following sentence: > > This line has odd spaces in it > > How can I remove the extra spaces so the sentence reads: > > This line has odd spaces in it > > > And secondly, how do you delete text BEFORE and AFTER a word? > For example, > > This sentence has some words > > How would I delete everything before the word "has"? > And delete everything after the word "has"? > > -Thanks > Clarify what you mean by a "word". Where, if at all, does the word "has" appear in these: Don't chastise this. Yes he has. No, he hasn't. I said "has"! If it appears in "hasn't" we might have a problem ;-). What are you really trying to do, though, since it sounds like all you might really want is a variation of: grep -o has file Regards, Ed.
From: Greg Russell on 7 Oct 2009 02:24 "nobody" <nobody(a)nowhere.com> wrote in message news:uHPym.154$_s7.47(a)en-nntp-04.dc1.easynews.com... > Given the following sentence: > This line has odd spaces in it > > How can I remove the extra spaces so the sentence reads: > This line has odd spaces in it 's/ * / /g' > And secondly, how do you delete text BEFORE and AFTER a word? > For example, > > This sentence has some words > > How would I delete everything before the word "has"? > And delete everything after the word "has"? 's/.*has.*/has/g'
From: Greg Russell on 7 Oct 2009 02:31 I wrote in message news:7j2qhtF32itbaU1(a)mid.individual.net... >> How would I delete everything before the word "has"? >> And delete everything after the word "has"? > > 's/.*has.*/has/g' I forgot the space delimiters that qualify a word: 's/.* has .*/has/g'
From: Stephane CHAZELAS on 7 Oct 2009 07:14 2009-10-6, 23:56(+00), Chris F.A. Johnson: > On 2009-10-06, nobody wrote: >> Given the following sentence: >> >> This line has odd spaces in it >> >> How can I remove the extra spaces so the sentence reads: >> >> This line has odd spaces in it > > line="This line has odd spaces in it" > set -f > set -- $line > newline=$* [...] AFAIK, the behavior of "newline=$*" is unspecified as per POSIX (and in reality, YMMV for some non-default values of $IFS). So newline="$*" is preferable. Note that the above turns tabs and newlines into spaces as well (with the default value of $IFS). If it's not wanted, you may change $IFS to ' '. -- St�phane
From: Ed Morton on 7 Oct 2009 08:23
Greg Russell wrote: > I wrote in message news:7j2qhtF32itbaU1(a)mid.individual.net... > >>> How would I delete everything before the word "has"? >>> And delete everything after the word "has"? >> 's/.*has.*/has/g' > > I forgot the space delimiters that qualify a word: > > 's/.* has .*/has/g' > Punctuation delimits words too. And then there's "has" at the start or end of a line... Ed. |