Read command restarts at beginning of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command restarts at beginning of file
# 1  
Old 01-24-2017
Read command restarts at beginning of file

I am reading in a file in a Korn shell script. The file has one header row followed by possibly millions of data rows. I want to do one thing to the header row and write it out to a new output file, then something different to the data rows. My code looks like this:

Code:
read header < $infile 
    lineout=...$header...
echo $lineout >> $outfile
 
while read dataline
do
   lineout=....$dataline.....
   echo $lineout >> $outfile
done < $infile

But when I do this the while loop reads the first line in again - so I get the header line twice. The pointer goes back to the start of the file, or it closes and reopens, not sure what is going on. I know I could put the header logic in the while loop and just test for the first line and do that coding there. But I'd rather not have to do that test millions of times when I know I only need to process the first line differently. Why does the file reader not move to the next line? I assume the "done < $infile" part is forcing it to start at the beginning again. Anyway to make this work this way?
# 2  
Old 01-24-2017
Every time you do <$infile, it's re-opening the file, just because that's what it means.

If you want to open the file only once, you can do this:

Code:
exec 5<$infile # Open into file descriptor 5

read header <&5 # Read from file descriptor 5.  It will remember its place.

while read dataline
do
...
done <&5

exec 5<&- # Close file descriptor 5

These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 01-24-2017
A redirection is opened, read/written, and closed for every single compound command that uses it (c.f. e.g. man bash).
You're redirecting stdin twice, so $infile is opened (and reset) twice. How about collecting all above into one "group command" and redirect its stdin?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-24-2017
Thank You Corona688. That's exactly what I was looking for. Works fine now.
This User Gave Thanks to DJR For This Post:
# 5  
Old 01-24-2017
Since there is only one input file and one output file in your original code:
Code:
read header < $infile 
    lineout=...$header...
echo $lineout >> $outfile
 
while read dataline
do
   lineout=....$dataline.....
   echo $lineout >> $outfile
done < $infile

you could put all of your script in a group and redirect the input and output of the group instead of redirecting each command in the group:
Code:
{   read header
    lineout=...$header...
    echo "$lineout"

    while read dataline
    do
        lineout=....$dataline.....
        echo $lineout
    done
} < "$infile" >> "$outfile"

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Du command not able to read file.

hi when i am trying to check the file size using du command in shell script but getting a below error, although the file is present du: cannot access `LOGS_18-08-20.tar.gz': No such file or directory below is snippet of script cd /home dt=$(date --date="yesterday" +"%y-%m-%d") du ... (3 Replies)
Discussion started by: scriptor
3 Replies

2. Shell Programming and Scripting

Read file and run a command

Hi I have jobs (some 1000) defined in a file and I want to read those jobs and run a a command. For example: jobs.txt abc efg I want to read the entire file and run the following command Delete -JOB "abc" Deleteing abc... Delete -JOB "efg" Delete efg... Can somebody help me... (4 Replies)
Discussion started by: karan8810
4 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Solaris

Unix restarts error in sar command

Hi Folks, I am again here with a peculier problem. Please see the code root@celdws01: # sar -u -f /var/adm/sa/sa21 SunOS celdws01 5.9 Generic_118558-34 sun4u 12/21/2011 10:10:09 %usr %sys %wio %idle 10:10:09 unix restarts Can anyone help me in... (9 Replies)
Discussion started by: vivek.goel.piet
9 Replies

5. UNIX for Dummies Questions & Answers

Any tips/suggestions for a newbie beginning to read the Linux Kernel Source Code

Hi All, I recently downloaded the Linux kernel source code, added them all to a project in MS VC++ and plan to read through it so that I can improve the way I code, read/understand a large code database and hopefully contribute something to the development of the Linux OS. I have taken a... (3 Replies)
Discussion started by: clavian
3 Replies

6. Shell Programming and Scripting

Command to remove numbers from beginning of txt file

Hello. I have the following issue: my txt file has the following format: train/dr4/fklc0/sx175.txt 0 80282 Severe myopia contributed to Ron's inferiority complex. train/dr4/fklc0/sx355.txt 0 42906 Dolphins are intelligent marine mammals. train/dr4/fklc0/sa2.txt With the... (1 Reply)
Discussion started by: li_bi
1 Replies

7. UNIX for Dummies Questions & Answers

Command to delete numbers at beginning of txt file line

Hello. I have the following issue: my txt file has the following format: train/dr4/fklc0/sx175.txt 0 80282 Severe myopia contributed to Ron's inferiority complex. train/dr4/fklc0/sx355.txt 0 42906 Dolphins are intelligent marine mammals. train/dr4/fklc0/sa2.txt awk 'NR%2==0' test1.txt >... (4 Replies)
Discussion started by: li_bi
4 Replies

8. Shell Programming and Scripting

Command filtering ONLY rows NOT beginning with '*'

I need a command which filters rows ONLY NOT beginning with '*' So far I have following NOT sufficient command, because it does not include ALL possible literals except of '*' grep ^ INPUT_FILE >>OUTPUT_FILE Is it possible to write something like grep NOT ^ INPUT_FILE... (3 Replies)
Discussion started by: ABE2202
3 Replies

9. Shell Programming and Scripting

command to read file name and size

hi all, there is any command or anything we can use to read the file name and size. thanks (6 Replies)
Discussion started by: s_linux
6 Replies

10. Shell Programming and Scripting

File deletion when server restarts

Hi, In a shell script I am makin use of 3 files f1,f2 and f3.txt. When the Unix server is restarted I want to delete all these 3 files if they are existing. ( I suppose I will have to use this command rm /thefilepath/f* but dont know in which script to use.) Anyone knows what can be... (6 Replies)
Discussion started by: k_oops9
6 Replies
Login or Register to Ask a Question