xzk
xzk

Reputation: 877

Linux - Diff number of lines in 2 set of files

I have 2 folders which contains 2 set of files.

Folder1

input1.csv
input2.csv
...

Folder2

output1.json
output2.json
...

Ideally, the number of lines in input1 should be same as output1, the number of lines in input2 should be same as output2, and so on.

I need a Linux command to check this automatically and tell me which files are different.

Basically,

If wc -l input1 == wc -l output1, 
    then skip;
else
    show input1 file name (or output1 file name)

repeat for all other files.

How can I achieve this?

Upvotes: 0

Views: 38

Answers (2)

KamilCuk
KamilCuk

Reputation: 140960

  • list filenames from both folders
  • join the list of both folders with matching filenmaes
  • for each two filenames
    • get count of lines
    • compare it
      • "show input1 file name"

The following is some code you can start with:

list_sorted_filenames() {
  find "$1" -maxdepth 1 -mindepth 1 -type f -printf "%f\n" | sort
}
paste <(list_filenames "Folder1") <(list_filenames "Folder2") |
while IFS=$'\t' read -r input output; do
   if (( $(wc -l <"$input") != $(wc -l <"$output") )); then
         echo "input=$input
   fi
done

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30565

something like this might help

arr=("1.in" "2.in")
arr2=("1.out" "2.out")

for i in ${!arr[@]}; do  
  v_in=$(wc -l < ${arr[$i]}) ; 
  v_out= $(wc -l < ${arr2[$i]}) ;

  if [ v_in -ne v_out ] then
     echo "not equal"
  else
     echo "equal"
  fi
done

Upvotes: 2

Related Questions