[NTLUG:Discuss] scripting foo help

Fred James fredjame at fredjame.cnc.net
Thu Jul 2 12:30:24 CDT 2009


Dan Wright - SPCA of Texas wrote:
>  
>
>
>   
>>> I have a CSV file that needs " added to the ID fields like bellow, 
>>> right now there missing the " ( ex: 1263831 instead of:  
>>>       
>> "1263831" and 
>>     
>>> ID instead of: "ID"  )
>>>  
>>>       
>> Here is a Perl solution.  It will read from data.csv and 
>> print the result to stdout.  You can add '-i' if you want it 
>> to modify the file inplace.
>>
>> perl -n -a -F"," -e '$F[0]=qq{"$F[0]"};print join(q{,}, @F);' data.csv
>>
>> If you have any newlines within your data fields, this will break. 
>> You'd need a full csv parser to handle that.
>>     
>
> your foo is strong :) 
>
> but yes i would need to modify the file inplace so
> where would i put the -i 
>
> thanks for your help.
>
> Dan
>   
Dan
Caution is advised on area lakes -- any wind that blows away the 
original file before the results can be verified will blow away any 
chance of recovery - your millage may vary
Regards
Fred James

PS:  If it were me I think I might opt for AWK or SED, or at least EX - 
sorry, personal opinion there - the thing is (from personal experience) 
these types of files always seem to have some sort of gotcha that begs 
for some sort of decision tree - best of luck




More information about the Discuss mailing list