Reputation: 3162
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!
Upvotes: 0
Views: 179
Reputation: 246774
I think @anishsane probably has the right answer. I just want to offer some notes about your code. If you're using bash, take advantage of some of bash's features:
1) just assign a string instead of spawning a subshell to echo the string:
c1="start time;project_start_time"
(unless the echo
is just a placeholder here for some more complicated process
2) use an array instead of numerically-indexed variables
c=(
"start time;project_start_time"
"start_time;project_start_time"
"end time;project_end_time"
"end_time;project_end_time"
"total time;project_total_time"
"total_time;project_total_time"
"project_id;project_ID"
"status;project_status"
"client_id;client_ID"
"employee_id;employee_ID"
"employee_name;employee_name"
"date created;date_created"
"date_created;date_created"
)
3) split the strings with read and IFS and avoid eval nastiness:
for (( countc=1; countc <= ${#c[@]}; countc++ )); do
IFS=';' read nc_string real_string <<< "${c[countc]}"
# ...
done
Upvotes: 3
Reputation: 20980
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:
cp phasef/"$count"p.csv phasef/"$count".csv
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 -i -e "s/$nc_string/$real_string/gI" phasef/"$count".csv
countc=$(( $countc + 1 ))
done
Note the -i
in sed line.
Upvotes: 2
Reputation: 74018
You have a typo in the sed line. Change
sed -e "s/$nc_string/$real_string/gI" phasef/"$count"p.csv
to
sed -e "s/$nc_string/$real_string/gI" phasef/"$countc"p.csv
Upvotes: 0