Zhro
Zhro

Reputation: 2614

awk and matching a literal backslash in a string

I'm trying to get the first substring cut along the delimiter of a literal "\n" (actual '\' and 'n' not a new line).

I am able to split the string at the '\' using an octal:

echo "1234\n5678" | awk -F'\134' '{print $1}'

But I cannot figure out how to split with the octal as part of a larger string. For example, the following fails:

echo "1234\n5678" | awk -F'\134'n '{print $1}'

I can do a string replace on the "\n" with sed and then split on that, but shouldn't I be able to do this simply with awk?

Upvotes: 0

Views: 979

Answers (1)

CS Pei
CS Pei

Reputation: 11047

First you don't have to use \134. You can just -F '\\'. For your question, you can use

    echo "1234\n5678" | awk -F '\\\\n' '{print $1}'

\ is used to escape \.

Upvotes: 3

Related Questions