Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

search string and replace in shell

0 views
Skip to first unread message

Obama

unread,
Aug 18, 2009, 7:43:09 PM8/18/09
to
I do have a file that contains the following line:

This the only one in shell
That are the only one in shell
This is the shell
---
--

what I need is to search each line that starts with "This" and also
line
contains "shell" then replace shell with word "fish"
unfortunately I get this to work your help will be appricated in
advance..

Here is the code:
file=$1
replace=$2

sed -i.backup -e "s/\(^This\)\(..*$\)\(shell\)/\(^This\)\(..*$\)\1$
{replace}/" $file

Allen Kistler

unread,
Aug 18, 2009, 10:24:37 PM8/18/09
to
Obama wrote:
> I do have a file that contains the following line:
>
> This the only one in shell
> That are the only one in shell
> This is the shell
> ---
> --
>
> what I need is to search each line that starts with "This" and also
> line
> contains "shell" then replace shell with word "fish"
> unfortunately I get this to work your help will be appricated in
> advance..
>
> [snip]

Taking a little extra liberty to help insure "This" and "shell" are
whole words:

sed -e '/^This /s/ shell/ fish/'

Of course, "This is a shellfish." would come out "This is a fishfish."
But I expect you're really doing something not quite the same as you
represent.

Or is this a homework assignment?

0 new messages