Gurus,
Need little guidance.
I have A.txt and B.txt file. B.txt file contains Unique strings.
Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file
And A.txt file contains the above string as a fourth column which is last column.
So A.txt file fourth column content is
while read line
do
COUNT=`grep "$line" A.txt | wc -l`
echo $line "," $COUNT >> output.txt
done < B.txt
Output.txt file displays output
Here ghjjj should display 1 instead 2...
Can anybody guide me the better way to get this.
Thanks in advance!!!
ctsgnb
October 13, 2010, 4:37am
2
Take a look at the man pages of command "uniq" and "comm"
uniq -c A.txt
Note that this assume A.txt has previously been sorted.
sort A.txt | uniq -c
birei
October 13, 2010, 4:48am
3
Hi,
I would change a little bit your script, test this:
#!/bin/sh
while read line; do
count=$(grep "^${line}$" a.txt | wc -l)
echo "$line $count"
done < b.txt
Regards,
shirisha:
Gurus,
Need little guidance.
I have A.txt and B.txt file. B.txt file contains Unique strings.
Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file
And A.txt file contains the above string as a fourth column which is last column.
So A.txt file fourth column content is
while read line
do
COUNT=`grep "$line" A.txt | wc -l`
echo $line "," $COUNT >> output.txt
done < B.txt
Output.txt file displays output
Here ghjjj should display 1 instead 2...
Can anybody guide me the better way to get this.
Thanks in advance!!!
That's because in A.txt, the fourth line also contains "ghjjj", so it greps 2
So with awk...
awk 'NR==FNR{arr[$0]++;next}arr[$0]{print $0,arr[$0]}' A.txt B.txt
You can modify your script like this:
while read -r line
do
COUNT=$(grep -Fcx "$line" A.txt)
echo "$line , $COUNT"
done < B.txt > output.txt
It is similar to birei's suggestion
malcomex999:
That's because in A.txt, the fourth line also contains "ghjjj", so it greps 2
So with awk...
awk 'NR==FNR{arr[$0]++;next}arr[$0]{print $0,arr[$0]}' A.txt B.txt
yeah its correct but the above solution is not working...
---------- Post updated at 05:11 AM ---------- Previous update was at 04:59 AM ----------
birei:
Hi,
I would change a little bit your script, test this:
#!/bin/sh
while read line; do
count=$(grep "^${line}$" a.txt | wc -l)
echo "$line $count"
done < b.txt
Regards,
hi..
I am getting 0 for every line....i thing that grep wc -l is not working...
i tried giving in ` ` but giving error that command not found
What error do you get when you run it?
Use nawk if you are on Solaris...
nope..i am on linux..it doesnot show any output...tried to redirecting to other file as well..