0

I have a script which takes a .csv file as a an input and runs row by row

kma210,projects.kma210
kma215,projects.kma215_2
KMA3xx,projects.kma3xx
KMI7,projects.kmi7

The above one is a sample for the .csv file. I have two types of process running for each row (example : abc_process and xyz_process).

Now my requirement is once the script runs one row , the output i.e., success/fail should print in the same file in third column (abc_process) and fourth column (xyz_process).

Example please see here:

kma210,projects.kma210,success,success
kma215,projects.kma215_2,fail,success
KMA3xx,projects.kma3xx,success,fail
KMI7,projects.kmi7,fail,fail

Could anyone please suggest?

7
  • How do you receive the status? success/fail? Commented Dec 6, 2016 at 16:23
  • You'll have to check for simultaneous file access Commented Dec 6, 2016 at 16:28
  • @Inian i gave echo for the output to see if it's success or failed. echo "abc_process success" echo "xyz_process success" Is there a possibility to parse the echo output and add in third, fourth columns ? Commented Dec 6, 2016 at 16:33
  • @SubratSahoo: When do you say, process a row? how are you doing it, using awk and running any system commands within or how else? Commented Dec 6, 2016 at 16:36
  • @Inian cat "$1" | while IFS='' read -r line; do D="$(echo "$line" | cut -d ',' -f 1 | tr -d '"')" P="$(echo "$line" | cut -d '.' -f 2)" echo 'D: '"$D" echo 'P: '"$P" Commented Dec 6, 2016 at 16:37

1 Answer 1

1

I think what you are trying to achieve is something of this sort.

From your input file (input.csv), use the script as follows:-

#!/bin/bash

while IFS=',' read -r col1 col2
do
    # The below two read commands are to process the success/fail status
    # string as you indicated. If you are receiving a string of the below 
    # form, from an external bash command; you need to use process-substitution 
    # as

    # IFS= read -r _ p1Status <<(p1Command)
    # IFS= read -r _ p1Status <<(p2Command)

    IFS= read -r _ p1Status <<<"abc success"
    IFS= read -r _ p2Status <<<"xyz fail"

    # The below printf just prints the output as you need to stdout
    # to create a .csv out of it, append it to a new file as

    # printf "%s,%s,%s,%s\n" "$col1" "$col2" "$p1Status" "$p2Status" >> output.csv

    printf "%s,%s,%s,%s\n" "$col1" "$col2" "$p1Status" "$p2Status"
done<input.csv

The above command as such writing to stdout, produces an output as

kma210,projects.kma210,success,fail
kma215,projects.kma215_2,success,fail
KMA3xx,projects.kma3xx,success,fail
KMI7,projects.kmi7,success,fail

which you can append to a file using the >> operator with the line I commented out. Or if you want a single file and replace the original file. Add a line

mv -v output.csv input.csv 

as the last line of the script.

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

2 Comments

i am a rookie in scripting, i will give my script here. could you please incorporate the same into it ?
SO is not a free coding service, you have to make an attempt to improvise from the solution I have already provided. You can post your requirement, let me see how can I help you.

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.