Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Ubuntu User (http://www.linux-archive.org/ubuntu-user/)
-   -   need help with sed problem (http://www.linux-archive.org/ubuntu-user/502748-need-help-sed-problem.html)

Joao Ferreira gmail 03-18-2011 04:46 PM

need help with sed problem
 
Hello all

I need to use sed to replace a given line in a text file with the
current working directory. But this is getting quite tricky. Problem
ilustrated below:

====================================
$ cat text.txt
aaaa
bbbb:/some/wrong/path/
cccc

$ sed s/bbbb.*/bbbb:$TERM/ text.txt
aaaa
bbbb:xterm
cccc

jmf@squeeje:~$ sed s/bbbb.*/bbbb:$PWD/ text.txt
sed: -e expression #1, char 16: unknown option to `s'
=====================================

I'd like to have

====================================
aaaa
bbbb:/home/myself
cccc
====================================

Can anyone help ? thanks in advance.

Joao



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 1300470364.2932.5.camel@squeeje.critical.pt">http://lists.debian.org/1300470364.2932.5.camel@squeeje.critical.pt

Joao Ferreira gmail 03-18-2011 04:47 PM

need help with sed problem
 
Hello all

I need to use sed to replace a given line in a text file with the
current working directory. But this is getting quite tricky. Problem
ilustrated below:

====================================
$ cat text.txt
aaaa
bbbb:/some/wrong/path/
cccc

$ sed s/bbbb.*/bbbb:$TERM/ text.txt
aaaa
bbbb:xterm
cccc

$ sed s/bbbb.*/bbbb:$PWD/ text.txt
sed: -e expression #1, char 16: unknown option to `s'
=====================================

I'd like to have

====================================
aaaa
bbbb:/home/myself
cccc
====================================

Can anyone help ? thanks in advance.

Joao



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Andrej Kacian 03-18-2011 04:59 PM

need help with sed problem
 
On Fri, 18 Mar 2011 17:46:04 +0000
Joao Ferreira gmail <joao.miguel.c.ferreira@gmail.com> wrote:

>jmf@squeeje:~$ sed s/bbbb.*/bbbb:$PWD/ text.txt
>sed: -e expression #1, char 16: unknown option to `s'

Hello,

this is because $PWD gets expanded by shell before sed gets called, so what
actually gets executed is:

sed s/bbb.*/bbbb:/home/myself/ text.txt

...which is obviously syntactically incorrect.

To get result you want, try using different separator character than /, for
example the comma, or underscore:

$ sed s,bbb.*,bbbb:$PWD, text.txt
$ sed s_bbb.*_bbbb:$PWD_ text.txt

--
Andrej Kacian


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20110318185908.2f959df8@penny">http://lists.debian.org/20110318185908.2f959df8@penny

"Boyd Stephen Smith Jr." 03-18-2011 05:15 PM

need help with sed problem
 
On 2011-03-18 12:59:08 Andrej Kacian wrote:
>To get result you want, try using different separator character than /, for
>example the comma, or underscore:

Colon is also a pretty good choice. While it is allowed in pathnames, it
already causes problems. Try adding a directory containing colon to your PATH
or MANPATH. :P
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
bss@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/

Tony Pursell 03-18-2011 05:22 PM

need help with sed problem
 
On Fri, 2011-03-18 at 17:47 +0000, Joao Ferreira gmail wrote:
> Hello all
>
> I need to use sed to replace a given line in a text file with the
> current working directory. But this is getting quite tricky. Problem
> ilustrated below:
>
> ====================================
> $ cat text.txt
> aaaa
> bbbb:/some/wrong/path/
> cccc
>
> $ sed s/bbbb.*/bbbb:$TERM/ text.txt
> aaaa
> bbbb:xterm
> cccc
>
> $ sed s/bbbb.*/bbbb:$PWD/ text.txt
> sed: -e expression #1, char 16: unknown option to `s'
> =====================================
>
> I'd like to have
>
> ====================================
> aaaa
> bbbb:/home/myself
> cccc
> ====================================
>
> Can anyone help ? thanks in advance.
>
> Joao
>
>
>

This is what you want

sed /^bbbb.*/s?.*?bbbb:$PWD? txt.txt > newtxt.txt

Note:
/bbbb.*/ addresses the line you want to change.

The error you got is due to $PWD being expanded to /home/myself *before*
the command is executed. The way round it is to use the little known
facility to use any character instead of / in the substitute command (I
have used ?).

Tony







--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Joao Ferreira gmail 03-18-2011 06:08 PM

need help with sed problem
 
On Fri, 2011-03-18 at 18:59 +0100, Andrej Kacian wrote:
> On Fri, 18 Mar 2011 17:46:04 +0000
> Joao Ferreira gmail <joao.miguel.c.ferreira@gmail.com> wrote:
>
> >jmf@squeeje:~$ sed s/bbbb.*/bbbb:$PWD/ text.txt
> >sed: -e expression #1, char 16: unknown option to `s'
>
> Hello,
>
> this is because $PWD gets expanded by shell before sed gets called, so what
> actually gets executed is:
>
> sed s/bbb.*/bbbb:/home/myself/ text.txt
>
> ...which is obviously syntactically incorrect.
>
> To get result you want, try using different separator character than /, for
> example the comma, or underscore:
>
> $ sed s,bbb.*,bbbb:$PWD, text.txt
> $ sed s_bbb.*_bbbb:$PWD_ text.txt

Thanks a lot... I did not know that was possible...

I used the # separator. Worked just fine

Thank you

Joao

>
> --
> Andrej Kacian
>
>



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 1300475305.2285.0.camel@squeeje.critical.pt">http://lists.debian.org/1300475305.2285.0.camel@squeeje.critical.pt

Tony Pursell 03-18-2011 10:01 PM

need help with sed problem
 
On Fri, 2011-03-18 at 18:22 +0000, Tony Pursell wrote:
> On Fri, 2011-03-18 at 17:47 +0000, Joao Ferreira gmail wrote:
> > Hello all
> >
> > I need to use sed to replace a given line in a text file with the
> > current working directory. But this is getting quite tricky. Problem
> > ilustrated below:
> >
> > ====================================
> > $ cat text.txt
> > aaaa
> > bbbb:/some/wrong/path/
> > cccc
> >
> > $ sed s/bbbb.*/bbbb:$TERM/ text.txt
> > aaaa
> > bbbb:xterm
> > cccc
> >
> > $ sed s/bbbb.*/bbbb:$PWD/ text.txt
> > sed: -e expression #1, char 16: unknown option to `s'
> > =====================================
> >
> > I'd like to have
> >
> > ====================================
> > aaaa
> > bbbb:/home/myself
> > cccc
> > ====================================
> >
> > Can anyone help ? thanks in advance.
> >
> > Joao
> >
> >
> >
>
> This is what you want
>
> sed /^bbbb.*/s?.*?bbbb:$PWD? txt.txt > newtxt.txt
>
> Note:
> /bbbb.*/ addresses the line you want to change.
>
> The error you got is due to $PWD being expanded to /home/myself *before*
> the command is executed. The way round it is to use the little known
> facility to use any character instead of / in the substitute command (I
> have used ?).
>
> Tony
>

Just another point. $PWD will usually give you the home directory as it
is set when you log in and the current working directory will be your
home directory, but you really should use $HOME, which will always be
your home directory.

Tony






--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

"Dr. Ed Morbius" 03-18-2011 10:05 PM

need help with sed problem
 
on 19:08 Fri 18 Mar, Joao Ferreira gmail (joao.miguel.c.ferreira@gmail.com) wrote:
> On Fri, 2011-03-18 at 18:59 +0100, Andrej Kacian wrote:
> > On Fri, 18 Mar 2011 17:46:04 +0000
> > Joao Ferreira gmail <joao.miguel.c.ferreira@gmail.com> wrote:
> >
> > >jmf@squeeje:~$ sed s/bbbb.*/bbbb:$PWD/ text.txt
> > >sed: -e expression #1, char 16: unknown option to `s'
> >
> > Hello,
> >
> > this is because $PWD gets expanded by shell before sed gets called, so what
> > actually gets executed is:
> >
> > sed s/bbb.*/bbbb:/home/myself/ text.txt
> >
> > ...which is obviously syntactically incorrect.
> >
> > To get result you want, try using different separator character than /, for
> > example the comma, or underscore:
> >
> > $ sed s,bbb.*,bbbb:$PWD, text.txt
> > $ sed s_bbb.*_bbbb:$PWD_ text.txt
>
> Thanks a lot... I did not know that was possible...
>
> I used the # separator. Worked just fine

Please note that '#' is a valid filesystem name character.

There are relatively few values which are safely excludable. In your
case this shouldn't be a problem, but in the general case (I'm working
on a script that needs to do path parsing) it's a bit of a pain.

--
Dr. Ed Morbius, Chief Scientist / |
Robot Wrangler / Staff Psychologist | When you seek unlimited power
Krell Power Systems Unlimited | Go to Krell!


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20110318230526.GK3482@altaira.krellpowersys.exo">h ttp://lists.debian.org/20110318230526.GK3482@altaira.krellpowersys.exo

shawn wilson 03-18-2011 10:36 PM

need help with sed problem
 
On Fri, Mar 18, 2011 at 7:05 PM, Dr. Ed Morbius <dredmorbius@gmail.com> wrote:

on 19:08 Fri 18 Mar, Joao Ferreira gmail (joao.miguel.c.ferreira@gmail.com) wrote:

> On Fri, 2011-03-18 at 18:59 +0100, Andrej Kacian wrote:

> > On Fri, 18 Mar 2011 17:46:04 +0000

> > Joao Ferreira gmail <joao.miguel.c.ferreira@gmail.com> wrote:
*


Please note that '#' is a valid filesystem name character.



There are relatively few values which are safely excludable. *In your

case this shouldn't be a problem, but in the general case (I'm working

on a script that needs to do path parsing) it's a bit of a pain.




