LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-30-2009, 07:28 AM   #1
Diubidone
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
CSV file parser


Hi,

I need to parse a big CSV file that has some issues

It contains list of users of a network as it follows:

domain\firstname.secondname,number_of_connections_with_that_user,ip_used_to_connect

Example:

college\john.doe,5,192.168.1.1
college\mario.rossi,1,192.168.1.3
college\john.doe,5,192.168.1.2

and so go on...

So as you can see I have John Doe that connected 5 times so I would need an output like this

college\john.doe,5,192.168.1.2,192.168.1.1,....

Can anyone help me?

Last edited by Diubidone; 10-30-2009 at 07:31 AM.
 
Old 10-30-2009, 10:02 AM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 68

Rep: Reputation: 33
There are probably many ways of doing this.

I would approach it by first sorting the file by the first field. (man sort... remember to set the -t flag to tell it to use a comma as field separator.) This would place all the "John Does" on consecutive lines.

You could then use an awk script to accumulate a list of IP addresses for identical first field. Only print a line when the first field is different from the previous, again making sure to specify "," as field separator.

This might get you started, sorry that I don't have time to try a sample script as it would be quite involved.
 
Old 10-30-2009, 10:51 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP , see here for similar example
 
Old 10-30-2009, 07:38 PM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Further, the OP is also asking us to "print" the "5" ($2 in awk) only once.

To take further Vrajgh's example
Since the file is now sorted, you need not even accumulate it, but printf without "\n". You

The awk now has the following pseudo code.
BEGIN {a=$1}
{
If $1 != a then ( printf("\n") ; a=$1; printf("%s,%s,",$1,$2) }
printf("%s",$3) ;
}
END {printf("\n"); }

OK
 
Old 10-30-2009, 08:03 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put the $1 outside BEGIN block.
 
Old 11-03-2009, 03:49 AM   #6
Diubidone
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
I couldn't make it

I have real base knowledge as for scripting...I tried to build something starting with sort and the awk but didn't made it. I'm trying to develop my scripting skills...but anyone can help me? My boss is asking for this thing every damn morning...
 
Old 11-03-2009, 03:57 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Diubidone View Post
I have real base knowledge as for scripting...I tried to build something starting with sort and the awk but didn't made it. I'm trying to develop my scripting skills...but anyone can help me? My boss is asking for this thing every damn morning...
and? where's your code? its your work remember? not ours.. furthermore , some of use have already shown you ways to do it. now pull up your socks and show us something.
 
Old 11-03-2009, 07:26 AM   #8
Diubidone
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
what am I doing wrong?

awk -F","
BEGIN {a=}$1
{
If $1 != a then ( printf("\n") ; a=$1; printf("%s,%s,",$1,$2) }
printf("%s",$3) ;
}
END {printf("\n"); }
OFS="," prova1.csv

I'm so lost at this I'm not asking for a solution I'm asking for an explanation that guides me to the solution
 
Old 11-03-2009, 07:35 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
don't just follow blindly the pseudocode AnanthaP gives you. Its wrong in syntax. Read the gawk doc (see my sig) to learn how to use awk. Or see here for simpler one.
 
Old 11-03-2009, 08:20 AM   #10
Diubidone
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
PLeaassee

Sometime people don't have the time to learn awk and need to resolve problems quickly. That's what are forums for also. I don't need you to bring me internet documentation, google does an excellent job at that. I needed directions to build this darn thing by tonight...

Let's drop it, you can go back to you're teaching class...I'll solve my problem another way...
 
Old 11-03-2009, 08:25 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Diubidone View Post
Sometime people don't have the time to learn awk and need to resolve problems quickly. That's what are forums for also. I don't need you to bring me internet documentation, google does an excellent job at that. I needed directions to build this darn thing by tonight...

Let's drop it, you can go back to you're teaching class...I'll solve my problem another way...
one sentence says it all. you are just too lazy, even with already given hints.
 
  


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
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 01:09 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
anyone knows a config file parser (Linux/c/c++) Thinking Programming 2 07-28-2005 07:55 AM
CSV File AMMullan Programming 2 11-10-2003 12:49 AM

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

All times are GMT -5. The time now is 05:26 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