Amit Arora
Amit Arora

Reputation: 1

Bash: Filtering records in a file based on multi column delimiter

Need help in Bash to filter records based on a multicolumn delimiter. Delimiter is |^^|

Sample record

[email protected]|^^|xyz|^^|307

Awk runs file when used with single character delimiter but not with multi character.

awk -F"|^^|" "NF !=3  {print}" file.txt

Any suggestions?

Upvotes: 0

Views: 88

Answers (3)

Ed Morton
Ed Morton

Reputation: 203324

The issue is that every character in your delimiter is a regexp metacharacter so you need to escape them when appropriate so awk knows you want them treated literally. This might be overkill:

awk -F'\\|\\^\\^\\|' 'NF!=3' file.txt

but I can't test it since you only provided one line of input, not the selection of lines some of which do/don't match that'd be required to test the script.

Upvotes: 1

Haifeng Zhang
Haifeng Zhang

Reputation: 31895

One way is to escape all the regex characters as @Ed Morton answered.

Alternatively, you can replace all |^^| with a single character which never shows in your file content, here let's say a comma

sed 's/|^^|/,/g' file.txt
[email protected],xyz,307

The command would be

sed 's/|^^|/,/g' file.txt | awk -F, 'NF != 3'

Upvotes: 0

tomc
tomc

Reputation: 1207

awk -F "<regex>" ...

It is not a multicolumn delimiter, is is a regular expression

simple regex, such as match this single char are what you get use to, but not all there is.

Upvotes: 0

Related Questions