How to grep specific lines and print only those that match character

I have a number of pdb files and I want to grep only those lines that starts with ^FORMUL and if line has C followed by the number that is larger then (C3,C4,C5,C6 etc) then I should not print it.

I used this one for extracting line that start with FORMUL but don't know how to search trough each of the lines and match it with C followed by 3>.

grep ^FORMUL *pdb (probably here have to put some kind of cutoff that if inside each line C3> is found then don't print it).

3OC2.pdb:FORMUL 3 HOH *207(H2 O) (print it)
3OC7.pdb:FORMUL 2 SF4 FE4 S4 (print it)
3OC8.pdb:FORMUL 3 NIC C5 H7 N O7 (don't print, there is C5)
3OC9.pdb:FORMUL 4 HOH *321(H2 O) (print it)
3OC10.pdb:FORMUL 3 HEM 2(C34 H32 FE N4 O4) (don't print, there is C34)

2 Answers

Use two greps:

grep '^FORMUL' *pdb | grep -vE 'C([3-9]|[12][0-9])'

The first lists the lines matching ^FORMUL, the second removes (-v inverts the match) those matching C followed by a digit between 3 and 9, or two digit numbers beginning with 1 or 2 (so every number from greater than 3 will be removed).

11

I think this should work:

awk '/^FORMUL/ && !match($4, /C[3-9]?[0-9]/) {print;}' *.pdb 

?: have an online data source we could try?

ref(page 154): ftp://

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like