0

I have a single text file. It's a multiple choice exam. In it are several hundred questions, each with four answer choices, one per line, that start with A. B. C. D.

After each A. (and B. etc) should be a single space, then immediately the first char of question text. Like this:

++++++++++++++++++++++++++++++++
This is my question text?
A. Choice 1
B. Choice 2
C. Choice 3
D. Choice 4
++++++++++++++++++++++++++++++++

I want to find all lines that start with a A. that have something other than a single space (a different char OR, say, two or more spaces) before the first character of the answer choice. Then do the same for B. C. and D.

/edit

I want to differentiate between the "OK" and "not OK" lines.

A.<space><any char not a space> is OK.

for example

A.ABC not OK.
A.123 not OK.
A.  ABC not OK

A. 123 OK.
A. ABC OK.

I want to locate all "not OK" lines.

/ end of edit

1
grep -E '^[ABCD]\.([^ ]|  )' file

This would extract all lines from you file that start with A., B., C. or D. followed by either a non-space character or two spaces.

The expression is an extended regular expression (due to the alternation with |) which is why we use -E with grep.

  • This seems to work OK. I'll mark it as the correct answer after I test it some more. Thanks! – mike65535 Jul 16 '18 at 11:24
0

If your grep supports Perl Compatible Regular Expressions (PCRE) you could do a negative lookahead for a single space followed by any non-space character:

grep -P '^A\.(?! \S)' file

or, if you want to combine the search for all four A,B,C,D

grep -P '^[A-D]\.(?! \S)' file
0
 sed -e '/^[A-D][.][ ][^ ]/d' input-file.txt

This will delete all the Ok lines, and what's left would be the not ok lines and which sed would take to stdout for printing.

Assuming that you don't consider a TAB char also as space.

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.