Ali E
Ali E

Reputation: 87

Replacing value inside while loop with using sed and IFS

Im using a while loop to split the line into variables and use those same variable inside the loop to pass into sed to replace in the line. Is there a way to achieve printing the whole line without having to print out all the while variables into a new file?

while IFS=';' read -r task_id date name action; do
    sed -e "s/\$date/$date/; s/\$name/$name/" >> $TASKSFILE.tmp
done < $FILE

Input file would look like:

1;20190423;Name1;print $date $name
2;20190424;Name2;print $date $name

Expected output:

1;20190423;Name1;print 20190423 Name1
2;20190424;Name2;print 20190424 Name2

Upvotes: 1

Views: 927

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

Why don't you use awk for this? It's available on all POSIX-compliant platforms.

awk -F';' '{sub(/\$date/,$2);sub(/\$name/,$3)} 1' "$FILE" > "${TASKSFILE}.tmp"
$ awk -F';' '{sub(/\$date/,$2);sub(/\$name/,$3)} 1' file
1;20190423;Name1;print 20190423 Name1
2;20190424;Name2;print 20190424 Name2

Upvotes: 2

Related Questions