1

Need to filter and show log lines, if line contains exactly 2 commas, and does not contains a specific string. Which linux command need I use, awk, grep, what is the expression?

For second condition I use this:

awk '!/specificstring/' ./log/file/path

Two comma check I do not know how to put in. Usually line is like this two:

arbitrary,arbitrary,arbitrary,arbitrary
arbitrary,arbitrary,arbitrary

Need the second type of line.

Tried something like this:

grep -P '[^,]+[,][^,]+[,][^,]+[,]"specificstring"[^,]+' ./log/file/path

How to exclude "specificstring"?

0

2 Answers 2

2

I suggest:

grep '^[^,]*,[^,]*,[^,]*$' file | grep -v 'specificstring'
0

This can can be done in one pass with awk, by combining the two conditions:

awk '/^[^,]*,[^,]*,[^,]*$/ && !/specificstring/' ./log/file/path

The same can be achieved in perl with an almost identical command:

perl -lane 'print if /^[^,]*,[^,]*,[^,]*$/ && !/specificstring/' ./log/file/path

To answer the question in its more generic sense, i.e. filter lines if an arbitrary number of string occurrencies is found:

perl -lane '@m = /,/g; print if scalar @m == 2 and !/specificstring/' ./log/file/path
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .