Prev: Q. How do I get bash to respect my physical directory structure?
Next: quoting: $foo, $(foo), ${foo} ..
From: master44 on 7 Jan 2010 19:04 I looking to write a bash shell script to parse a log file for the first occurance of a date and then once that date is found, the contents of the rest of the log file will be copied to a new file. Here is a simplified example of a log file (test.log) 2010-01-05 .... 2010-01-06 .... 2010-01-07 .... 2010-01-07 .... 2010-01-07 .... So for example I want to search for the first occurance of 2010-01-07 and then copy the rest of the file to anther file (test2.log) So the new file (test2.log) would look like this: 2010-01-07 .... 2010-01-07 .... 2010-01-07 .... I am thinking this can be done with sed and I have tried the following, but something is off... grep "2010-01-07|sed 's/^0//g'`" test.log|tail +2|head -1 > test2.log If anyone has done this before or has any ideas, I'd appreciate it! Thanks
From: Kevin Collins on 7 Jan 2010 20:14 On 2010-01-08, master44 <trpost(a)gmail.com> wrote: > I looking to write a bash shell script to parse a log file for the > first occurance of a date and then once that date is found, the > contents of the rest of the log file will be copied to a new file. > > Here is a simplified example of a log file (test.log) > > 2010-01-05 .... > 2010-01-06 .... > 2010-01-07 .... > 2010-01-07 .... > 2010-01-07 .... > > So for example I want to search for the first occurance of 2010-01-07 > and then copy the rest of the file to anther file (test2.log) > > So the new file (test2.log) would look like this: > > 2010-01-07 .... > 2010-01-07 .... > 2010-01-07 .... > > I am thinking this can be done with sed and I have tried the > following, but something is off... > > grep "2010-01-07|sed 's/^0//g'`" test.log|tail +2|head -1 > test2.log ^^ You need to tell grep the file name - you are combining the grep into your sed command! > If anyone has done this before or has any ideas, I'd appreciate it! Using perl, you could do: perl -ne '$cnt++ if (/^2010-01-07 /); print if ($cnt)' test.log > test2.log Kevin
From: Ed Morton on 7 Jan 2010 20:37 On 1/7/2010 6:04 PM, master44 wrote: > I looking to write a bash shell script to parse a log file for the > first occurance of a date and then once that date is found, the > contents of the rest of the log file will be copied to a new file. > > Here is a simplified example of a log file (test.log) > > 2010-01-05 .... > 2010-01-06 .... > 2010-01-07 .... > 2010-01-07 .... > 2010-01-07 .... > > So for example I want to search for the first occurance of 2010-01-07 > and then copy the rest of the file to anther file (test2.log) > > So the new file (test2.log) would look like this: > > 2010-01-07 .... > 2010-01-07 .... > 2010-01-07 .... > > I am thinking this can be done with sed and I have tried the > following, but something is off... > > grep "2010-01-07|sed 's/^0//g'`" test.log|tail +2|head -1> test2.log > > If anyone has done this before or has any ideas, I'd appreciate it! > sed is an excellent tool for simple substitutions on a single line, for anything else use awk, perl, etc. In this case: awk '/2010-01-07/,0' test.log > test2.log Ed.
From: Seebs on 7 Jan 2010 20:18 On 2010-01-08, master44 <trpost(a)gmail.com> wrote: > I looking to write a bash shell script to parse a log file for the > first occurance of a date and then once that date is found, the > contents of the rest of the log file will be copied to a new file. sed -n -e '/2010-01-07/,$p' -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Bill Marcum on 7 Jan 2010 20:36 On 2010-01-08, master44 <trpost(a)gmail.com> wrote: > > So for example I want to search for the first occurance of 2010-01-07 > and then copy the rest of the file to anther file (test2.log) > > So the new file (test2.log) would look like this: > > 2010-01-07 .... > 2010-01-07 .... > 2010-01-07 .... > > I am thinking this can be done with sed and I have tried the > following, but something is off... > > grep "2010-01-07|sed 's/^0//g'`" test.log|tail +2|head -1 > test2.log > > If anyone has done this before or has any ideas, I'd appreciate it! > sed -n '/2010-01-07/,$p' test.log >test2.log
|
Next
|
Last
Pages: 1 2 Prev: Q. How do I get bash to respect my physical directory structure? Next: quoting: $foo, $(foo), ${foo} .. |