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 |
correction
In Eg 2: the value of date is not 06202009..
|
Maybe you mean AWK, not AWF:
Code:
date=06202009 |
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 |
Maybe this will help: http://www.grymoire.com/Unix/Awk.html
|
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. |
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 Code:
$ sed -i.bck "/$date/d" testfile |
need to delete a line if a field of that line matches using awf in bash scripting
Quote:
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 |
Thank you guys.
It worked in my desktop. thanks for your suggestions.
regards Karthi |
All times are GMT -5. The time now is 03:25 AM. |