0

I have a text file which has combination of lower/upper characters. I want to remove lines where any of a few specific lowercase letters are seen.

Text file contents:

aASDF1234qwerty;lkJHG
fdsaAghiyirchgdadgqa2

Task - Remove all lines where any of the lowercase characters 'a','e','j','k' occurs.

I tried below option to get all lines where only upper case are found but its not working as expected. grep -wo '[[:alnum:]]*A*E*J*K*[[:alnum:]]*' test.txt

5
  • You might want to read about the -v option in the grep man page. Jul 27, 2017 at 21:40
  • All of the given lowercase letters must be on the line, or any of them?
    – Jeff Schaller
    Jul 27, 2017 at 21:40
  • Welcome to U&L. What have you tried so far for your homework ?
    – steve
    Jul 27, 2017 at 21:42
  • I tried below option to get all lines where only upper case are found but its not working as expected. grep -wo '[[:alnum:]]*AEJK[[:alnum:]]' test.txt Jul 27, 2017 at 21:56
  • Jeff, any of them Jul 27, 2017 at 21:58

1 Answer 1

4

Using grep:

grep -v '[aejk]' /path/to/file

Using awk:

awk '! /[aejk]/ { print }' /path/to/file

Using sed:

sed '/[aejk]/d' /path/to/file

The regular expression used in all of these examples, /[aejk]/, can be transliterated to mean "any character of: a, e, j, or k". The grep and awk invocations return any line which does not match that expression; the sed invocation has the same end result, but works by omitting from the parsed file any lines which do match.

11
  • hi DopeGhoti, aejk is not a string but individual characters. so I want to scan each of this individual letters in entire file and remove the lines where combination of this charcters are all lower case. Jul 27, 2017 at 21:59
  • I'm fairly certain that's what the brackets do. When you search for [aejk] you are searching for a, e, j, and/or, k. Not for "aejk". You can test it at: regex101.com
    – jesse_b
    Jul 27, 2017 at 22:10
  • Please clarify in your question: Are you looking to exclude any line in which any of these lower case letters appear, or in which all of them appear in that specific sequence?
    – DopeGhoti
    Jul 27, 2017 at 22:21
  • 1
    @Wildcard, yes that's what I initially thought too but his comment on this answer contradicts that: I want to scan each of this individual letters in entire file and remove the lines where combination of this charcters are all lower case.
    – jesse_b
    Jul 27, 2017 at 22:40
  • 1
    @Jesse_b, ah. Oh well, this answer matches the question as currently edited. ;) If he wants to clarify he can.
    – Wildcard
    Jul 27, 2017 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .