Read line by line of two files at once in a shell script

I have two files:

One: /tmp/startinghas the following contents:

15
30
45

Two: /tmp/endinghas the following contents:

22
35
50

I want to read these files in turn at the same time and use them in another command. For instance,

sed -n '15,22p' myFilePath
sed -n '30,35p' myFilePath
sed -n '45,50p' myFilePath

How to do this in the script shell?

+4
source share
1 answer

You can get the lines you want from the command paste:

$ paste -d, starting ending
15,22
30,35
45,50

You can use this with the command sedas follows:

while read range
do 
    sed -n "${range}p" file
done < <(paste -d, starting ending)

The design <(...)is called process replacement. The space between the two <is significant.

+4
source

All Articles