SED - Match a line from a File

Hi,
I want to match a line which exists in a file. I have written a test script similar to below -
The content of the file file.txt would be like this -

/usr/bin/1234.xcf
/usr/bin/3456.xcf
/usr/bin/7897.xcf
/usr/bin/2345.xcf
out=`sed -n '\/usr\/bin\/7897.xcf/p' file.txt 2>&1`
echo $out ##  Prints the expected line

But, my issue is that I will be getting the file contents in the code inside a variable in a while loop. i.e. like file="/usr/bin/7897.xcf". So, now, do I have to decompose this content at runtime to pass it to sed ?

Or is there a better way to achieve this ?

Here's an idea:

$ 
$ 
$ cat file.txt
/usr/bin/1234.xcf
/usr/bin/3456.xcf
/usr/bin/7897.xcf
/usr/bin/2345.xcf
$ 
$ 
$ FILE="/usr/bin/7897.xcf"
$ 
$ grep "$FILE" file.txt
/usr/bin/7897.xcf
$ 
$ 

tyler_durden

Hi,
Why I need this is so that I can delete the matching line from the file. Or I should be able to replace it with a BLANK.

I saw that if I use option "d" in sed command, it echos all the lines except the given one.

`sed -n '\/usr\/bin\/7897.xcf/d' file.txt 2>&1`

So, if there is a variable, can I do the same thing with sed ? It will be lot more easier.

---------- Post updated at 11:07 PM ---------- Previous update was at 09:08 PM ----------

One solution could be -

In this example, we tell grep to look for "out dated"--with the space in the middle.
This command searches the file "myfile.new" for the text "out dated"--no matter whether upper-case or lower-case letters have been used--and puts all lines that do not have "out dated" in them into the file "myfile.newer".

grep -iv "out dated" myfile.new > myfile.newer 

Courtsey

http://www.udel.edu/topics/software/general/editors/unix/vi/delsearch.html

Here's a Perl solution:

$
$
$ # display the content of the data file
$ cat -n file.txt
     1  /usr/bin/1234.xcf
     2  /usr/bin/3456.xcf
     3  /usr/bin/7897.xcf
     4  /usr/bin/2345.xcf
$
$ FILE="/usr/bin/7897.xcf"
$
$ # replace the line matching pattern $FILE with a blank space
$ # a backup file is created here
$
$ perl -i.bak -pne "s|^$FILE$| |" file.txt
$
$ # verify the update
$
$ cat -n file.txt
     1  /usr/bin/1234.xcf
     2  /usr/bin/3456.xcf
     3
     4  /usr/bin/2345.xcf
$
$

tyler_durden