Urgent help required in deleting a line without opening a file usinga shell script

Hi,

I need a help in deleting a line matching a particular pattern in a file using shell script without opening the file. The file is a .c/.cpp file. Is it possible?

Thanks

Hi naan, you can overright the file.
eg. I have a file name.cpp & i wanna delete line with a text "test file". then do it like this

cat name.cpp | grep -v "test file" > name.cpp

"grep -v "test file" will display the name.cpp without line containing words "test file" & " >name.cpp" will overright the previous file

Regds,
Girish

Hi naan, you can delet th eline by overrighting the file.
eg. I have a file name.cpp & i wanna delete line containing words "test file". then do it like this

cat name.cpp | grep -v "test file" > name.cpp

"grep -v "test file" will display the name.cpp without line co :cool: ntaining words "test file" & " >name.cpp" will overright the previous file

Regds,
Girish

That command will wipe out the complete file.

You should be doing

grep -v "test file" name.cpp > name.cpp.new
mv name.cpp.new name.cpp

Yes. I do agree !

Thanks a lot for the input...So can we use the same solution while writing a batch DOS program to delete a particular line from a set of files within a folder so that it recursively goes and delete the line in all the files. :confused:

I think by Dos batch program you mean a shell script, if that is the case then try this:

#! /bin/ksh

for i in *.cpp;
do
grep -v "test file" $i > $i.new
mv $i.new $i
done

Regards,
Tayyab