Assumptions:
- user provides a list of characters to match on (
x
and y
in the provided example)
- lines of interest are those that contain only said characters (plus white space)
- matches should be case insensitive, ie,
x
will match on both x
and X
- blank/empty lines, and lines with only white space, are to be ignored
Adding more lines to the sample input:
$ cat filename.txt
xyz
xy
yx
zyx
---------
xxx
abc def xy
Xy xY XY
z x yy z
x; y; X; Y:
xxyYxy XXyxyy yx # tab delimited
# 1 space
# blank/empty line
NOTE: comments added for clarification; file does not contain any comments
One awk
idea:
awk -v chars='xY' ' # provide list of characters (in the form of a string) to match on
BEGIN { regex="[" tolower(chars) "]" } # build regex of lowercase characters, eg: "[xy]"
{ line=tolower($0) # make copy of all lowercase line
gsub(/[[:space:]]/,"",line) # remove all white space
if (length(line) == 0) # if length of line==0 (blank/empty lines, lines with only white space) then ...
next # skip to next line of input
gsub(regex,"",line) # remove all characters matching regex
if (length(line) == 0) # if length of line == 0 (ie, no other characters) then ...
print $0 # print current line to stdout
}
' filename.txt
This generates:
xy
yx
xxx
Xy xY XY
xxyYxy XXyxyy yx
NOTE: the last 2 input lines (1 space, blank/empty) are ignored