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

  • You might want to read about the -v option in the grep man page. – glenn jackman Jul 27 '17 at 21:40
  • All of the given lowercase letters must be on the line, or any of them? – Jeff Schaller Jul 27 '17 at 21:40
  • Welcome to U&L. What have you tried so far for your homework ? – steve Jul 27 '17 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 – Nikhil Mehta Jul 27 '17 at 21:56
  • Jeff, any of them – Nikhil Mehta Jul 27 '17 at 21:58
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.

  • 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. – Nikhil Mehta Jul 27 '17 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 '17 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 '17 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 '17 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 '17 at 22:47

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.