1

I am trying to delete the content of the file on the basis of time range.So far i have tried on the basis of the pattern matching as below:

sed -i '/2018-03-09T13:53/,/2018-03-09T14:00/!d' LogAgentServer

And content of file(LogAgentServer) like below:

2018-03-09T13:52:04.429+0000: 64.870: [GC (Allocation Failure) [PSYoungGen: 195634K->4192K(240640K)] 305630K->155712K(940032K), 0.1015550 secs] [Times: user=0.18 sys=0.02, real=0.10 secs] 
2018-03-09T13:52:23.085+0000: 83.525: [GC (Allocation Failure) [PSYoungGen: 147552K->1600K(145408K)] 299072K->156324K(844800K), 0.0113755 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
2018-03-09T13:52:36.438+0000: 96.879: [GC (Allocation Failure) [PSYoungGen: 144960K->1376K(238080K)] 299684K->156488K(937472K), 0.0085403 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
2018-03-09T13:52:47.174+0000: 107.615: [GC (Allocation Failure) [PSYoungGen: 132448K->1344K(132608K)] 287560K->156544K(832000K), 0.0067113 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
2018-03-09T13:52:58.583+0000: 119.023: [GC (Allocation Failure) [PSYoungGen: 132416K->1248K(240128K)] 287616K->156552K(939520K), 0.0072957 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
2018-03-09T13:53:13.075+0000: 133.516: [GC (Allocation Failure) [PSYoungGen: 131808K->1248K(239616K)] 287112K->156624K(939008K), 0.0142212 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:53:30.405+0000: 150.845: [GC (Allocation Failure) [PSYoungGen: 131808K->1792K(243712K)] 287184K->157248K(943104K), 0.0092942 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:53:43.380+0000: 163.820: [GC (Allocation Failure) [PSYoungGen: 137984K->4320K(241664K)] 293440K->160512K(941056K), 0.0141034 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:53:57.422+0000: 177.863: [GC (Allocation Failure) [PSYoungGen: 140512K->4512K(250880K)] 296704K->163920K(950272K), 0.0190071 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:55:07.187+0000: 247.627: [GC (Allocation Failure) [PSYoungGen: 153504K->3552K(247296K)] 312912K->166176K(946688K), 0.0113494 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:55:49.734+0000: 290.174: [GC (Allocation Failure) [PSYoungGen: 152544K->4256K(259072K)] 315168K->169024K(958464K), 0.0137216 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:56:24.438+0000: 324.878: [GC (Allocation Failure) [PSYoungGen: 169120K->4896K(254976K)] 333888K->172440K(954368K), 0.0231447 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:57:17.868+0000: 378.308: [GC (Allocation Failure) [PSYoungGen: 169760K->4880K(267264K)] 337304K->175953K(966656K), 0.0197401 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
2018-03-09T13:58:38.692+0000: 459.133: [GC (Allocation Failure) [PSYoungGen: 186128K->5216K(263168K)] 357201K->179796K(962560K), 0.0123329 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:59:51.920+0000: 532.361: [GC (Allocation Failure) [PSYoungGen: 186464K->5312K(275968K)] 361044K->184116K(975360K), 0.0134409 secs] [Times: user=0.03 sys=0.01, real=0.02 secs] 
2018-03-09T14:01:03.648+0000: 604.088: [GC (Allocation Failure) [PSYoungGen: 204352K->4832K(284160K)] 387421K->192292K(983552K), 0.0129953 secs] [Times: user=0.02 sys=0.01, real=0.01 secs] 
2018-03-09T14:01:30.629+0000: 631.069: [GC (Allocation Failure) [PSYoungGen: 219872K->5088K(280064K)] 407332K->196181K(979456K), 0.0104791 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 

But Problem with this command is ,if there is no entry for the above time range then it will not work, so How can i achieve that using comparing time range.
Like below:

sed -i '/2018-03-09T13:54/,/2018-03-09T14:00/!d' LogAgentServer

It should give below result:

2018-03-09T13:55:07.187+0000: 247.627: [GC (Allocation Failure) [PSYoungGen: 153504K->3552K(247296K)] 312912K->166176K(946688K), 0.0113494 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:55:49.734+0000: 290.174: [GC (Allocation Failure) [PSYoungGen: 152544K->4256K(259072K)] 315168K->169024K(958464K), 0.0137216 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
2018-03-09T13:56:24.438+0000: 324.878: [GC (Allocation Failure) [PSYoungGen: 169120K->4896K(254976K)] 333888K->172440K(954368K), 0.0231447 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:57:17.868+0000: 378.308: [GC (Allocation Failure) [PSYoungGen: 169760K->4880K(267264K)] 337304K->175953K(966656K), 0.0197401 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
2018-03-09T13:58:38.692+0000: 459.133: [GC (Allocation Failure) [PSYoungGen: 186128K->5216K(263168K)] 357201K->179796K(962560K), 0.0123329 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
2018-03-09T13:59:51.920+0000: 532.361: [GC (Allocation Failure) [PSYoungGen: 186464K->5312K(275968K)] 361044K->184116K(975360K), 0.0134409 secs] [Times: user=0.03 sys=0.01, real=0.02 secs]
1

Try this.

sed -e '/2018-03-09T13:5[4-9]/!d' LogAgentServer

  • 1
    Upvoted! But it would help if you explained a little, basically, that you are using a range in the regex. Besides, you want -i, not -e ;-) – thecarpy May 4 '18 at 11:15
  • 1
    @thecarpy since its a log file, it's better to use -e to print or redirect the o/p to a new file. – msp9011 May 4 '18 at 11:37
  • @SivaPrasath , Thanks this is working, but if i want to take time range as input from user along with the script that time it wont work, so any suggestion on this part? – Rahul Singh May 4 '18 at 14:50
  • @RahulSingh, you mean to say that you want to pass time range as a variable? – msp9011 May 4 '18 at 15:19
  • @SivaPrasath , Suppose this "trimData.sh" is my script and inside that i'm handling the this deleting of content form file.So below is the command : ./trimData.sh startTime endTime Then inside script i am writing like below: starttime="$1" endtime="$2" sed -i "/$starttime/,/$endtime/!d" LogAgentServer So, in this case how we can do? – Rahul Singh May 4 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.