LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-04-2012, 11:32 PM   #1
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Rep: Reputation: Disabled
Find the null value in the frist column of a csv file


I have csv file which has 5 columns.I need to find out the null value in the first column.Kindly help with grep or awk command.

sample file
 
Old 10-04-2012, 11:40 PM   #2
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
what do you mean by the null value? if you just mean the first column, then

Code:
awk -F, '{print $1}' file.csv
 
Old 10-04-2012, 11:50 PM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
null values don't exist in text files Are you referring to empty fields? Or fields that contain the text 'NULL'? Your sample file is missing; it would probably clarify what you're looking for.

Are you using a specific language (C, bash, ...)?
 
Old 10-04-2012, 11:50 PM   #4
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
SMLP,866142102,NYS,-1.000000,SUMMIT MIDSTREAM PARTNERS LP U
,866142201,NYS,-1.000000,SUMMIT MIDSTREAM PARTNERS LP SB UNIT LT PRT[/B][/B][/B]
LLYDF,G5533WAJ6,OBB,-1.000000,LLOYDS BANKING GROUP PLC 7 7/8%NON PF
GOSSF,380884106,OBB,-1.000000,GOLDEN CROSS RESOURCES INC COM
QLYS,74758T303,NMS,-1.000000,QUALYS INC COM

Sample file above. Please note in the row, first column value is missing.I need to find such type of null values
 
Old 10-04-2012, 11:59 PM   #5
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by Vthimmap View Post
SMLP,866142102,NYS,-1.000000,SUMMIT MIDSTREAM PARTNERS LP U
,866142201,NYS,-1.000000,SUMMIT MIDSTREAM PARTNERS LP SB UNIT LT PRT[/B][/B][/B]
LLYDF,G5533WAJ6,OBB,-1.000000,LLOYDS BANKING GROUP PLC 7 7/8%NON PF
GOSSF,380884106,OBB,-1.000000,GOLDEN CROSS RESOURCES INC COM
QLYS,74758T303,NMS,-1.000000,QUALYS INC COM

Sample file above. Please note in the row, first column value is missing.I need to find such type of null values
you could look for lines starting w/a comma:
Code:
grep ^, file
 
Old 10-05-2012, 12:08 AM   #6
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
Thanks a lot. It worked.

small doubt, does this command works on csv file or on all files
 
Old 10-05-2012, 12:12 AM   #7
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by Vthimmap View Post
small doubt, does this command works on csv file or on all files
well, as csv files are supposedly Comma Separated Value format, then yes. but maybe a line starts with a space, then a comma, e.g.:

Code:
  ,foo,bar,etc
so you'd have to adjust it, e.g.:

Code:
grep ^[[:space:]]*, file

Last edited by nugat; 10-05-2012 at 12:13 AM. Reason: made space more obvious
 
Old 10-05-2012, 12:12 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Quote:
you could look for lines starting w/a comma:
Like he said

(text not binary though I'd say)
 
Old 10-07-2012, 01:03 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

grep is designed for testing lines for patterns. So if all we want is to check whether a line begins with a comma or something, it's a good choice.

But for more complex work on field-delimited text, awk is usually better. To test whether a given field contains (or doesn't contain) a certain value, just use expressions like this:

Code:
awk -F ',' '$1 == "" { print }'
awk -F ',' '$1 ~ /^$/'
awk -F ',' '! $1 ~ /./'
awk -F ',' '! $1'	#see below for this one
The basic pattern of awk expressions is:
Code:
tests { actions }
So in the first example, if field one matches the exact value of "" (i.e. nothing), then awk will print it.

( And since print is the default action on affirmative tests, it's unnecessary to include it here. I've left it off of the remaining examples ).

In the second, the "~" tests the string against a regular expression, and the regex "^$" means "string start+string end", with nothing between them. So it will only print on empty fields.

In the third one, the "!" means "not", and so reverses the test, and "." is the regex for "any single character". So again, if the field is empty (doesn't have any characters) it will print.

Finally, instead of a comparison test, we might also be able to use a simple Boolean test. If field one is empty or set to "0" (the integer zero), it will print. (Zero and null values evaluate as false in a Boolean tests. Any other value evaluates as true. And of course "!" reverses the outcome.)


Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to make a column in a CSV file lowercase from terminal? ingram87 Linux - Software 3 08-08-2012 01:09 AM
[SOLVED] validating mandatory column value in csv file using script sukhdip Linux - Newbie 2 01-31-2012 02:45 AM
How to read CSV data and compare the column values and then write them in new file VijayaRaghavanLakshman Linux - Newbie 9 01-26-2012 09:02 PM
How to redirect the output to a different column in .csv file Sayan Acharjee Linux - General 2 04-25-2011 11:43 AM
Rearrange the column in CSV file.. govi1234 Linux - Newbie 9 08-27-2010 07:15 PM


All times are GMT -5. The time now is 10:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration