tomtom
tomtom

Reputation: 614

How to grep and filter out at the same time?

how can I grep a line starting with number 2 excluding number 26 after the tab

file1.txt:

2    0   'C001000'
2    26 'C001000' So we come to step 3
2    0   'C001000'
2    26 'C001000' So we come to step 3


expected output 

    2    0   'C001000'
    2    0   'C001000'
   

my approach cat file1.txt | grep '^2' // which grabs all line staring with 2

cat file1.txt | grap '^2' $(printf '\t') '26'

Upvotes: 2

Views: 925

Answers (3)

James Brown
James Brown

Reputation: 37394

Using the requirement from the OP's comment: line starting with 2 followed by tab and then excluding number starting with 26 - and grep:

$ grep $'^2[\t]\(2[^6]\|[^2]\)' file

Output using sample data:

2       0       'C001000'
2       0       'C001000'

Explained some:

  • grep $'\t' This syntax is used to grep for tab
  • ^2[\t] line starting with 2 followed by tab
  • \(2[^6]\|[^2]\) and then [my definition* of] excluding number starting with 26
  • \(2[^6]\|[^2]\) is basically starts with a 2 followed by anything but a 6 or starts with anything but a 2

Upvotes: 2

Sundeep
Sundeep

Reputation: 23667

As far as I can understand, this should work:

awk -F'\t' '$1==2 && $2!=26' ip.txt

This will process fields based on tab character. If first field is 2 and second field is not 26, the line will be printed.

If you need to print lines that start with 2 like 2a as well, use:

awk -F'\t' '$1 ~ /^2/ && $2!=26' ip.txt

The above solutions will work only if the character after second field is tab as well. If that is not the case, then this might work, but need a better description of the input.

awk -F'\t' '$1==2 && $2 !~ /^26([[:space:]]|$)/' ip.txt

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

With GNU grep:

grep -P '^2(?!\t26\b)' file

where

  • -P - enables the PCRE regex engine
  • ^2(?!\t26\b) - matches
    • ^ - start of string (jhere, line)
    • 2 - a 2
    • (?!\t26\b) - not followed with a tab and 26 as a whole word (\b is a word boundary). You might use (?!\t26(?:\t|$)) or (?!\t26(?![^\t])) to make sure 26 is matched only in between tab chars/end of the string.

See an online demo:

s="2    0   'C001000'
2   26  'C001000'   So we come to step 3
2   0   'C001000'
2   26  'C001000'   So we come to step 3"
grep -P '^2(?!\t26\b)' <<< "$s"

Output:

2   0   'C001000'
2   0   'C001000'

Upvotes: 2

Related Questions