I have a problem with checking if each line of my files is ending with a letter (A-Z (always capital))followed by exatcly 8 digit numbers (doesnt matter which one). So I have a number of files and content of each file looks like this:

Nc1nc2cc3OCCOc3cc2s1 A10000001 CCN(CC)C1CCN(Cc2cc(I)cc(I)c2O)CC1 B100000002 CCN(CC)C1CCN(Cc2cc(cc(I)c2O)C#CCO)CC1 C10000003 

There is always a space between this "string" and letter wtih numbers. So, in this example B100000002 there are 9 digits after letter. Since I done most of the things manually I would like to check if in my files there are errors. Can anybody help me with some bash command so I can see which lines have different, incorrect pattern?

Thanks!

2 Answers

You can use grep to get the lines that don't follow the rule:

grep -v ' [[:upper:]][0-9]\{8\}$' file* 
  • space matches itself
  • [[:upper:]] is matched by any uppercase letter
  • [0-9] matches a digit
  • \{8\} is a "quantifier", it means the preceding construct must be repeated 8 times
  • $ matches at the end of line
  • -v shows the lines that are not matched
2

you could grep with a perl regexp:

grep -P ' [a-zA-Z]{1}[0-9]{8}$' 

-P : for perl Regular expression

: the regexp starts with a space, because you want a space before the uppercase letter

[a-zA-Z]{1} : Exactly 1 alphbaetic char lowercase or uppercase (you can remove a-z for uppercase only ie [A-Z]{1})

[0-9]{8} : exactly 8 numeric chars

$ : end of line

If you want to display the lines that does not match the pattern, just add the -v option to the grep command.

If you want to display lines numbers, add the -n option.

grep -Pvn ' [a-zA-Z]{1}[0-9]{8}$' 

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