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?
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:
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.
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....
-
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?
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....