Reputation: 1
I see that awk can recognize escape sequences
$ awk 'BEGIN {print "foo\nbar"}'
foo
bar
However, from the input it does not
$ awk '{print $1}' <<< 'hello\nworld'
hello\nworld
Can it be made to recognize escape sequences from the input?
Upvotes: 4
Views: 183
Reputation: 1
This works with variables as well
$ set 'hello\nworld'
$ printf %b "$1" | awk '{print $1}'
hello
world
Upvotes: 0
Reputation: 77145
You need to do something like this -
[jaypal:~/temp] awk '{print $1}' <<< $'hello\nworld'
hello
world
Upvotes: 3
Reputation: 1316
The here-string you're using does not expand the newline escape sequence into an actual newline. Try this:
`echo -e "hello\nworld" | awk '{print $1}'`
Or alternatively:
awk '{print $1}' <<< "hello
world"
Upvotes: 0