1

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

2
  • what is column DY? Is it the column name (header)? What does "DY=3" mean? Commented Jun 12, 2017 at 20:59
  • Sorry thats the headername. Thanks Commented Jun 12, 2017 at 21:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.