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)

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).

  • SInce I am new and cannot vote for useful answers I have to thank You here. Just one thing, since I have many exampels of C10-99 this command wont keep them but I want to keep them as well. Can I select somehow that if it meets C3-C99 range then print them? – djordje Jan 6 '18 at 7:17
  • 1
    See if the updated command helps – muru Jan 6 '18 at 7:22
  • Yeah, it is fine now. Thank You very much. All the best! – djordje Jan 6 '18 at 8:07
  • Hey, one thing. I figure out that this code will delete all the lines in which it founds C followed by the number 2 or more..in my input file this can be found as well: 3OC7.pdb:FORMUL 2 EDO C2 H6 O2 – djordje Jan 8 '18 at 10:31
  • 1
    @djordje then it's probably simpler to just remove the filename from the output: grep -h '^FORMUL' *pdb | ... – muru Jan 8 '18 at 10:42

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://ftp.wwpdb.org/pub/pdb/doc/format_descriptions/Format_v33_A4.pdf

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.