使用if语句嵌套for循环会给出太多答案

时间:2011-10-07 14:00:46

标签: bash

我想逐行比较一个文本文件和另一个文本文件,以找出文本文件2中出现相同文本的次数。问题是我得到的循环太多了。 我该如何解决这个问题?

#!/bin/bash
# Read text file
echo "Enter file name"
read fname

# Read text file
echo "Enter file name"
read fcheck

# rm out2.txt
c1=0

for i in $(cat $fname);

do
    for j in $(cat $fcheck);
    do

        if [[ $i == $j ]]
        then
            let c1=c1+1;
        fi

    done

    echo $c1 # >> out2.txt
    c1=0;
done

3 个答案:

答案 0 :(得分:1)

for循环的问题在于他们逐字逐句地读取文件。相反,做这样的事情:

while read line_a
do
    while read line_b
    do
        if [ "$line_a" = "$line_b" ]
        then
            let c1=c1+1;
        fi
    done < "$fcheck"

    echo $c1
    c1=0;
done < "$fname"

养成将变量括在引号中的习惯,例如"$var",以避免出现空格问题。

答案 1 :(得分:1)

comm正是您所需要的:

common_lines=$(comm -12 <(sort "$fname") <(sort "$fcheck"))
printf "%d common lines:\n" $(wc -l <<< "$common_lines")
echo "$common_lines"

答案 2 :(得分:0)

我会做

 fname=file1.txt
 fcheck=file2.txt

 cat "$fname" | while read line 
 do
     echo -e "$(fgrep -c "$line" "$fcheck")\t$line"
 done