Grep all lines with exactly one instance of a specific character
try
grep '^[^#]*#[^#]*$' file
where
^ ; begin of line
[^#]* ; any number of char ≠ #
# ; #
[^#]* ; any number of char ≠ #
$ ; end of line
as sugested, you can grep on the whole line, with
grep -x '[^#]*#[^#]*'
with
- same pattern without begin of line/end of line anchor.
-x
to grep whole line, seeman grep
-x, --line-regexp Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.
Using awk
:
awk -F'#' 'NF==2' infile
based on #
field separator, if number of fields in a line was exactly two fields then will print out. note that for example #x
or x#
or even #
are considered two fields so.
With two calls to grep
: pick any line that has at least one #
, then remove those that have at least two:
grep '#' filename | grep -v '#.*#'