0

How do I edit line of a file in while loop, Code works except for what is inside the "if" statement, anyone done this before? (second question today, thank you)

#!/bin/bash
export ELMS_STORAGE="/loc1/f1.csv" 
export CUSTOMER_LIST="/loc2/f2.csv"
while read -r ELMS_SIZE ELMS_NAME ELMS_ENV do while IFS=, read -r
CUSTOMER_ENV CUSTOMER_URI CUSTOMER_NAME CUSTOMER_SCHEMA_OWNER
CUSTOMER_DB_INSTANCE OTHER do


if [ "${ELMS_NAME}" = "${CUSTOMER_URI}" ]; then
#### add a new item to the f2.csv 
${ELMS_STORAGE}.${ELMS_ENV}="${ELMS_ENV} ${CUSTOMER_URI}"   ### doesn't work 
fi


done < <(tac $CUSTOMER_LIST) done < <(tac $ELMS_STORAGE)

exit

f2.csv before edit

monkey
dog
tiger

f2.csv after edit

monkey brown 
bird green 
cat yellow
aymanzone
  • 135
  • 13
  • This code looks like a bunch of syntax errors. Also, bash doesn't have a do-while loop. – melpomene Sep 19 '17 at 19:40
  • 2
    Dump that thing in https://www.shellcheck.net/ and fix it up. – JNevill Sep 19 '17 at 19:41
  • 1
    You can't write to a file at the same time as you're reading it, you need to write to a new file. Then rename it when the loop is done. – Barmar Sep 19 '17 at 19:44
  • I suggest you do this with `awk`, not `bash`, so you don't have to read `CUSTOMER_LIST` for each line in `ELMS_STORAGE`. Read the customer list into an array, then use the array when processing elms storage. – Barmar Sep 19 '17 at 19:45
  • @Barmar, I take very slight issue with the implication that it's *impossible* to implement this properly in bash. One could certainly go the read-into-an-array approach there too. – Charles Duffy Sep 19 '17 at 19:48

1 Answers1

0

Try paste which is probably what you want

$ paste f1.csv f2.csv 
monkey  brown
dog green
tiger   yellow
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134