Friday, May 10, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 2720  / 2 Years ago, wed, april 13, 2022, 9:36:46

I have a few hundreds of files that has this pattern



@<TRIPOS>ATOM
2 H18 65.2220 Du 1 RES1 0.0000
@<TRIPOS>BOND
1 3 5 ar
@<TRIPOS>SUBSTRUCTURE


among them, some of the files are missing the line after the @<TRIPOS>BOND and they look like



@<TRIPOS>ATOM
2 H18 65.2220 Du 1 RES1 0.0000
@<TRIPOS>BOND
@<TRIPOS>SUBSTRUCTURE


I'm trying to find all the files in my working directory that are missing the numeric line after the @<TRIPOS>BOND and move them to another directory. I know this is a simple task, but I am quite new to Linux.



Note: the files vary in length and line numbers, this is why I am "grepping" the line after the @<TRIPOS>BOND string.



Here's one of my codes, which I was planning to write in a for loop. It doesn't do the job, but I am showing it to show one of my trials.



cat file | grep -A1 '@<TRIPOS>BOND' | awk 'FNR == 2 {print}'


Thank you


More From » command-line

 Answers
2

If your version of grep supports PCRE mode (-P) you could try a multiline match that finds instances of @<TRIPOS>BOND that are followed (after only a newline) by @<TRIPOS>SUBSTRUCTURE e.g.



grep -lzP 'Q@<TRIPOS>BONDE
Q@<TRIPOS>SUBSTRUCTUREE' *


The Q and E may be unnecessary in this case but are intended to force literal matching (in case @, >, < have special meaning in the Perl regex syntax). The -l tells grep to list the matching files instead of printing the match. You can then use the list of files as an input to the mv command e.g.



grep -lzP 'Q@<TRIPOS>BONDE
Q@<TRIPOS>SUBSTRUCTUREE' * | xargs mv -t /path/to/newdir/





Additional info



You could express the second part of the match as a lookahead but I don't think it has any advantage in this case



grep -lzP 'Q@<TRIPOS>BONDE
(?=Q@<TRIPOS>SUBSTRUCTUREE)' *


Equivalent expressions in pcregrep (which is not part of the standard Ubuntu system, but obtainable from the repository) would be something like



pcregrep -lM 'Q@<TRIPOS>BONDE
Q@<TRIPOS>SUBSTRUCTUREE' *


and



pcregrep -lM 'Q@<TRIPOS>BONDE
(?=Q@<TRIPOS>SUBSTRUCTUREE)' *

[#25379] Thursday, April 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pardsea

Total Points: 290
Total Questions: 115
Total Answers: 98

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;