Read from last point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from last point
# 1  
Old 07-02-2016
Read from last point

Hi again,

first thanks for all your suggestions. This forum it is very useful.
I have a question. Is it possible to read from the last line a file was closed. For example, imagine that i've got a file with a LOT of timestamps :

Code:
1467387616.868717770
1467387616.874189609
1467387616.888759180
1467387616.894188020
1467387616.908742591
1467387616.914199431
(..)

and i need to read it in block of 70 lines each time, BUT since the file is updating quite fast i need to return to read it form the last (awk?) command without missing any line.

i don't know itf i render the idea or if it's possible. Maybe i need a program instead.

Thanks!
Kind Regards
# 2  
Old 07-02-2016
You can save the last timestamp processed by your awk script and reload that value in your shell script before calling awk the next time and then have awk skip over all lines with a timestamp that is less than or equal to the saved timestamp. For example:
Code:
#!/bin/ksh
IAm=${0##*/}
TSF="$HOME/.LastTimeStamp"

if [ ! -f $TSF ]
then	printf '%s: Time stamp file (%s) not found.\n\tProcessing all timestamps.\n' \
	    "$IAm" "$TSF" >&2
	ts=0
else	read ts < "$TSF"
fi
awk -v TS="$ts" -v TSF="$TSF" '
$1 + 0 <= TS {
	# Skip previous processed timestamps.
	next
}
{	# Do what every you want to do with the new timestamp...
	# Save last completed time stamp.
	print $1 > TSF
	close(TSF)
}' input_file...

Obviously, you should also verify that the read was successful, but this might work as a starting point for what you need.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-02-2016
i'll give a try. Thanks for your support.

Kind Regards
# 4  
Old 07-04-2016
Using Perl
Code:
#!/usr/bin/perl
open (FH,"/path/to/file.txt") or die "cannot open file \n";
my $pos=0;
while (1) {
  seek(FH,$pos,0);
  while (<FH>) {
    print $_;
  }
  $pos=tell(FH);
}

# 5  
Old 07-05-2016
Code:
#!/bin/bash
f=0
while [ ${f} -eq 0 ] ; do
    for i in {1..70} ; do
        read a || { f=1 ; break ; }
        echo "${a}"
    done
done < <(tac yourfile.txt)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a new mount point with 600GB and add 350 GBexisting mount point? IN AIX

How to create a new mount point with 600GB and add 350 GBexisting mount point Best if there step that i can follow or execute before i mount or add diskspace IN AIX Thanks (2 Replies)
Discussion started by: Thilagarajan
2 Replies

2. Shell Programming and Scripting

Read from last point

Hi again, first thanks for all your suggestions. This forum it is very useful. I have a question. Is it possible to read from the last line a file was closed. For example, imagine that i've got a file with a LOT of timestamps : 1467387616.868717770 1467387616.874189609... (1 Reply)
Discussion started by: Board27
1 Replies

3. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

4. Linux

Make directory used as mount point read-only

For my backup , I mount and external hard disk to /mnt/mybackup and then I do an rsync to /mnt/mybackup If for some reason the rsync fails, I want to prevent it from writing data on the server hard disk itself since the external hard disk will no longer be mounted on it. I want /mnt/mybackup... (8 Replies)
Discussion started by: coolatt
8 Replies

5. Shell Programming and Scripting

How to perform a hexdump using dd from start point to end point?

hi, I would like to ask or is it possible to dump a hex using dd from starting point to end point just like the "xxd -s 512 -l 512 <bin file>" I know the redirect hexdump -C but i can't figure it out the combination options of dd. Hope someone can share their knowledge.. Thanks in... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

8. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

9. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

10. UNIX for Advanced & Expert Users

Fibre connection Point to Point SUN

Anyone know of a guide or instructions for Solaris I got to configure a SBUS HBA to talk to a tape robot. I have done this on a switch but not point to point. just going HBA >>>>> TAPE Fibre simple two nodes Kie (6 Replies)
Discussion started by: kie
6 Replies
Login or Register to Ask a Question