jkshah
jkshah

Reputation: 11703

Search pattern containing forward slash using AWK

In AWK, is there a way to match pattern containing forward slash / without actually escaping it?

awk '$1~/pattern/' file

The above command works fine as long as there isn't any / in the pattern.

I am looking for something similar to what's available in sed for using different separators in search and replace syntax.

Upvotes: 13

Views: 26594

Answers (2)

Kent
Kent

Reputation: 195059

This may be OK for you:

kent$ echo "///aaa"|awk '/[/]/'
///aaa

Upvotes: 5

Jotne
Jotne

Reputation: 41456

Yes, you can do

awk '$0~v' v="patt/ern"

Explanation

N.B.: extracted from comments, so upvote them too!

In awk dynamic regex and regex constant are not exactly same. The example in OP's question was regex constant you turned it into dynamic regex (see. Using Dynamic Regexps).

Upvotes: 14

Related Questions