user189035
user189035

Reputation: 5789

Regex match using awk for a line starting with a non conforming string

I have a huge file, I want to only copy from it lines starting with

,H|756|F:BRN\

but when I do

awk  '$1 ~ /^ ,H|756|F:BRN\/' file_1.txt > file_2.txt

I get:

awk: line 1: runaway regular expression /^ ,H|756|F ...

Upvotes: 2

Views: 559

Answers (2)

Kent
Kent

Reputation: 195179

If the file is "huge", you can consider grep or ack or ag, which may bring you better performance.

grep '^,H|756|F:BRN\\' input > output

grep uses BRE as default, so you don't have to escape the pipe |. But the ending back-slash you should escape.

Upvotes: 2

Inian
Inian

Reputation: 85780

The meta-characters in the regex match needs to be properly escaped to achieve what you are trying to do. In Extended Regular Expressions (ERE) supported by awk by default | has a special meaning to do alternate match, so you need to escape it to deprive it of its special meaning and treat it literally and the same applies for \

awk '/^,H\|756\|F:BRN\\/' file

Also you don't need to use the explicit ~ match on $1. For a simpler case like this, a string pattern starting with, the /regex/ approach is easier to do.

Upvotes: 2

Related Questions