1

I am trying to compare 2 x files. If the contents are not commons, I want to echo them.

Here is my code thus far

#!/bin/bash


while IFS= read -r line && IFS= read -r line2 <&3; do
  
 if [ "$line" -ne "$line2" ]; then
    echo "we  doing this $line2"
    
  else
    echo "we will not  do this $line2"
  fi

done <file1test 3<file2test

The contents of each file which I am testing are just numbers. file1test is from 1..10 per line and file2test if from 1..20 per line.

My code only echos 1..10 which is common to both.

3
  • 2
    Read man comm. Commented Jun 13, 2021 at 18:00
  • 4
    Why trying to re-invert the wheel when "diff" and "comm" commands are readily available? Commented Jun 13, 2021 at 18:27
  • 1
    If you're asking why it only "echos 1..10" then remember IFS= read -r line && IFS= read -r line2 <&3 is only true if both reads succeed Commented Jun 13, 2021 at 18:50

1 Answer 1

1

Got it working, with the use of a status flag

while IFS= read -r line
do
   
 
  status=0
  while IFS= read -r line2 <&3
  do

  if [ "$line" = "$line2" ]; then
     status=1
     break 
  fi
 
done 3< file1test
if [ "$status" != 1 ]; then
  echo "$line"
fi
done < file2test

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.