473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to compare values from 2 different filenames?

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]
Nov 4 '08 #1
10 2707
ashitpro
542 Expert 512MB
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
Nov 4 '08 #2
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.
Nov 6 '08 #3
ashitpro
542 Expert 512MB
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...
Nov 6 '08 #4
wow! great! u r truly an expert! thanks again!
Nov 6 '08 #5
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
Nov 10 '08 #6
ashitpro
542 Expert 512MB
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....
Nov 10 '08 #7
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
Nov 10 '08 #8
ashitpro
542 Expert 512MB
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: "
Nov 10 '08 #9
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. :-)
Nov 11 '08 #10
ashitpro
542 Expert 512MB
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
Nov 11 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Neil | last post by:
Folks, I've got a problem at work, and I'm hoping that someone out there may have had something similar (although I doubt it!) or may be able to offer some advice. I'm in the process of...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
3
by: Simple Simon | last post by:
Hi, I'm filling DataTable1 with the filenames from within a directory, and filling another DataTable2 with filenames from a SQL Db table. I'd like to compare the two, and delete the files from...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
2
by: Nitro | last post by:
Does anyone know a simple way to compare two images to see if they are the same? The filenames of the images will be different, but if the image data is the same I would like to test for that. ...
29
by: Amer Neely | last post by:
I've got a dynamically built form with checkboxes for each element ( a list of file names in a directory). I need to grab only those checkboxes that are checked, so I can then delete those files. ...
6
by: yinglcs | last post by:
Hi, i have 2 files which are different (1 line difference): $ diff groupresult20070226190027.xml groupresult20070226190027-2.xml 5c5 < x:22 y:516 w:740 h:120 area: --- But when I use the...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.