Replace a particular field in all records in a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a particular field in all records in a csv file
# 1  
Old 10-14-2010
Error 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 10203001|001","6151|886|9024|N|N|N|0033


in this i want to change the 3rd field(2010/08/15-10-00-00.01) if the user enters a particular date it must change to that in all records...

Eg:
if he enters 201209 all records in all files must have the year and month of the 3rd field changed (2012/09/15-10-00-00.01)..can anyone tell me how to do it??


should i use perl or awk or anything.....
# 2  
Old 10-14-2010
Try this,

Code:
#!/usr/bin/perl

use strict;

print "Enter year month:-";
my $YR_MON=<STDIN>;

my $YR=substr($YR_MON,0,4);
my $MON=substr($YR_MON,4);
chomp($YR,$MON);

open (FH,"<","/path/file") || die "Cannot open file\n";

while (<FH>) {
s/(.+?),(.+?),(\d+\/)(\d+\/)/$1,$2,$YR\/$MON\//;
print $_;
}
close(FH);

Invocation
Code:
perl scriptname.pl

# 3  
Old 10-14-2010
This is could help..maybe..

Code:
d=$(date "+%Y%m")|awk -F, -v dt=$d '{gsub($3,dt)}1' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

1. This will insert the records into db table by reading from ta csv file

I have this code with me but the condition is If any of the mandatory columns are null then entire file will be rejected. LOAD DATA infile ' ' #specifies the name of a datafile containing data that you want to load BADFILE ' ' #specifies the name of... (1 Reply)
Discussion started by: raka123
1 Replies

2. UNIX for Beginners Questions & Answers

Filtering records of a csv file based on a value of a column

Hi, I tried filtering the records in a csv file using "awk" command listed below. awk -F"~" '$4 ~ /Active/{print }' inputfile > outputfile The output always has all the entries. The same command worked for different users from one of the forum links. content of file I was... (3 Replies)
Discussion started by: sunilmudikonda
3 Replies

3. Shell Programming and Scripting

Script to check field value from a file records

I need a script to check the records in a file , if any value match transfer the record in error.txt file. 1- If any of the any field value is NULL(nothing in this field) Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Example: 11111111|22222222|NULL|12|444|27042018|27042018... (8 Replies)
Discussion started by: vivekn
8 Replies

4. 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

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

8. Shell Programming and Scripting

Extract file records based on some field conditions

Hello Friends, I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited): ColA|ColB|ColC|ColD|ColE|ColF Now for this file, I have to get those records which fulfil the following condition: If "ColB" is NOT NULL and "ColD" has values one of the following... (9 Replies)
Discussion started by: mehimadri
9 Replies

9. 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

10. Linux

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: ... (4 Replies)
Discussion started by: pcboss
4 Replies
Login or Register to Ask a Question