linux - How to replace strings on a static position and do not delete the blanks with sed -
i try create script template. in this, want replace strings on static position. after first sed try, default string corrupted.
my script:
#!/bin/sh # default record record="nnnnnnnnnnnnnnnnnnnn cccccccccccccccccccc bbbbbbbbbb 001" echo "$record" # read values configuration file var1="user name: sibob" # 20 var2=" berlin" # 20 var3="1983 [us]" # 10 record=`echo $record | sed -e "s/\(.\{0\}\)\(.\{20\}\)/\1$var1/" ` echo "$record" record=`echo $record | sed -e "s/\(.\{21\}\)\(.\{20\}\)/\1$var2/" ` echo "$record" record=`echo $record | sed -e "s/\(.\{54\}\)\(.\{10\}\)/\1$var3/" ` echo "$record" the result of script is:
$ ./rename.sh nnnnnnnnnnnnnnnnnnnn cccccccccccccccccccc bbbbbbbbbb 001 user name: sibob cccccccccccccccccccc bbbbbbbbbb 001 user name: sibob cccc berlinbbbbbbb 001 user name: sibob cccc berlinbbbbbbb 001 user name: sibob cccc berlinbbbbbbb 001 but want is:
user name: sibob berlin 1983 [us] 001 the problem is, blanks removed. in case replacement of strings doesn't work correctly.
have idea?
sounds should use printf instead.
printf '%20s %-20s %10s %03i\n' "$var1" "$var2" "$var3" "1" if insist on using sed, can still use printf produce necessary padding, though perhaps refactoring code awk script best route forward.
the immediate problem fail quote argument echo, causes shell perform whitespace tokenization , wildcard expansion on value, squashing whitespace in value.
Comments
Post a Comment