From: Ed Morton on
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
"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
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
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
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.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Fork Concept
Next: conditional grep-like command