0

I have below printf statement in shell script

printf "%-60s   read-successful %-30s$value1 \n" "${value2}" >> output1

output expected:

value2      read-successful  value1

I am getting correct output when value2 is not IP address but when its IP output is not as expected.

when i tried with set -x.It shows me below

printf '%-60s   read-successful %-30s$ \n'  $'10.63.112.56\r'
Biffen
  • 6,249
  • 6
  • 28
  • 36
AWS_Beginner
  • 382
  • 9
  • 21
  • 1
    It looks like you read the IP address from a file with Windows-style line-endings (CR LF). – Biffen Jun 07 '21 at 13:24
  • @Biffen What changes should i do to print only IP address – AWS_Beginner Jun 07 '21 at 13:27
  • @Biffen tried this but no luck ```printf "%-60s read-successful %-30s$value1 \n" "${value2}|tr -d '\r'" >> output1``` – AWS_Beginner Jun 07 '21 at 13:39
  • 1
    `${value2%$'\r'}` is the correct formulation. Or `${testVar//$'\r'}`. Checking the proposed duplicate it does show answers that work in this context (and don't have a pipeline's inefficiency) – Charles Duffy Jun 07 '21 at 13:54
  • @AWS_Beginner, btw, while I don't recommend it, if you _did_ want to use a `tr`-based approach for some reason, that might look like `printf "%-60s read-successful %-30s$value1 \n" "$(tr -d <<<"${value2}")"` (though really, `value1` should have its own `%s` placeholder and be a separate argument rather than substituted into the format string) – Charles Duffy Jun 09 '21 at 15:56

0 Answers0