The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read and compare multiple fields in a column at the same time ahjiefreak Shell Programming and Scripting 1 06-19-2008 11:08 AM
Replace 3 fields with null in the file vukkusila Shell Programming and Scripting 1 09-12-2007 01:10 AM
Combine a datafile with Master datafile, emergent! onthetopo Shell Programming and Scripting 6 05-12-2007 06:36 AM
search and replace different fields tungaw2004 UNIX for Dummies Questions & Answers 3 03-29-2007 03:16 AM
replace one section in a datafile Paprika Shell Programming and Scripting 4 06-17-2005 08:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-20-2008
MrCarter MrCarter is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
How do I read/find/replace fields in a csv datafile?

hello.

I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this:

I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only [instead of YYYYMONDD date field expected] then replace it with a null/blank character.

I had a go at:

cat <datafile> | awk ‘$5 == “0” { “ “ } END { print }';
cat <datafile> | awk ‘$8 == “0” { “ “ } END { print }'; ..etc..

..alas it didn't work..

all advice/comments very much appreciated

Steven.
  #2 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Post a sample of the csv datafile and the desired output.

Regards
  #3 (permalink)  
Old 06-20-2008
MrCarter MrCarter is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
thanks for the quick reply..

the csv's first two lines look like:

Code:
13426,Williams,Robyn,Robyn,20031106,0,0,20080421,0,NASP39,Casual Sport & Recreation Ass,43.24,FACI,E,FASP,SUPP,SP,NASP35,20080421,0,100,FACI,V,FASP,SUPP,SP,0,0,Ms,FT,FT,SUB
15721,Clinton,Hilary,Hilary,20041101,hs0jo,poppy@aol.com,20080601,0,HSL236,Project Manager,56.76,700,A,700,ACAD,PB,V32636,20071207,20080731,0.36,700,V,HSEN,VLEC,FL,0,0,Dr,PT,FT,SUB
..and the desired output is valid entry into our ORACLE database via sqlldr.. (apologies if thats not what you meant..)

thanks again..

Steven

Last edited by Yogesh Sawant; 06-20-2008 at 04:54 AM.. Reason: added code tags
  #4 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk 'BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i=="0"){$i=""}}}1' file
Regards
  #5 (permalink)  
Old 06-20-2008
MrCarter MrCarter is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
..an error is being returned when I try to run this either by itself or in a script - am I not running it right?

$ cat SLsTester_DataFile.csv | awk BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i=="0"){$i=""}}}1' file;

..the error given reads:

-bash: syntax error near unexpected token `('

your assistance is very much appreciated..

regards, Steven
  #6 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Don't pipe the result of the cat command to the file. This is sufficient:

Code:
awk 'BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i=="0"){$i=""}}}1' SLsTester_DataFile.csv
You can redirect the output to a file as follow:

Code:
awk 'BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i=="0"){$i=""}}}1' SLsTester_DataFile.csv > NewFile
Regards
  #7 (permalink)  
Old 06-20-2008
MrCarter MrCarter is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
..its stilll not running Im afraid..

$ awk 'BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i=="0"){$i=""}}}1' SLsTester_DataFile.csv > SLsOUtputFile.csv;
awk: syntax error near line 1
awk: bailing out near line 1
/applmgr@fintest:~/finliveappl/su/11.5.0/payroll/gl $

..I've also tried running it within a script but it errors the same...

Thing is, as I have several date fields in each line ($5 $8 $9 $19 $20 $27) that may or may not need changing (exactly the same however, from zero to null), will this script simply swap ALL fields it finds with a zero only populated or will I need to specify field numbers?

Apologies for the questions but this is good for me to learn..

ThanksInAdvance...

Steven
Sponsored Links
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0