Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-30-2009, 06:35 PM   #1
LQ Newbie
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Rep: Reputation: 0
Smile [SOLVED] CSV null columns with gawk


Suppose I have a file.
$ cat dat
1,newport  ,          ,5.38
2,sydnenham,     58894,2.12
How can I tell gawk to print column $1,$2 but if column $3 is blank to ignore spaces and output just ,, (with no spaces) else print values $3 $4

I tried something like this but I don't think it's right
gawk 'BEGIN { FS=OFS=","}
{if  ($3 == "          ") print $1,$2,FS,$4 ; else print $1,$2,$3,$4}'

Last edited by pcock; 03-30-2009 at 08:37 PM. Reason: resolved
Old 03-30-2009, 08:35 PM   #2
LQ Newbie
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Original Poster
Rep: Reputation: 0

It was easier to remove leading and trailing space between commas
sed 's;^ *;;;s; *, *;,;g;s; *$;;' a.txt > b.txt
found this here!


awk, blank, csv, gawk, null

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
CSV | GAWK | Record merge problem! lmedland Programming 4 07-30-2008 09:10 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 10:15 PM
JavaScript:: alert(node) shows null, but node != null taylor_venable Programming 1 05-01-2006 10:51 PM
fstab-sync: error: libhal_ctx_init_direct: (null): (null) rpz Linux - Hardware 1 11-01-2005 06:42 AM
FS=? in gawk realos Programming 2 05-28-2003 08:30 PM

All times are GMT -5. The time now is 03:22 PM.

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