While loop reading a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop reading a record
# 8  
Old 03-25-2009
I typically take one of two approaches
1. capture the current line count to a variable in a script that remains in memory
2. capture the current line count to a file so you can read it in the next time you need it
The next time you read fileA use tail +$previous_line_count >> fileB
# 9  
Old 03-26-2009
this is what i have right now:
INFILE=input.txt
OUTFILE=output.txt

LINECOUNT= wc -l < $INFILE
OUTCOUNT=$LINCOUNT
tail -f $INFILE + $OUTCOUNT >> $OUTFILE

Right now with the input file being the following:
1
2
3
4
5
6
7
8
9

with running this
I am only getting:
5
6
7
8
9
in the output.txt

Also how do i put this in while loop?

The idea is the input.txt file is going to be updated regularly.So with the original input being:
1
2
3
4
5
6
7
8
9
If the file is update to put:
10
12
13
at the end file how do i add that to the end of the output.txt file in a while loop
# 10  
Old 03-26-2009
Here is an option that will allow the script to run until a time in the future

Code:
INFILE=input.txt
OUTFILE=output.txt
PREV_LINECOUNT=0
CURRENT_TIME=`date +%H%M`

# # time format HHMM
EXIT_TIME="$1"

# # keep gather data until you reach exit time
until (( $CURRENT_TIME >= $EXIT_TIME )); do
  LINECOUNT=`wc -l < $INFILE`
  CURRENT_TIME=`date +%H%M`
  tail +${PREV_LINECOUNT} $INFILE >> $OUTFILE
  PREV_LINECOUNT=`expr $LINECOUNT + 1`
  # # wait 1 min and get data again
  sleep 60
done

exit 0

If you want it to run until you tell it to stop then replace the until line with a while loop that will require you to use ctrl+c to exit the script
Code:
while true; do

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

2. Shell Programming and Scripting

ksh while read loop breaks after one record - AIX

#!/bin/ksh for SRV in imawasp01 \ imawasp02 \ imawasp03 \ imawasp04 \ imawasp05 \ imawasp06 \ imawasp07 \ imawasp08 \ imawasp09 do print "${SRV}" while read PASSLINE do SRVNAME=`echo ${PASSLINE} | awk -F\: '{print $1}'` LASTLOGIN=`ssh ${SRV} lsuser ${SRVNAME} | tr '... (2 Replies)
Discussion started by: port43
2 Replies

3. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

Unable to read the first space of a record in while loop

I have a loop like while read i do echo "$i" . . . done < tms.txt The tms.txt contians data like 2008-02-03 00:00:00 <space>00:00:00 . . . 2010-02-03 10:54:32 (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

Parsing issue while reading excel having 3000 record

Hi Need urgent help (2 Replies)
Discussion started by: premp26
2 Replies

6. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

7. UNIX for Dummies Questions & Answers

Reading only first record from the multipe directories

Hi All, I have a requirement, I had a parent directory Land under that we have sub directories Yesterday, Today and Tommorrow And we have a file test.txt under the above directories Yesterday, Today and Tommorrow The data in the file test.txt under Yesterday folder is ... (5 Replies)
Discussion started by: somu_june
5 Replies

8. Shell Programming and Scripting

Reading a file (one record) in a SHL script

I am trying to read a file in a shl script (only one record) and stored in a variable file_number I got the following read -u $BANNER_HOME/xxxxxxx/misc/EFTSQL.dat file_number file_number2 = $file_number + 1 echo $file_number2 > $BANNER_HOME/xxxxxx/misc/EFTSQL.dat EOF It is not working... (2 Replies)
Discussion started by: rechever
2 Replies

9. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies

10. Shell Programming and Scripting

problem in reading a record from the file

Hi Guys, I need to check whether the last column is RP, If so, then i have to second column and pass it to a select statement as sonid and fetch the value to a variable and run it. This has to be done till the last column is RW. value Fatherid sonid topid ... (8 Replies)
Discussion started by: mac4rfree
8 Replies
Login or Register to Ask a Question