Does sed '/'$var'/d' textfile work if $var has / in it???

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by William W Zhan » Wed, 03 Jul 1996 04:00:00



The subject says all! I was trying to search and delete some line from a
file:

#! /bin/sh -f

var=/local/bin/test

sed '/'$var'/d' textfile >>textfile1  # This won't work!!!

~
~
~

so help!!

Thanks in advance!

--Bill

 
 
 

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by Harrison Berger » Thu, 04 Jul 1996 04:00:00



 >The subject says all! I was trying to search and delete some line from a
 >file:
 >
 >#! /bin/sh -f
 >
 >var=/local/bin/test
 >
 >sed '/'$var'/d' textfile >>textfile1  # This won't work!!!
 >

#! /bin/sh

var=/local/bin/test

sed '/"$var"/d' textfile >>textfile1

--

    Steinberger:
State of the Instrument

 
 
 

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by George Hack » Thu, 04 Jul 1996 04:00:00



:  >The subject says all! I was trying to search and delete some line from a
:  >file:
:  >
:  >#! /bin/sh -f
:  >var=/local/bin/test
:  >sed '/'$var'/d' textfile >>textfile1  # This won't work!!!
:
: #! /bin/sh
: var=/local/bin/test
: sed '/"$var"/d' textfile >>textfile1
:
: --

:     Steinberger:
: State of the Instrument

The above proposed solution does *not* work.  It deletes lines that
contain the literal pattern "$var" in the file textfile.  The single
quotes surrounding the sed command do not allow the shell to expand
the variable.

Since sed requires the slash character to delimit addresses that match
patterns, the slashes in the pattern to be matched ($var) need to be
protected with backslashes.  Rather than going through all of the work
to get that quoting to work, use egrep to delete the lines that contain
the pattern:

        var=/local/bin/test
        egrep -v "$var" textfile >> textfile1

Hope this helps...

George

-----------------------------------------------------------------------

Unix Specialist                   Web: http://www.teleport.com/~ghacker
Portland, Oregon

 
 
 

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by Uwe Waldma » Thu, 04 Jul 1996 04:00:00





> :  >The subject says all! I was trying to search and delete some
> :  >line from a file:
> :  >
> :  >#! /bin/sh -f
> :  >var=/local/bin/test
> :  >sed '/'$var'/d' textfile >>textfile1  # This won't work!!!

> Since sed requires the slash character to delimit addresses that
> match patterns, the slashes in the pattern to be matched ($var)
> need to be protected with backslashes.  Rather than going through
> all of the work to get that quoting to work, use egrep to delete
> the lines that contain the pattern:

>    var=/local/bin/test
>    egrep -v "$var" textfile >> textfile1

This is the easiest solution, if you just want to delete some
lines from a file. If the deletion command is part of a longer
sed script that can't easily be rewritten using *grep, there
might be another way:

Some sed implementations (consult your manual) allow to use a
character different from "/" to delimit regexps, if the first
delimiter is preceded by a backslash. So

  /qwerty/d

can be replaced by

  \#qwerty#d

Any other character can be used instead of "#" (even a control
character, which has the advantage to occur rather seldom in
file names). Using this feature, the problem can be solved by

  sed -e '\#'"$var"'#d' textfile >>textfile1

--
Uwe Waldmann, Max-Planck-Institut fuer Informatik
Im Stadtwald, D-66123 Saarbruecken, Germany

 
 
 

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by Sudhakar Mekathott » Fri, 05 Jul 1996 04:00:00






> > :  >The subject says all! I was trying to search and delete some
> > :  >line from a file:
> > :  >
> > :  >#! /bin/sh -f
> > :  >var=/local/bin/test
> > :  >sed '/'$var'/d' textfile >>textfile1  # This won't work!!!

[load of stuff deleted.]

1. Somebody mentioned sed requires a '/'. It is wrong.

2. The simplest and easiest and coolest way is to use some other
delimiter instead of '/'

example.

to select a pattern with '/' in it
    sed -s '.blah/woo/far.bar.'g works fine. It IS generic.
you can use characters like .(dot),(comma),?(question mark) to delimit.

There you go. You have lot of choice.

Hope this helps.

==

 
 
 

Does sed '/'$var'/d' textfile work if $var has / in it???

Post by Uwe Waldma » Fri, 05 Jul 1996 04:00:00




> 1. Somebody mentioned sed requires a '/'. It is wrong.

> 2. The simplest and easiest and coolest way is to use some other
> delimiter instead of '/'

> example.

> to select a pattern with '/' in it
>     sed -s '.blah/woo/far.bar.'g works fine. It IS generic.
> you can use characters like .(dot),(comma),?(question mark) to delimit.

Where? In the substitution command (s/foo/bar/), other delimiters
can be used, e.g.,

  sed -e s,foo,bar,g   # Replace every "foo" by "bar"

But neither Sun sed nor GNU sed allows a non-backslashed delimiter
different from "/" in the address part, i.e.,

  sed -e ,foo,d        # Delete every line containing "foo"

Are there any "sed"s where this command is legal? Which?

--
Uwe Waldmann, Max-Planck-Institut fuer Informatik
Im Stadtwald, D-66123 Saarbruecken, Germany

 
 
 

1. sed -e 's/\'a\'/\';\'/' ?

Hi,

my question is, how can I mask the single
quotation mark in my file for sed

I have a lot of SQL-Loader files of this kind:

cat t55.ctl
load data
infile 't55'
truncate
into table A_220_T55
fields terminated by ';' TRAILING NULLCOLS
(AAA_000_KEY    char ,
B_220_BBB_LFD_NR char ,
AAA_220_BBB     char )

there is given a delimiter ';'

The delimiter should be changed to a.

#!/bin/ksh
#set -x
for file in t*.ctl
do
sed -e 's/;/a/' $file >$file.new
mv $file.new $file
done

That's ok.

Now the delimiter should be rechanged from a to ;.
I'd tried

sed -e 's/\'a\'/\';\'/' $file >$file.new

It doesn't work.

How to mask the single quotation mark inside the sed command?

TIA,

Nele

2. Broadcast, how?

3. How to make sed '/'$var'/d' testfile work if $var has / in it???

4. Fatal Error:

5. sed and '/' to '\/' conversion.

6. AudioPCI Wavetable Soundcard

7. alpha sort: 'sed'/'awk'?

8. RTC malfuntion? on dual boot 486sx

9. grep on '$VAR ' (read: $VAR with a SPACE " " after it)

10. sed -e s/\'/\&apos\;/g doesn't seem to work

11. Can 'require' override 'allow'/'deny' (Apache)

12. pd-ksh/zsh won't inherit '747=jet' env var?

13. PROBLEM: 'sed' script 's/^ /\n/' not working properly