Re: Shell question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 27Dec2016 15:49, JD <jd1008@xxxxxxxxx> wrote:
Reading lines from 2 files in such a way that each iteration
lets me read the next line from each file so that the items
read from each file are in sync as far as line number is concerned.
Is this "doable"?

For a single file, indexing to next line is automatic, visa vis
while read line; do
echo $line
done < some_file.txt

for when more than 1 file is involved, it is not so easy ...

Hypothetical (of course, non-working example):

export index=1
while true do;
file_A_line=`read line$index`  # Read from file A
file_B_line=`read line$index`  # read from file B
[ x$file_A_line = x -o x$file_B_line = x ] && break
<do something withfile_A_line and file_B_line>
index=`expr $index + 1`
done

So how can the reading from 2 files be accomplished and keep the index to next line in sync?

It is easy in the shell, too.

Untested:

 exec 3<file1
 exec 4<file2
 while read -r line1 <&3 \
       && read -r line2 <&4
 do
   ... do stuff with $line1 and $line2
 done

That's the direct thing. There are other things, depending on what you're trying to achieve.

Cheers,
Cameron Simpson <cs@xxxxxxxxxx>
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]
  Powered by Linux