LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need to delete a line if a field of that line matches using awf in bash scripting (http://www.linuxquestions.org/questions/linux-newbie-8/need-to-delete-a-line-if-a-field-of-that-line-matches-using-awf-in-bash-scripting-735818/)

accesskarthi 06-26-2009 10:14 AM

need to delete a line if a field of that line matches using awf in bash scripting
 
Hi all,
I am new to linux and scripting. I have a file REPORT which has entries like
00:00:00 06182009 23:59:59 16182009
00:00:00 06192009 23:59:59 26192009
00:00:00 06202009 23:59:59 36202009
00:00:00 06212009 23:59:59 46212009
00:00:00 06232009 23:59:59 56232009

I have a variable named as DATE. Each line is ended with "\n". I need to check DATE with the second column of each row. If there is a match found i need to delete the entire line.

Eg : if DATE = 06202009, the output should be
00:00:00 06182009 23:59:59 16182009
00:00:00 06192009 23:59:59 26192009
00:00:00 06212009 23:59:59 46212009
00:00:00 06232009 23:59:59 56232009
Eg 2 : if DATE = 07202009, for the original file,the output should be
00:00:00 06182009 23:59:59 16182009
00:00:00 06192009 23:59:59 26192009
00:00:00 06202009 23:59:59 36202009
00:00:00 06212009 23:59:59 46212009
00:00:00 06232009 23:59:59 56232009

There should not be any empty line in that place after deleting. I need possibly an one line script for this using AWF so that i can insert this in my script file.

Kindly help to solve this .. I am using bash script in linux environment

regards
Karthi

accesskarthi 06-26-2009 10:17 AM

correction
 
In Eg 2: the value of date is not 06202009..

colucix 06-26-2009 10:19 AM

Maybe you mean AWK, not AWF:
Code:

date=06202009
awk '$2!='$date testfile


accesskarthi 06-29-2009 01:13 AM

it is not overwriting in that file
 
Hi Thanks for your reply. the output is showing exactly what i want but , it is not deleted in the testfile.
In this case , i need to delete the row which is matched.
please help me how to overwrite this in my file

chrism01 06-29-2009 01:39 AM

Maybe this will help: http://www.grymoire.com/Unix/Awk.html

accesskarthi 06-29-2009 02:18 AM

i tried to put the output
 
awk '$2!='$date testfile > testfile
but the testfile is empty.
when i tried to write the output to another file it worked.
awk '$2!='$date testfile > newtestfile


Kindly guide me to resolve this.

colucix 06-29-2009 02:42 AM

Awk cannot edit the file in place. As in your last example you have to redirect output to a new file, then rename it overwriting the old one:
Code:

$ awk '$2!='$date testfile > newtestfile
$ mv newtestfile testfile

On the other hand, sed can do that on the fly by means of the -i option:
Code:

$ sed -i.bck "/$date/d" testfile
The "extension" after the -i option is to force sed to do a backup copy of the original file before modifying it. You can remove the extension if you don't want any backup. Anyway, take in mind that the example above does not look for the pattern in the second field, but in the entire line. As a consequence if the pattern appear somewhere else in a line, that line will be deleted as well and maybe that is an unwanted effect.

gururaj.jois 06-29-2009 03:45 AM

need to delete a line if a field of that line matches using awf in bash scripting
 
Quote:

Originally Posted by accesskarthi (Post 3589569)
awk '$2!='$date testfile > testfile
but the testfile is empty.
when i tried to write the output to another file it worked.
awk '$2!='$date testfile > newtestfile


Kindly guide me to resolve this.

The ">" operator for redirection is not a facility of awk, but the shell.
Never try such things!!! The source file will be truncated.

Why?

The main cause is that the shell is not so smart to understand all of your needs.

For example, if you say $> cat file > file
then the shell in order to dump the output of 'cat' command to file 'file', it first tries to open the file.
If that file 'file' exist (obviously in this case), it opens it with O_TRUNCATE flag, i.e. it content will be truncated and the 'cat' decently cat's the empty file.

So always redirect the output to temp file and then move it to original.

Bye,
Guru

accesskarthi 06-29-2009 04:15 AM

Thank you guys.
 
It worked in my desktop. thanks for your suggestions.
regards
Karthi


All times are GMT -5. The time now is 05:51 AM.