From: nobody on
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

From: Chris F.A. Johnson on
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=$*

> 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"?

line="This sentence has some words"
left=${line%%has*}
right=${line#*has}
printf "%s\n" "${line#"$left"}" "${line%"$right"}"


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
From: sharma__r on
On Oct 7, 3:40 am, nobody <nob...(a)nowhere.com> 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

## using the "tr" command to squeeze multiple spaces into one
tr -s '\040' '\040' < yourfile

## using "sed" to squeeze multiple spaces into one
sed -e 's/[ ][ ]*/ /g' yourfile

### deleting everything before a word:
sed -e '
s/has/\
&/
s/.*\n//
' yourfile

### deleting everything after a word:
sed -e '
s/has/&\
/
s/\n.*//
' yourfile

### deleting everything before and after a word:
sed -e '
s/has/\
&\
/
s/.*\n\(.*\)\n.*/\1/
' yourfile

Note: Try these on a bourne shell command line. For a c-shell -based
commandline the backslashes need to double.

-- Rakesh
From: Richard C. Giles on
On Oct 6, 8:40 pm, sharma...(a)hotmail.com wrote:
> ### deleting everything before and after a word:
> sed -e '
>    s/has/\
> &\
> /
>    s/.*\n\(.*\)\n.*/\1/
> ' yourfile
>
> Note: Try these on a bourne shell command line. For a c-shell -based
> commandline the backslashes need to double.
>
> -- Rakesh

It would be a lot simpler to use:
$ sed -e 's/.*\(has\).*/\1/' yourfile

Why add tokens and additional statements when the base syntax will do
the task.

-Arcege
From: Ed Morton on
Richard C. Giles wrote:
> On Oct 6, 8:40 pm, sharma...(a)hotmail.com wrote:
>> ### deleting everything before and after a word:
>> sed -e '
>> s/has/\
>> &\
>> /
>> s/.*\n\(.*\)\n.*/\1/
>> ' yourfile
>>
>> Note: Try these on a bourne shell command line. For a c-shell -based
>> commandline the backslashes need to double.
>>
>> -- Rakesh
>
> It would be a lot simpler to use:
> $ sed -e 's/.*\(has\).*/\1/' yourfile
>
> Why add tokens and additional statements when the base syntax will do
> the task.

Good question! Neither of them work, though, as they both look for a
string "has" instead of a word "has":

$ cat file
lets chastise this effort

$ sed -e '
s/has/\
&\
/
s/.*\n\(.*\)\n.*/\1/
' file
has

$ sed -e 's/.*\(has\).*/\1/' file
has

I believe GNU sed has some start/end of word delimiters but if all the
OP actually wants is the word "has" printed there are simpler ways to do
that!

Regards,

Ed.
 |  Next  |  Last
Pages: 1 2 3
Prev: Fork Concept
Next: conditional grep-like command