LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-26-2009, 09:14 AM   #1
accesskarthi
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Rep: Reputation: 0
Smile 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

Last edited by accesskarthi; 06-26-2009 at 09:15 AM. Reason: sorry kindly check the Eg 2; the case is ,if the entry is not present
 
Old 06-26-2009, 09:17 AM   #2
accesskarthi
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
correction

In Eg 2: the value of date is not 06202009..
 
Old 06-26-2009, 09:19 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Maybe you mean AWK, not AWF:
Code:
date=06202009
awk '$2!='$date testfile
 
Old 06-29-2009, 12:13 AM   #4
accesskarthi
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 06-29-2009, 12:39 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
Maybe this will help: http://www.grymoire.com/Unix/Awk.html
 
Old 06-29-2009, 01:18 AM   #6
accesskarthi
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 06-29-2009, 01:42 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
Old 06-29-2009, 02:45 AM   #8
gururaj.jois
LQ Newbie
 
Registered: Jun 2008
Posts: 16

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

Quote:
Originally Posted by accesskarthi View Post
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
 
Old 06-29-2009, 03:15 AM   #9
accesskarthi
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you guys.

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
How do I actually delete a line using bash? gerben12 Linux - General 2 12-03-2008 01:54 AM
Bash scripting new line interpretation lord-fu Programming 4 06-29-2007 04:34 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration