-2

I have 2 files with some content/lines.

Example :

cat a.txt
  1
  2
  3
  4

cat b.txt
  a
  b
  c
  d

Need to get below output using for loop

 1 a
 2 b
 3 c
 4 d

I have tried the below script but it gives the wrong output

for i in `cat a.txt`
 do 
  for j in `cat b.txt`
   do 
     echo $i and $j
   done
 done

1 and a
1 and b
1 and c
1 and d
2 and a
2 and b
2 and c
2 and d
3 and a
3 and b
3 and c
3 and d
4 and a
4 and b
4 and c
4 and d

Kindly help me to do with For loop. Thanks in Advance

Beginner
  • 1,920
  • 5
  • 21
  • 34
  • 2
    A for loop is not the right solution for this, is this a homework question? – jesse_b Jun 15 '22 at 17:58
  • 3
    You have two nested loops, so of course you get all pairings. You'd need to read once from both files on each iteration. Or just use `paste a.txt b.txt` – ilkkachu Jun 15 '22 at 18:01
  • 2
    Related: [Why is using a shell loop to process text considered bad practice?](//unix.stackexchange.com/q/169716) – Stéphane Chazelas Jun 15 '22 at 18:16
  • @jesse_b is not a homework. I'm doing script for trunk port creation. For that I needed. I should take port id and host name to create the subport – Beginner Jun 15 '22 at 20:14
  • 1
    @Beginner In the comments, you mention that your data is different from what you show in the question. Could you please update the question with the _actual_ data and expected result? If `paste -d ' ' a.txt b.txt` is "the wrong answer" for you, then please also explain why. There is absolutely no need for any `for` loop to solve this. – Kusalananda Jun 15 '22 at 20:39
  • 1
    Are these the same two files you talk about in [your other recent question](https://unix.stackexchange.com/q/703545/116858)? If so, the two files may be of different lengths, and you should mention this in the question and incorporate this fact in the test data and the expected output. I also still lack an explanation as to why the obvious solution with `paste` is not acceptable to you. – Kusalananda Jun 16 '22 at 07:38
  • 1
    It would be helpful (to you) if you could answer my simple questions so that I understand your requirements and reasoning. Flagging any comment that says anything about `paste` or not using `for` loops or asking for further clarifications as "not needed" is not an optimal communication method. If we understood why you needed a `for` loop, then we would be able to formulate an answer that is ultimately helpful to you. As it stands right now, you have an answer that shows no fewer than 3 different ways to solve your issue (with variations), one of which employs a perfectly good `for` loop. – Kusalananda Jun 16 '22 at 09:28

1 Answers1

3

As @ilkkachu comments, paste is the best answer:

paste a.txt b.txt

or, with a single space as the delimiter in place of tab,

paste -d ' ' a.txt b.txt

For a shell-only answer, first don't read lines with for and Why is using a shell loop to process text considered bad practice?

You can use a single while loop to read lines from 2 files simultaneously

while IFS= read -r lineA <&3; IFS= read -r lineB <&4; do
    echo "$lineA $lineB"
done 3<a.txt 4<b.txt

if you like more whitespace for readability:

while IFS= read -r lineA <&3
      IFS= read -r lineB <&4
do
    echo "$lineA $lineB"
done 3<a.txt 4<b.txt

Another alternative: read each file into an array, then iterate over the array indices.

mapfile -t a < a.txt
mapfile -t b < b.txt
for idx in "${!a[@]}"; do printf '%s %s\n' "${a[idx]}" "${b[idx]}"; done
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
glenn jackman
  • 84,176
  • 15
  • 116
  • 168