LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2012, 11:21 PM   #1
arbex5
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu
Posts: 17

Rep: Reputation: Disabled
Question Analyzing CSV files


From Tread: http://www.linuxquestions.org/questi...0/#post4509101


I see this is a pretty nice solution.
I do need something very similar to this.

I have two cvs files (geodata files)

One have 3 columns (lets call file_1) and the other 5 columns (lets call file_2)

file_1 is like this:

startIpNum,endIpNum,locId
"16777216","16777471","17"
"16777472","16777727","24328"
"16777728","16778239","49"

file_2 is like this:

geo_id,country,region,city,postalCode
32,BR,,,
19304,BR,SP,São Paulo,
24328,BR,RJ,Rio De Janeiro,
22975,BR,SP,Salto,

What I need is to create a new file (lets call file_3) that match the field locId of the file_1 with the field geo_id of the file_2
In other words, if a field locId (file_1) and a field gep_id (file_2) are equal, generate a file_3 with the row of file_1 that matched the criteria.

Example.

Using the tables above and running the script the resulting file_3 would be

startIpNum,endIpNum,locId
"16777472","16777727","24328"

Which is the locId "24328" (file_1) matched the geo_id "24328" (file_2) and generate the resulting file_3

Is this hard to make?
Thanks.

Last edited by crabboy; 07-05-2012 at 08:41 AM.
 
Old 07-05-2012, 12:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please do not resurrect old questions. If you feel this one is relevant to your solution then add a link and comment back to this one within your own question.
 
Old 07-05-2012, 08:53 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
This sounds much easier than the other problem, sounds like you want to pluck items from file1 which contain a key that is in the second file. This can be done with a simple shell script.

Loop over each line in file1 and grab the third field and grep for that in file2:
Code:
#!/bin/sh

for LocID in `cat file1 | cut -d ',' -f3 | tr -d \"`; do
  grep ^$LocID file2 >> file3
done
This is not bullet proof, but should work.
 
Old 07-06-2012, 09:59 AM   #4
arbex5
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
Smile

Hi tried your code but I didn't get it to work, I don't know why.
But here is the thing.
As a suggestion I opened a new thread about this problem, and Daniel B. Martin answered a solution that worked pretty nice with my files.
Here it is.

cut -d, -f1 $InFile2 \
|sed 's/^/"/g' \
|sed 's/$/"$/g' \
|grep -f - $InFile1 \
> $OutFile

And here is the link.
http://www.linuxquestions.org/questi...21#post4720321

Anyway thank you so much and have a nice day.
 
Old 07-06-2012, 11:13 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please close this thread then
 
Old 07-06-2012, 01:19 PM   #6
arbex5
LQ Newbie
 
Registered: Jul 2012
Location: Brazil
Distribution: Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
Done.
Thank you Grail.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux and CSV files saxophonemaster Linux - General 3 01-30-2011 12:57 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
Help with interpreting / analyzing log files brokenpromises Linux - Server 17 12-28-2007 01:44 PM
Analyzing .WAV files indienick Programming 4 08-16-2006 02:30 PM
help editing .csv files schneidz Programming 7 06-11-2005 04:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration