list - How to either read from one file or another in bash? -


i have 2 files trying read line line, want continue reading file or other on given iteration of loop. (i unsure how check eof). here pseudocode:

#initialize variables line1=read <file1.txt line2=read <file2.txt  #compare lists while true     #check if there match     if [[ "$line1" == "$line2" ]]             echo match         break     elif [ "$line1" -lt "$line2" ]            line1=read <file1.txt    # <-should read next line of f1     else        line2=read <file2.txt    # <-should read next line of f2     fi      #check eof     if [[ "$line1" == eof || "$line2" == eof ]]              break     fi done 

obviously, stands now, continue reading first line of f1 , f2. appreciated!

you need open each file once, don't reset file pointer before each read. read have non-zero exit status time tries read past last line of file, can check terminate loop.

{   read line1 <&3   read line2 <&4    while true;     #check if there match     if [[ "$line1" == "$line2" ]];       echo match       break     elif [ "$line1" -lt "$line2" ];       read line1 <&3 || break     else       read line2 <&4 || break     fi   done } 3< file1.txt 4< file2.txt 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -