1753274 Members
4928 Online
108792 Solutions
New Discussion юеВ

Re: Help scripting

 
SOLVED
Go to solution
Vogra
Regular Advisor

Help scripting

Hi All!
i have 2 files and i need check one with the contents that exist in another.

lista1.txt and lista2.txt

like this:

for each line in lista1.txt
do
is it equal any line in lista2.txt ?
send line to lista3.txt
done

Thanks,
Lima.
We are spirits in the material world
10 REPLIES 10
RAC_1
Honored Contributor
Solution

Re: Help scripting

for i in $(do
grep -iq "grep_string" file2 > /dev/null 2>&1
if [[ $? -eq 0 ]]
then
echo ${i} >> file3
fi

Also check comm, diff, uniq commands.

comm -12 file1 file2 > file3

Anil
There is no substitute to HARDWORK
KapilRaj
Honored Contributor

Re: Help scripting

cat lista1.txt |while read line
do
grep "$line" lista2.txt >> lista3.txt
done

Kaps
Nothing is impossible
A. Clay Stephenson
Acclaimed Contributor

Re: Help scripting

I would leverage the comm comand for this. This should do it:
----------------------------------------
#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}
typeset T1=${TDIR}/T${$}_1.txt
typeset T2=${TDIR}/T${$}_2.txt

typeset PROG=${0##*/}

trap 'eval rm -f ${T1} ${T2}' 0 1 2 15

typeset -i STAT=0
if [[ ${#} -eq 2 ]]
then
typeset F1=${1}
typeset F2=${2}
shift 2
if [[ -r ${F1} && -r ${F2} ]]
then
sort ${F1} > ${T1}
sort ${F2} > ${T2}
comm -12 ${T1} ${T2}
else
echo "Can't read file ${F1} and/or ${F2}." >&2
STAT=254
fi
else
echo "${PROG} requires 2 args" >&2
STAT=255
fi
exit ${STAT}

---------------------------------------

Use it like this:

comm.sh lista1.txt lista2.txt > lista3.txt
If it ain't broke, I can fix that.
Vogra
Regular Advisor

Re: Help scripting

thank you so much!
We are spirits in the material world
Victor Fridyev
Honored Contributor

Re: Help scripting

Lima,
Try this, it works

sort lista1.txt |uniq >l1.txt
sort lista2.txt |uniq >l2.txt
cat l1.txt l2.txt |uniq -c | awk '$1>1 {print}' |cut -f2- " "


HTH
Entities are not to be multiplied beyond necessity - RTFM
Hein van den Heuvel
Honored Contributor

Re: Help scripting

Why not use file 2 as a pattern file?

grep -f lista2.txt lista1.txt > lista3.txt

Example:

$ cat x1
aap
noot
mies
teun
$ cat x2
mies
noot
vuur
$ grep -f x2 x1
noot
mies


hth,
Hein.
Cem Tugrul
Esteemed Contributor

Re: Help scripting

Hi Vogra;

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=754581

i have asked before and found very interesting solutions.

Good Luck;
Our greatest duty in this life is to help others. And please, if you can't
Nguyen Anh Tien
Honored Contributor

Re: Help scripting

I like this simple solution like Kapil solution
cat lista1.txt |while read var
do
grep "$var" lista2.txt >> lista3.txt
done
HTH
tienna
HP is simple
Amit Agarwal_1
Trusted Contributor

Re: Help scripting

There is one issue with the above solutions. If there is a duplicate entry in file1, then it goes undetected using grep.

$ cat file1
aaa
bbb
aaa
$ cat file2
aaa
bbb
ccc

The above solution would still show that file2 contains file1,which is incorrcet.

I believe the best solution would be to sort the two files and then use diff.

$ cat compare.sh
sort file1 -o file1.sort
sort file2 -o file2.sort
diff file1.sort file2.sort > /tmp/diffout
grep '^< ' /tmp/diffout
if [ $? -eq 0 ]
then
echo "file1 is not a subset of file2"
else
echo "file1 is subset of file2"
fi


-Amit