I have the following string, and want to introduce additional spaces between the two %s. This will be done by specifying the gap between the %s. Example having gap=8 will put 8 spaces between the two %s.
frmt_k1d1_test="%s %s\n"
I am doing the script in bash.
---------- Post updated at 06:12 AM ---------- Previous update was at 05:42 AM ----------
I have tried this which works. Something simpler would be useful.
echo "$frmt_k1d1_test" | awk -v g="$gap" 'BEGIN {FS=OFS=""} {for (i=1;i<=NF;i++) {if (i==3) $i=$i g}}1'
Try this
aLongSpace=" "
gap=8
frmt_k1d1_test="%s${aLongSpace:0:$gap}%s\n"
echo "$frmt_k1d1_test"
Have got another method, however I need to put $gap in the sed statement. How can I pass the gap?
echo "$frmt_k1d1_test" | sed 's/.../&$gap/g'
---------- Post updated at 06:17 AM ---------- Previous update was at 06:14 AM ----------
This works, however I originally have the string
frmt_k1d1_test="%s %s\n"
Modification should be done using this string as initial input.
kristinu:
Have got another method, however I need to put $gap in the sed statement. How can I pass the gap?
echo "$frmt_k1d1_test" | sed 's/.../&$gap/g'
---------- Post updated at 06:17 AM ---------- Previous update was at 06:14 AM ----------
This works, however I originally have the string
frmt_k1d1_test="%s %s\n"
Modification should be done using this string as initial input.
Just use ""
instead of ''
The sed is a good way. However things are not done yet
echo "$frmt_k1d1_test" | sed "s/.../&$gap/g"
which produces
%s %s\ n
---------- Post updated at 06:51 AM ---------- Previous update was at 06:30 AM ----------
I think the best way is to search for 4 spaces, then replace with a string of size gap.