I am currently trying to use sed to find and replace some text in a file, and for the terms that are being found/replaced, I am trying to use variables, but I cannot get them to work properly for some reason. The variables consist of c1-c9, which can be seen below:
c1=$( echo "start time;project_start_time" )
c2=$( echo "start_time;project_start_time" )
c3=$( echo "end time;project_end_time" )
c4=$( echo "end_time;project_end_time" )
c5=$( echo "total time;project_total_time" )
c6=$( echo "total_time;project_total_time" )
c7=$( echo "project_id;project_ID" )
c8=$( echo "status;project_status" )
c9=$( echo "client_id;client_ID" )
c10=$( echo "employee_id;employee_ID" )
c11=$( echo "employee_name;employee_name" )
c12=$( echo "date created;date_created" )
c13=$( echo "date_created;date_created" )
and the code that contains the sed part is:
while [ "$countc" -le 13 ]; do
real_string=$( eval echo "\$c$countc" | cut -d ';' -f 2 )
nc_string=$( eval echo "\$c$countc" | cut -d ';' -f 1 )
sed -e "s/$nc_string/$real_string/gI" phasef/"$count"p.csv #> phasef/"$count".csv
countc=$(( $countc + 1 ))
done
(there is more code to the script, but it is irrelevant) When I run the script, if i tell it to output real_string/nc_string as the while loop moves along, the variables are correctly outputted, real_string/nc_string variables are actually being defined correctly, but I am not sure why sed is not reading them correctly. If anyone could point out what I am doing wrong I would really appreciate it as I have been trying to figure this out for a few hours now, thanks!
I checked the echo for “s/$nc_string/$real_string/gI” & it is expanding the variables properly. Please let us know, the input file contents.
I suspect that, highlited part in below line has some issue.
sed -e “s/$nc_string/$real_string/gI” phasef/”$count”p.csv > phasef/”$count”.csv
If you want to replace all pairs in input file (
phasef/${count}p.csv) & save to output file (phasef/$count.csv), use below code:Note the
-iin sed line.