Shell script to check line end not ending with comma

I have several line in a text file. for example

I like apple;
I like apple
I like orange;

Output: I like apple

I try to use

if grep -q "!\;$"; then

(Not work)

If the output you're trying to get is:

I like apple

then using grep -q is never going to work for you. You'd want something more like:

grep -v ';$' filename

to do that. If you don't want grep to produce any output but an exit code telling you whether or not there were any lines in a file that did not have a semicolon as the last character on the line, then you want something more like:

if grep -qv ';$' filename
then	echo "At least one line in filename does not end in \";\"!"
else	echo "Every line in filename ends in \";\"."
fi
1 Like