rookievmc
rookievmc

Reputation: 183

Bash Script and Edit CSV

Im trying to create a bash file to do the following task

1- I have a file name called "ORDER.CSV" need to make a copy of this file and append the date/time to file name - This I was able to get done

2- Need to edit a particular field in the new csv file I created above. Column DY and row 2. This have not been able to do. I need to insert date bash script is run in this row. Needs to be in this format DDMMYY

3- then have system upload to SFTP. This I believe I got it figured out as shown below.

#!/usr/bin/env bash

Im able to get this step done with below command

# Copies order.csv and appends file name date/time


#cp /mypath/SFTP/order.csv /mypath/SFTP/orders.`date +"%Y%m%d%H%M%S"`.csv

Need help to echo new file name

echo "new file name "

Need help to edit field under Colum DY Row 2. Need to insert current date inthis format MMDDYYYY

awk -v r=2 -v DY=3 -v val=1001 -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; 
print}' 

This should connect to SFTP, which it does with out issues.

sshpass -p MyPassword sftp -o "Port 232323" 
[email protected]

Need to pass new file that was created and put into SFTP server.

put /incoming/neworder/NEWFILEName.csv

Thanks

Upvotes: 1

Views: 873

Answers (1)

karakfa
karakfa

Reputation: 67467

I guess that's what you want to do...

echo -e "h1,h2,h3,h4\n1,2,3,4" | 
awk -v r=2 -v c=3 -v v=$(date +"%d%m%y") 'BEGIN{FS=OFS=","} NR==r{$c=v}1'

h1,h2,h3,h4
1,2,120617,4

to find the column index from the column name (not tested)

... | awk -v r=2 -v h="DY" -v v=$(date +"%d%m%y") '
         BEGIN {FS=OFS=","}
         NR==1 {c=$(NF+1); for(i=1;i<=NF;i++) if($i==h) {c=i; break}}
         NR==r {$c=v}1'

the risk of doing this is the column name may not match, in that case this will add the value as a new column.

Upvotes: 1

Related Questions