LinuxQuestions.org
Review your favorite Linux distribution.
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 03-30-2009, 05:35 PM   #1
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

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


Hi,

Suppose I have a file.
Code:
$ 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
Code:
#!/bin/bash
gawk 'BEGIN { FS=OFS=","}
{if  ($3 == "          ") print $1,$2,FS,$4 ; else print $1,$2,$3,$4}'
Thanks

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

Original Poster
Rep: Reputation: 0
Smile

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


Reply

Tags
awk, blank, csv, gawk, null



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

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

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