I am trying to concat two strings in awk. One of them seems to contain '\\r':
stringA = "Hello\r"
stringB = "Hi"
print stringA stringB
results in
Hillo
What can I do to remove the '\\r' if I can't change the source of stringA?
Thanks :)
Given:
$ awk 'BEGIN {s1="Hello\r"; s2="Hi"; print s1 s2}'
Hillo
Just replace the carriage return \\r
with gsub()
:
$ awk 'BEGIN {s1="Hello\r"; s2="Hi"; gsub(/\r$/,"",s1); print s1 s2}'
HelloHi
That is, gsub(/\\r$/,"",s1)
looks for the \\r
at the end of the string s1
and replaces it with ""
, that is, it removes it.
If multiple carriage returns appear, just get rid of the $
so that it doesn't just match the one at the end of the line.
Ed Morton suggests in comments that probably just one carriage return will appear in a string, which is quite reasonable. If this is the case, just use sub()
instead: sub(/\\r$/,"",s1)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.