Delete lines from file based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines from file based on condition
# 1  
Old 04-20-2014
Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me.

Sample Input

Code:
[root@test1 log]# cat messages-2
Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 15 11:25:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start
Apr 16 19:42:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 16 19:42:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start
Apr 17 15:34:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 17 15:34:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start
Apr 18 18:41:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.123456
Apr 18 18:41:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start1234567890987654321
Apr 19 17:11:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 19 17:11:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start1234567890987654321234567890987654321234567890
Apr 20 15:11:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 20 15:11:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start

Sample Output

Code:
[root@test1 log]# cat messages-2
Apr 19 17:11:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 19 17:11:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start1234567890987654321234567890987654321234567890
Apr 20 15:11:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started.
Apr 20 15:11:03 test1 rsyslogd: [origin software="rsyslogd" swVersion="4.6.2" x-pid="1092" x-info="http://www.rsyslog.com"] (re)start


Last edited by Scrutinizer; 04-20-2014 at 07:51 AM.. Reason: CODE tags
# 2  
Old 04-20-2014
Code:
awk -v d="$(date -d '1 days ago' '+%h %d')" 'NR == FNR {if($0 !~ "^"d) {next}; n = (n == "") ? FNR : n; next} FNR >= n' messages-2 messages-2 > messages-2.tmp && mv messages-2.tmp messages-2

# 3  
Old 04-21-2014
This mini-script with grep works for me.

Code:
numdays=1
while [ $numdays -lt 2 ];do
  mydate=$(date -d"${numdays} days ago" "+%h %d")
  grep -i ^"${mydate}" message
  let numdays++
done

[edit]

Just redirect the output to another file.

myscript.sh > messages_last2days

Last edited by Kibou; 04-21-2014 at 10:05 AM.. Reason: Aclaration
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat lines based on condition

The awk below uses the tab-delimeted fileand reformats each line based on one of three conditions (rules). The 3 rules are for deletion (lines in blue), snv (line in red), and insertion (lines in green). I have included all possible combinations of lines from my actual data, which is very large.... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

3. Shell Programming and Scripting

Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt. 32bm.txt: 249 253 A P - 0 0 8 0, 0.0 6,-1.4 0, 0.0 2,-0.4 -0.287 25.6-102.0 -74.4 161.1 37.1 13.3 10.9 250... (2 Replies)
Discussion started by: aden
2 Replies

4. Shell Programming and Scripting

Print certain lines based on condition

Hi All, I have following listing Filesystem GB blocks Free Used Iused Iused Mounted on /dev/hd2 4.00 0.31 93 63080 43 /usr Filesystem GB blocks Free Used Iused Iused Mounted on Filesystem GB blocks Free Used Iused Iused... (11 Replies)
Discussion started by: ckwan
11 Replies

5. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

6. Shell Programming and Scripting

Need to delete all lines where any line meets a condition

Here is an example of a file... foo1,good foo1,good foo2,error foo2,good Note that both rows for foo1 have good in the 2nd field, but one of the foo2 rows has error... I need something in ksh/awk/perl that will delete ALL foo2 lines if ANY of them have error in the 2nd field...so: ... (7 Replies)
Discussion started by: dbiggied
7 Replies

7. UNIX for Dummies Questions & Answers

Delete records from a big file based on some condition

Hi, To load a big file in a table,I have a make sure that all rows in the file has same number of the columns . So in my file if I am getting any rows which have columns not equal to 6 , I need to delete it . Delimiter is space and columns are optionally enclosed by "". This can be ... (1 Reply)
Discussion started by: hemantraijain
1 Replies

8. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

9. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

10. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies
Login or Register to Ask a Question