ALBI
ALBI

Reputation: 721

Read line by line from two files simultaneously in shell script

I have two files:

One: /tmp/starting has following content:

15
30
45

Two: /tmp/ending has following content:

22
35
50

I want to read these files line by line simultaneously and use them in another command. For Example,

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

How Can I do this in Shell Script?

Upvotes: 5

Views: 1522

Answers (1)

John1024
John1024

Reputation: 113834

You can get the strings that you want from the paste command:

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

You can use this with your sed command as follows:

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

The construct <(...) is called process substitution. The space between the two < is essential.

Upvotes: 5

Related Questions