Replace field in csv

Thread Tools Search this Thread
Operating Systems Linux Replace field in csv
# 1  
Old 01-19-2008
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

the result should be:



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

Im familiar with awk, i tried this:

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

thanks in advanced....
# 2  
Old 01-19-2008
how about this?
 awk -F, 'BEGIN{OFS=","}{if($2 == 232){$5="PASSED";print}}'

# 3  
Old 01-19-2008
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  
Old 01-19-2008
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.


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

# 5  
Old 01-19-2008

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

Thanks a lot.... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Shell Programming and Scripting

Update field value on a csv file

Hi I have a job status csv file. I want to update the status of the job in the file. Below is the csv file 1,jobname1,in_progress,starttime,somthing,somthing 2,jobname2,completed,starttime,somthing,somthing 3,jobname3,failed,starttime,somthing,somthing... (8 Replies)
Discussion started by: midhun19
8 Replies

5. Shell Programming and Scripting

Extracting field values from .csv

How can I select the bold fields from the following? "CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS" "tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON... (4 Replies)
Discussion started by: leghorn
4 Replies

6. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="" wel hope this works i want to replace only with different ip say i tried this sed -i "s/'ipaddress'/'ipaddress='/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. UNIX for Dummies Questions & Answers

Extract a certain field from a CSV?

EDIT: This problem has been solved thanks to the help of scottn. Okay, so I have a CSV. Let's say it has the following entries in it: Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M How would I, for example, extract the gender out of a certain person, maybe based on the year of... (12 Replies)
Discussion started by: chickeneaterguy
12 Replies

9. Shell Programming and Scripting

Replace a particular field in all records in a csv file

hi, i have various csv files, the file format is as follows Entry: "1",4,2010/08/15-10-00-00.01,,"E",,,,,,,,,120,0,"M4_","C","KEW-011-5337140-20100916163456-540097","1234567890","N N 0 ",,,"NUK 800100200",,,"NN",,,,,,,,,,,,"0000000001|0001|20150401... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

10. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question