Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400

01,000300032,193631306,190619,0640,1,80,,2/
02,193631306,000300032,1,190618,0640,CAD,2/

I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might change values from file to file.

what makes row 2 different from row 1?
Please be more specific with your requirements....

This is a .csv file that is populated daily. Each day it is a new file.

The bolded numbers change each day with each new iteration of the file. One day it'll be 0640, the next day it might be 0641. But instead of going in manually through vi daily to change this one value, I would like to put it into a command in cron.

sed 's/,0640,/,2400,/' filename

changes the line#1 and line#2 in your example.
Again: if you want to change only line#2 then please tell us how to distinguish line#2 from line#1.

To distinguish line 2, it will always start with - 02,193631306,000300032,1,
The issue with that command is that 0640 will change tomorrow to 0641 (or some other number). I know I can just go in and manually run that command every day, after looking at the file and changing the appropriate values. But i would like to just have something that either counts to the 33rd character in line 2, deletes the next 4 characters, and replaces them with 2400, or something to that affect.

Sorry for not being clear in the first few posts. All your help is much appreciated.

Ok that's something, just put this as an address:

sed '/^02,193631306,000300032,1,/ s/,0640,/,2400,/' filename

If that looks good, and if you have GNU sed, change all your files with

sed -i.bak '/^02,193631306,000300032,1,/ s/,0640,/,2400,/' filename1 filename2 ...

The original files are saved as filename1.bak filename2.bak ... .
Of course you can use wildcards like *.txt .

I think juggernautjoee is looking for something more like:

awk '/^02,193631306,000300032,1,/{$0 = substr($0,1,32) "2400" substr($0,37)}1' file

or if the line to be changed is always line #2:

awk 'NR==2{$0 = substr($0,1,32) "2400" substr($0,37)}1' file

but the specification of the problem is not clear. If the desire is to change the 6th field instead of to change the 33rd through the 36th characters on the line, then use:

awk -F, '/^02,193631306,000300032,1,/{$6 = 2400}1' OFS=, file

or:

awk -F, 'NR==2{$6=2400}1' OFS=, file

With the sample input provided, all of the above produce the output:

01,000300032,193631306,190619,0640,1,80,,2/
02,193631306,000300032,1,190618,2400,CAD,2/
1 Like

Thanks for the replies guys!

Don, I'll try a few of those and get back to you. The end goal is a cron job that runs daily that will edit this file to replace that 2nd row 6th field with 2400 no matter what the number was before.

EDIT:
I had to compile a newer version of awk to get the inplace command to work, but I think I've got it.

/usr/local/bin/awk -i inplace -F, '/^02,193631306,000300032,1,/{$6 = 2400}1' OFS=, /testdir/BALTRN3.*

This will edit that line on all files in the directory. So I can run this every day to update any new files that get dumped in here by my lftp process.

I also needed to edit that 3rd line, 2nd field to something else, so I just tweaked the above command to fit my liking:

/usr/local/bin/awk -i inplace -F, '/^03,08082000001018001/{$2 = 1018001}1' OFS=, /testdir/BALTRN3.*

Guys, I can't thank you enough for pointing me in the right direction!

If what you're saying is that you want to make both of the above changes to that file, please make both changes in one invocation of awk instead of invoking awk twice. You will get your results faster and use fewer system resources getting the job done:

/usr/local/bin/awk -i inplace -F, '
/^02,193631306,000300032,1,/{$6 = 2400}
/^03,08082000001018001/{$2 = 1018001}
1' OFS=, /testdir/BALTRN3.*
1 Like