9

I have a file, file1.csv containing:

This
is
some
text.

I am using while read line to cycle through each line, e.g.:

while read line; do
    echo $line
done < file1.csv

I have another file, with an identical number of lines, called file2.csv:

A
B
C
D

The data each line corresponds to data in the first file of the same line number.

  • How can I modify the while loop, such so that it can print the corresponding line from file2.csv?

3 Answers 3

15

Use another FD.

while read line; do
  if ! read -u 3 line2
  then
    break
  fi
  echo "$line***$line2"
done < file1.csv 3< file2.csv
Sign up to request clarification or add additional context in comments.

Comments

10

You could try with the paste utility:

$ cat one
this
is
some
text
$ cat two
1
2
3
4
$ while read a b ; do echo $a -- $b ; done < <(paste one two)
this -- 1
is -- 2
some -- 3
text -- 4

Comments

9

You can use the paste command:

$ paste -d, file{1,2}.csv | while IFS=, read x y; do echo "$x:$y"; done
This:A
is:B
some:C
text.:D

1 Comment

Skip the -d in the paste and skip the IFS=, in the while. Let whitespace be your delimiter. (Won't work if you have spaces in your data... but the above solution is broken if you have commas in your data, so same difference.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.