Saturday 24 December 2011

filtering strings from text files

filtering strings from text files

I'm tring to create a bash script that removes text entries from text file 1
that are in file 2

text 1
Code:

abc
def
ghi

text 2
Code:

abc
ghi

my bash script so far
Code:

#!/bin/bash
cd /home/pouar/test
index=0
# read text 1
while read line ; do
        a[$index]="$line"
        index=$(($index+1))
done < ./1

index=0
# read text 2
while read line ; do
        b[$index]="$line"
        index=$(($index+1))
done < ./2
n=$((0))
m=$((0))
x=${#a[*]}
while ((n<x))
do
if [ "${a[$n]}"="${b[$m]}" ]; then #if string in text 1 is equal to string in text 2
n=$(($n+1)); #go to next line in text 1
m=$(($m+1)) #go to next line in text 2
else # or else
echo "${a[(($n))]}" #print string
n=$(($n+1)) #go to next line in text 1
fi
done

No comments:

Post a Comment