Connecting Tech Pros Worldwide Forums | Help | Site Map

how to compare values from 2 different filenames?

Newbie
 
Join Date: Nov 2008
Posts: 6
#1: Nov 4 '08
pls help.. (sh scripting)
I have 3 files, file1 & file2 both have thousands of records of mobile numbers, while updatefile.txt is for the output. what i want to do is to compare the mobile field of file1 to file2, if the mobile number in file1 have been activated then the result appends to updatefile.txt while if the number is not yet activated then it will do nothing and will not displayed in updatefile.txt. (I have done similar to this, but my work needs to input one mobile number each time and then output result in updatefile.txt but since it has thousands of records i think my recent work is not applicable). I hope you can help me. thanks in advance!!

i have 3 files --> file1.txt and file2.txtupdatefile.txt
file1.txt
[HTML]
Seq.No SERIAL# mobile Box Batch Area
1 5512 09063924944 123 11 Laguna
2 8716 09063944249 123 11 Ohayo
.
.
.
(thousands of records)
[/HTML]

file2.txt
[HTML]
mobile activated_date
09063924944 20080615
09256458654 20080417
.
.
(thousands of records)
[/HTML]

updatefile.txt
[HTML]
mobile activated_date
09063924944 20080615
.
.
.
(displayed other activated mobile nos.) [/HTML]
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#2: Nov 4 '08

re: how to compare values from 2 different filenames?


Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. for i in `more +2 f1.txt | awk '{print $3}'`
  3. do
  4.         pat=`grep -ri $i f2.txt`
  5.         if [ $? -eq 0 ]
  6.         then
  7.                 echo $pat >> f3.txt
  8.         fi
  9. done
  10.  
This should work for you....
f3.txt will contain the desire output
Newbie
 
Join Date: Nov 2008
Posts: 6
#3: Nov 6 '08

re: how to compare values from 2 different filenames?


great, it works!

but i have questions what does "more +2" (line 2) and "-ri" (line 4) use for?
just curious.. thank u very much for helping me.
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#4: Nov 6 '08

re: how to compare values from 2 different filenames?


Quote:

Originally Posted by companion98

great, it works!

but i have questions what does "more +2" (line 2) and "-ri" (line 4) use for?
just curious.. thank u very much for helping me.



Look at f1 file....here we are interested in mobile numbers which present on second line. so we must skip first line, which is:

"Seq.No SERIAL# mobile Box Batch Area "

to skip the first line, we've used "more +2"

And, later options with grep commands are quite simple...
-r means recursive search...
-i means case insensitive...
Here we can safely avoid both of them...I am so addicted with this option, that fingers automatically types this options...
Newbie
 
Join Date: Nov 2008
Posts: 6
#5: Nov 6 '08

re: how to compare values from 2 different filenames?


wow! great! u r truly an expert! thanks again!
Newbie
 
Join Date: Nov 2008
Posts: 6
#6: Nov 10 '08

re: how to compare values from 2 different filenames?


what if i have only 1 file and the mobtel and activation date were there, but some mobtel has no activatation date (means not activated), and what i want to do is to output in a newfile all the mobtel numbers that were no activation date together with their other records such as seq no., box, area1 and finally sort using uniq. pls help thanks
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#7: Nov 10 '08

re: how to compare values from 2 different filenames?


Quote:

Originally Posted by companion98

what if i have only 1 file and the mobtel and activation date were there, but some mobtel has no activatation date (means not activated), and what i want to do is to output in a newfile all the mobtel numbers that were no activation date together with their other records such as seq no., box, area1 and finally sort using uniq. pls help thanks

Could you please post the exact content of the file....
Newbie
 
Join Date: Nov 2008
Posts: 6
#8: Nov 10 '08

re: how to compare values from 2 different filenames?


this is my code:

#!/bin/sh
`more +2 f1.txt | grep "#N/A" f1.txt < sort output.txt | uniq >> output.txt`


======

these are my files and its contents:

f1.txt

code_____tel#________ place ____ activationdate
32_______ 123456______ jap ______ 20050913
33_______ 789012______ phil ______ #N/A
34_______ 987654______ thai ______ 19990112
35_______ 765433 ______ us ______
36_______ 345354 ______ la ______ #N/A
37_______ 987977 ______ us ______ 19990203

output.txt
code_____tel#________ place ____ activationdate
33_______ 789012______ phil ______ #N/A
35_______ 765433 ______ us ______
36_______ 345354 ______ la ______ #N/A
================================================== ====
PROBLEM:

i have a f1.txt file and i want to do is search all the mobile numbers that were not activated yet (do not have activation date samples are #N/A or simply a blank entry in activationdate field). now i already done "#N/A" this using grep (see my code) but obviously im not satistied finding "#N/A" only, what if the cell is empty?

second, how do i get the fieldnames of the ff so that when i look at my output.txt it automatically have these field names:

code_____tel#________ place ____ activationdate

lastly, why does f1.txt appears in every line on my output?
example :output.txt

f1.txt:33_______ 789012______ phil ______ #N/A
f1.txt:35_______ 765433 ______ us ______
f1.txt:36_______ 345354 ______ la ______ #N/A
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#9: Nov 10 '08

re: how to compare values from 2 different filenames?


OK....
Try this command...

more +2 f1.txt | awk '{if( $4 == "" || $4 == "#N/A") print $1"\t"$2"\t"$3"\t"$4 }'

Here, I'm assuming that field separator in f1.txt is TAB...

I just don't understand your second problem

"second, how do i get the fieldnames of the ff so that when i look at my output.txt it automatically have these field names: "
Newbie
 
Join Date: Nov 2008
Posts: 6
#10: Nov 11 '08

re: how to compare values from 2 different filenames?


i want to see these column names automatically as the first row of the output.txt

code_____tel#________ place ____ activationdate

example:
code_____tel#________ place ____ activationdate----> this what i want to see in the first row
34_____9788778______mla____20080303
23_____4535354______jap____20040907

again, thank u very much for your help. :-)
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#11: Nov 11 '08

re: how to compare values from 2 different filenames?


Quote:

Originally Posted by companion98

i want to see these column names automatically as the first row of the output.txt

code_____tel#________ place ____ activationdate

example:
code_____tel#________ place ____ activationdate----> this what i want to see in the first row
34_____9788778______mla____20080303
23_____4535354______jap____20040907

again, thank u very much for your help. :-)

First you separate first line from f1.txt and redirect to output.txt and then run the actual command....

cat f1.txt | head -n 1 > output.txt
more -2 f1.txt | awk '{if( $4 == "" || $4 == "#N/A") print $1"\t"$2"\t"$3"\t"$4 }'

Cheers,
Ash
Reply