2

I'm looking for a solution to a smple variant to this tail question...hoping someone will know the solution. Basically I want to tail a file until a string/pattern is matched and then write the contents out from the end of the file up until the line containing that pattern (or up to that pattern). The solution at this link, with a slight modification to re-direct output to a new file:

sh -c 'tail -n +0 --pid=$$ -f tmp.log | { sed "/pattern/ q" && kill $$ ;}' >& tmp.txt 

gave me the exact opposite (contains the file from the top upto the string/pattern match). Appreciate any ideas - thanks for your time.

Community
  • 1
  • 1
squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36
  • so want lines in the reversed order? Last line in the file should go as first and so on up to the pattern? Or simply want print out the content of the file from the last occurence of the pattern up to the end of file? – clt60 May 13 '13 at 15:17
  • Yes, the latter in your description. (Not reversed order). – squashed.bugaboo May 13 '13 at 15:19

3 Answers3

1

Use this instead:

sed -n '/pattern/,// p' >tmp.txt <tmp.log

It looks like you don't want the -f flag for tail, since that keeps trying to read past the end of file (in case someone else appends to the file). That takes care of the need to kill tail when you find pattern, and in fact it eliminates the need to use tail altogether.

Sir Athos
  • 9,403
  • 2
  • 22
  • 23
  • Ah..you might be right? Why the heck do I need tail here? Duh...let me try this. Thanks. – squashed.bugaboo May 13 '13 at 15:21
  • That worked in one simple case; but when I tried on another with multiple of the patterns, it gave me an unexpected result...need it to write out until the first occurrence of the pattern. – squashed.bugaboo May 13 '13 at 15:41
1

Because it is not clear for me from where you want print, e.g. from the FIRST occurrence of the pattern or the LAST one - here is the solution for both.

For the example will use the next "logfile"

line1 aa
line2 bb
line3 cc
line4 bb
line5 ee
line6 bb
line7 gg

and will search for the pattern bb.

#!/bin/bash
PATTERN="bb"
LOGFILE="/tmp/logfile"
tail -r "$LOGFILE" | sed -n '1,/bb/p' | tail -r

will print out lines from the LAST occurrence of the bb up to the end of file

line6 bb
line7 gg

the

#!/bin/bash 
PATTERN="bb"
LOGFILE="/tmp/logfile"
sed -n "/$PATTERN/,\$p" < $LOGFILE

will print out - from the 1st occurence of the pattern up to the end of file

line2 bb
line3 cc
line4 bb
line5 ee
line6 bb
line7 gg
clt60
  • 62,119
  • 17
  • 107
  • 194
  • Thanks jm666..found a one-liner answer (I posted below) that met my requirements..yes, I wanted to extract from the first occurrence of the pattern upto end of file and write that to another file, say.. – squashed.bugaboo May 13 '13 at 16:16
  • 1
    @squashed.bugaboo : the `sed -n '/pattern/,$p' < log >tmp` is not enough oneliner? ;) :) But sure, the awk is cool ;) – clt60 May 13 '13 at 16:21
0

OK...I think after looking around, I found what I think is the solution to my problem. Apologies for changing the definition of the problem mid-way (multiple occurrences of the pattern). Here's the fix using awk:

awk '/pattern/{i++}i' tmp.log >& tmp.txt

Thanks to everyone who commented/posted with ideas. Appreciate it.

squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36