Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

for example if we have a file contains :

Hello world
Hello earth
Hi everybody

we can easily parse it to get the first and second columns of the lines contains the word Hello using awk:

awk '/Hello/ { print $1 }' file
awk '/Hello/ { print $2 }' file

What if the content of the file is changed:

Hello world
Hello earth
Hi everybody
Hello sky
Hi madame
Hello USA

How can we parse only the new entries added to this file (using awk):

Hello sky
Hi madame
Hello USA

without re-parsing the already parsed Information?

Hello world
Hello earth
Hi everybody
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can use two files:

if [ -e checkfile ]; then
  lines=$(wc -l <checkfile)
else
  lines=0
fi
# read all new lines from source file and append them to target file
sed -n $((lines+1)),\$p file >>checkfile
awk '...' checkfile
share|improve this answer

You can pipe the file's contents via tail -f (or tail -F) e.g.

tail -f file | awk '...'

From the tail man page:

   -f, --follow[={name|descriptor}]
          output appended data as the file grows; -f, --follow, and --fol‐
          low=descriptor are equivalent
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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