Delete function

I need to delete a certain data in a file. May I know how to do it by using awk, sed any shell command?

For example, i have a file details.dat. The user will search in that file for a particular field and if match, he will delete the whole record. Do you know how to do it?

Can you be more specific?

I too have a similar requirement.
i have a text data file. in that i shall search for some words such as names. if found.. i have to delete the whole line. how can i do that using sed or awk or is there any other command that could do that.
:rolleyes:

sed -e /green/d <inputfile >outputfile
will make a copy of inputfile with all the lines containing "green" removed.

Thank U very much.. by the way.. is there any way to do the same without creating a new file and deleting the old file?
I mean to edit the same file with one command.:slight_smile:

As far as I believe it should be easier to do with a script than with a single command.

And just to be akward, may I suggest that it is possible to do using grep as well....

just use:

#!/bin/bash
#Script to remove line that contains a pattern
echo "Usage: $0 PATTERNTOREMOVE INPUTFILE [OUTPUTFILE]"

if [ $# \< 2 ]; then exit; fi
#if less than two arguments, exit script

if  [ $# = 3 ] ; then
#if 3 args, then third is the output filename
        OFILE=$3;

elif [ $# = 2 ] ; then
#if 2 args then input is also output
#but a temp file is needed
        OFILE="$2"new;

else echo Too many arguments!!!;
        exit;
fi

#output all lines that DO NOT contain string to file
grep -v $1 $2 >$OFILE


if [ $# = 2 ]; then
#if only 2 args rename temp file to input filename
mv "$2"new $2;

fi

I have not tried this on many systems, so it will prob need tweaking....
-

Thanks Ghoti,
I shall try that.

by the way.. I have to do this for more files.. that I thought to use a loop.. but I forgot to use that can anybody help me?
sskb:):confused:

Loops are actually quite easy, once you remember a few rules..

try someting in the format:

for x in $LISTOFVALUES
do
[ACTION TO PERFORM];
[ANY OTHER STUFF];
done;

and if you put my previous script into the middle of the loop and took the input filenames from $x you should have no trouble at all...

-gHoTI

I forgot to tell u.. it is to be done with multiple variables in multiple files.

fair enough then, you could do it with one script, but another option is to write a script to call the first one.

Just use a variable loop to set the values of both.

it sounds a little like a homework Q, but if you want to post some code, I'll certainly have a look at it.........

another thing you never mentioned that makes a difference, are the files needed to be given a new name, or is it ok to replace with the stripped files?

-gHoTi

i just want the same files to be edited. no new file to be created.

:slight_smile:

what shell are you using? bash? csh?
if it is Bash, may I suggest you go to:

http://www.linuxnewbie.org/nhf/intel/programming/introbashscript.html

There they give a good account on how to write scripts, in particular the page http://www.linuxnewbie.org/nhf/intel/programming/introbashscript2.html should have what you need.

If you have a go and post what you CAN do on here, then I will be able to help you more as I will have a better example of what you are trying to do exactly....

well i am using c shell csh. I do not have any idea about bash.

and unfortunatly, my experiences are the other way round, I have used BASH exclusivly, so I do not know the differences :mad:

however, as far as I know, there should not be too much of a difference, but I am sure that some of the stuff on those pages should help.

:slight_smile: thank you very much for your kind information. I shall go through that.

well befor this goes way out of hand. lets look at the above. Now i would really suggest reading a book or a help document on redirection.

the above commadn works but u want one more thing from it. ok easy.

sed '/green/d' inputfile &gt outputfile; mv outputfile inputfile

all done and you still have only one file.

If you get a solution, I would be interested to see it, can you post it please?

hi Optimus,
I tried that sed command syntax you have given.. but it gives me error message as

sed: Function pass/d cannot be parsed.

what does it mean actually?

:confused:

you might have to play w/ the syntax but it works for me. you can also try the orig. syntax posted by perderabo with this small modification.

sed -e /green/d inputfile > outputfile;mv outputfile inputfile

this one is working fine.
Thanks.:slight_smile: