Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read line by line from two files simultaneously in shell script

Tags:

file

bash

shell

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?

like image 577
ALBI Avatar asked May 28 '15 21:05

ALBI


1 Answers

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.

like image 178
John1024 Avatar answered Oct 12 '22 23:10

John1024