0

I require some help in reading and updating a csv file using Shell script. I have written the below code to read the csv file and it works fine.

#!/bin/sh
#This script is to read the excel data
IFS=","
echo "Starting to read csv"
i=1
while read f1 f2
do
    test $i -eq 1 && ((i=i+1)) && continue
    echo "tenantID is :$f1"
    echo "status is   :$f2"

#The below command will update the schema with data

java -Xmx512m -XX:MaxPermSize=128m -jar biz.jar -install -readers=seed -          delegator=default#$f1

done < TenantId.csv

echo "end of file "

The above code is working fine. I.e it reads the csv file and passes the tenantID to the java arg. Once the java processing is done, I would like to update the status(f2) with some values. Also in the above code,blank lines are also getting executed.I would like to know how to process only the lines where data is present. Please let me know how to acheive the same.

Thanks in Advance

1 Answer 1

1
while read f1 f2
do
    test $i -eq 1 && ((i=i+1)) && continue
    [ -z "$f1" ] && continue
    echo "tenantID is :$f1"
    echo "status is   :$f2"

    #The below command will update the schema with data

    java -Xmx512m -XX:MaxPermSize=128m -jar biz.jar -install -readers=seed -          delegator=default#$f1

    echo $f1, XXX >> TenantIdOut.csv
done < TenantId.csv

Added 2 lines of code:

  1. If the f1 is null, continue - This is to skip blank lines.
  2. Write the record f1 and new status to a new file , and outside the loop you can rename the input file with this new file.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Guru, Thanks a lot for the information. In the second line, I would like to update the same TenantID.csv file & dont want to write to a different file.Also I want to update only the status(f2) of the row the system is currently processing.
Being inside the while loop, you cannot update the input file.. You can write to a temporary file, and after the while loop , you can rename your temp file with the input file.

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.