i agree with that - the underscore that was used is also valid. you might look at proper quoting of variables to avoid this. something like cat text.txt | sed -e 's/bbb.*/bbbb:"$PWD"/' > new.txt

Clive Standbridge 03-19-2011 01:46 PM

need help with sed problem
 
> i agree with that - the underscore that was used is also valid. you
> might look at proper quoting of variables to avoid this. something
> like cat text.txt | sed -e 's/bbb.*/bbbb:"$PWD"/' > new.txt

The output from that, given Joao's original text.txt, is

aaaa
bbbb:"$PWD"
cccc

The reason is that " and $ have no special effect inside '.

I would prefer to use some other character for the s command delimiter
as earlier posters in this thread suggested, because it is easier to
read. But another way you can do it is to escape all the backslashes
in $PWD using a shell expansion:

$ sed "s/bbbb.*/bbbb:${PWD/////}/" text.txt
aaaa
bbbb:/home/clive/temp
cccc

You can see its effect on the string passed to sed using set -x:
$ (set -x; sed s/bbbb.*/bbbb:${PWD/////}/ text.txt)
+ sed 's/bbbb.*/bbbb:/home/clive/temp/' text.txt

A brief explanation of ${PWD/////} :-
${PWD//A/B} replaces all occurrences of pattern A with string B.
For A use / - the prevents / being treated as a delimiter.
For B use / - the first causes the second to be taken literally.

I hope this helps.

--
Cheers,
Clive


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20110319144616.GA3288@rimmer.esmertec.com">http://lists.debian.org/20110319144616.GA3288@rimmer.esmertec.com


All times are GMT. The time now is 12:50 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.