Ananta
Ananta

Reputation: 3711

grep excluding first char

How do I find a line where a pattern is in middle of line. i.e. in the following example. I want to only get 8th line but exclude 1st and 5th line grepping "@"

I know i would use grep "^@" to find only in first character but how to exclude it?

@DD65WKN1:203:H7T67ADXX:2:2216:19936:100494 1:N:0:
GTCGTTCTTCAGGTTCTC
+
FFFFFIIIIFFFIFFFFF
@DD65WKN1:203:H7T67ADXX:2:2216:6629:100501 1:N:0:
TAAAGTAGCAAAAATG
+
FFFFFFFFIFBFIFFF@DD65WKN1:203:H7T67ADXX:2:2216:6629:100501 1:N:0:
TAAAGTAGCAAAAATG
+
FFFFFFFFIFBFIFFF

Thanks

Upvotes: 0

Views: 2232

Answers (3)

msw
msw

Reputation: 43527

The best thing about unix filters is combining them

grep --invert-match '^@' file | grep '@'

or more traditionally

sed '/^@/d' file | grep '@' 

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174844

Use grep with Perl-regex option which supports negative lookbehind.

$ grep -P '(?<!^)@' file
FFFFFFFFIFBFIFFF@DD65WKN1:203:H7T67ADXX:2:2216:6629:100501 1:N:0:

The above grep command will print the line which doesn't have @ symbol at the begining but it may present anwhere on that line.

Upvotes: 1

fedorqui
fedorqui

Reputation: 290415

You can match any character beforehand, so that @ won't be matched if just in the first position:

$ grep '.@' file
FFFFFFFFIFBFIFFF@DD65WKN1:203:H7T67ADXX:2:2216:6629:100501 1:N:0:

Note that . matches any character. To be completely sure (first solution would match a line starting with @@), you can negate @ by using:

grep '[^@]@' file

Or also indicate that you want to find any line starting with a no-@ set of characters (at least one, as indicated by +).

grep '^[^@]\+@' file

Upvotes: 6

Related Questions