Zombo
Zombo

Reputation: 1

Recognize input escape sequence

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

Answers (3)

Zombo
Zombo

Reputation: 1

This works with variables as well

$ set 'hello\nworld'

$ printf %b "$1" | awk '{print $1}'
hello
world

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77145

You need to do something like this -

[jaypal:~/temp] awk '{print $1}' <<< $'hello\nworld'
hello
world

bash(1)

Upvotes: 3

user2303197
user2303197

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

Related Questions