Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. See Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case?Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case? if you want to keep processing until the end of both files.

See also When would you use an additional file descriptor?When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`?Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. See Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case? if you want to keep processing until the end of both files.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. See Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case? if you want to keep processing until the end of both files.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

moved discussion of processing beyond the end of one file to a separate question (thanks ixtmixilix)
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. Replace read … && read … bySee read … || read …Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case? if you want to keep processing the longest file ($lineA will be empty if you keep processing fileB and vice versa). Use IFS= read -r lineA; IFS= read -r lineB; if you want to process tilluntil the end of fileBboth files.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. Replace read … && read … by read … || read … if you want to keep processing the longest file ($lineA will be empty if you keep processing fileB and vice versa). Use IFS= read -r lineA; IFS= read -r lineB; if you want to process till the end of fileB.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. See Reading two files into an IFS while loop -- Is there a way to get a zero diff result in this case? if you want to keep processing until the end of both files.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

removed useless `count` variable (thanks ixtmixilix)
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

count=1
while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. Replace read … && read … by read … || read … if you want to keep processing the longest file ($lineA will be empty if you keep processing fileB and vice versa). Use IFS= read -r lineA; IFS= read -r lineB; if you want to process till the end of fileB.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

count=1
while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. Replace read … && read … by read … || read … if you want to keep processing the longest file ($lineA will be empty if you keep processing fileB and vice versa). Use IFS= read -r lineA; IFS= read -r lineB; if you want to process till the end of fileB.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

Open the two files on different file descriptors. Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read <&3.

while IFS= read -r lineA && IFS= read -r lineB <&3; do
  echo "$lineA"; echo "$lineB"
done <fileA 3<fileB

This snippet stops when the shortest file has been processed. Replace read … && read … by read … || read … if you want to keep processing the longest file ($lineA will be empty if you keep processing fileB and vice versa). Use IFS= read -r lineA; IFS= read -r lineB; if you want to process till the end of fileB.

See also When would you use an additional file descriptor? for additional information on file descriptors, and Why is `while IFS= read` used so often, instead of `IFS=; while read..`? for an explanation of IFS= read -r.

added 530 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k
Loading
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k
Loading