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 > Operating Systems > Linux
.
google unix.com



Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 07:17 PM
how to replace field for each record happyv Shell Programming and Scripting 12 06-26-2007 07:56 AM
Replace password field using ed/sed munch UNIX for Dummies Questions & Answers 6 04-30-2007 11:01 PM
find pattern and replace another field sergiioo Shell Programming and Scripting 3 04-11-2007 12:19 AM
Replace 10th Field by 2 PradeepRed Shell Programming and Scripting 2 01-19-2006 03:46 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 01-19-2008
pcboss pcboss is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Replace field in csv

Hi, I need to replace a field (field 5) in a csv file, based on the content of another field (field 2), something like this:

actual file:

field1, field2, filed3, field4, field5, field6
01,232,abb-pan,679,,pan
02,565,cdf-pan,683,,pan


the result should be:

01,232,abb-pan,679,PASSED,pan

****************

so, if field2 value is 232, then write PASSED in field 5

Im familiar with awk, i tried this:

awk -F, '{
if ($2 == "232") {
pass_ref="PASSED"
**** I'm stuck here, how I replace the field5... in the same file...???***

thanks in advanced....
  #2 (permalink)  
Old 01-19-2008
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 425
how about this?
Code:
 awk -F, 'BEGIN{OFS=","}{if($2 == 232){$5="PASSED";print}}'
  #3 (permalink)  
Old 01-19-2008
pcboss pcboss is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Thanks for your fast response, but... where I specify the name of the file to be modified...?

I tried:

awk -F, 'BEGIN{OFS=","}{if($2 == 232){$5="PASSED";print}}' alumni*.csv > alumni*.csv

but the files got deleted....
  #4 (permalink)  
Old 01-19-2008
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 425
you don't want to redirect to the same file that your reading! as you found out this will truncate the data file. Redirect to a new filename.


ie:

Code:
awk -F, 'BEGIN{OFS=","}{if($2 == 232){$5="PASSED";print}}' alumni*.csv > newfile.csv
  #5 (permalink)  
Old 01-19-2008
pcboss pcboss is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Thanks

I can achieve what I need by redirecting to another file, and then renameming those files...

Thanks a lot....
Sponsored Links
Closed Thread

Bookmarks

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




All times are GMT -4. The time now is 05: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