LinuxQuestions.org
Help answer threads with 0 replies.
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 08-23-2011, 09:06 AM   #1
imkornhulio
Member
 
Registered: Aug 2008
Posts: 58

Rep: Reputation: 15
filter a CSV file


My CSV file looks like this

Temp,0,Finance,456987
Perm,50,Accounting,895465
Perm,0,MIS,12354
Perm,0,HR,12589

I would like to filter the file and output where the second column is 0

Temp,0,Finance,456987
Perm,0,MIS,12354
Perm,0,HR,12589

I tried using awk with failure

Thank you

Last edited by imkornhulio; 08-23-2011 at 09:15 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-23-2011, 10:13 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I think this should work:

Code:
awk 'BEGIN { FS = "," }; {if ($2 == 0) print}' inputfile.txt
 
2 members found this post helpful.
Old 08-23-2011, 01:23 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
The previous method is totally legit, but here's a slightly different way to do the same thing:
Code:
$ awk -F"," '{ if ($2 == 0) print }' this.csv
 
1 members found this post helpful.
Old 08-23-2011, 02:17 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or even shorter:
Code:
awk -F, '$2==0' file
 
1 members found this post helpful.
Old 08-24-2011, 07:04 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
ruby -F"," -ane  'print if $F[1]=="0"' file
 
  


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
C++ and CSV file hung.rogerchan Programming 1 11-15-2009 03:04 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
CSV file merge help lmedland Programming 2 06-04-2008 11:11 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 10:50 PM.

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