Need help in comparision of two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in comparision of two strings
# 1  
Old 02-12-2012
Need help in comparision of two strings

I have two files. one files contain counts and second contain string. Now i have to compare two string . if two string are same i have to add count. If strings are same more than once i have to display final output.
like this

>Count.txt
1
3
12
4
5
6
1

>strings.txt
AA
BB
BB
BB
CC
CC
DD


Quote:
Need output like this

1AA
19 BB
11 CC
1 DD
here is my code what i am trying right now but its not give me correct out. please help me in this.

open(pr, "<", "count.txt");
my @count = <pr>;
close(pr);

open(pr1, "<", "string.txt");
my @st = <pr1>;
close(pr1);


for ($i = 0 ; $i <= $#id; $i++)
{
$j = $i + 1;
if ($st[$i] eq $st[$j])
{
$sa = $count[$i] + $count[$j];
print "$sa\n";
}

else
{
$sa = $count[$i];
print "$sa\n";
}
}



I can also merge these files. I need to add count based on string. And string file is sorted.

like this

1 AA
3 BB
12 BB
4 BB
5 CC
6 CC
1 DD

Quote:
Out put
1 AA
19 BB
11 CC
1 DD

Last edited by pjlotiya; 02-12-2012 at 10:22 AM..
# 2  
Old 02-12-2012
Hi pjlotiya,

One way using perl:
Code:
$ cat count.txt 
1
3
12
4
5
6
1
$ cat strings.txt 
AA
BB
BB
BB
CC
CC
DD
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <count-file> <strings-file>\n] unless @ARGV == 2;

open my $cfh, qq[<], shift or die qq[Cannot open file: $!\n];
open my $sfh, qq[<], shift or die qq[Cannot open file: $!\n];

my (%output);

while ( my $cl = <$cfh>, my $sl = <$sfh> ) {
        map { s/\A\s+//; s/\s+\Z// } $cl, $sl;
        $output{ $sl } += $cl;
}

for ( sort keys %output ) {
        printf qq[%d %s\n], $output{ $_ }, $_;
}
$ perl script.pl count.txt strings.txt 
1 AA
19 BB
11 CC
1 DD

Regards,
Birei
This User Gave Thanks to birei For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

3. Solaris

SPARCVII+ vs. T5 comparision

Hello, I need migrate our applications from M-series to T-series. I would like compare two different SPARC processors - SPARCVII+ from Fujitsu and T5 from Oracle. I found only next benchmarks: M8000: ... (9 Replies)
Discussion started by: olibertu
9 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. Shell Programming and Scripting

String comparision

I have a string like ab or abc of whatever length. But i want to know whether another string ( for example, abcfghijkl, OR a<space> bcfghijkl ab<space> cfghijkl OR a<space>bcfghijkl OR ab<space> c<space> fghijkl ) starts with ab or abc... space might existing on the longer string... If so, i... (1 Reply)
Discussion started by: nram_krishna@ya
1 Replies

6. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

7. Shell Programming and Scripting

File comparision

Hi All I have to files cat a.txt AAA BBB CCC DDD and cat b.txt AAA CCC EEE i want to compare these two files and o/p should have content of file a.txt which is not in file b.txt c.txt BBB DDD Please help me (3 Replies)
Discussion started by: aaysa123
3 Replies

8. Shell Programming and Scripting

File comparision

Hi All I have to files cat a.txt AAA BBB CCC DDD and cat b.txt AAA CCC EEE i want to compare these two files and o/p should have content of file a.txt which is not in file b.txt c.txt BBB DDD Please help me (1 Reply)
Discussion started by: aaysa123
1 Replies

9. Shell Programming and Scripting

while - comparision

Hi, Please find the attached scriplet and suggest me to fix the bug in this. ----------------------------------- noofdirs=`ls *.tar | wc -l` if ; then let i=1 while ( $i <= $noofdirs ) ; do echo $i mkdir $i file1=`ls *.tar | head -1` mv $file1 $i i =... (2 Replies)
Discussion started by: sharif
2 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question