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

时间:2016-04-04 16:35:28

标签: bash awk

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

Sample record

xyz@ATT.NET|^^|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?

3 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:0)

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.

答案 2 :(得分:0)

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
xyz@ATT.NET,xyz,307

The command would be

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