[SOLVED] Get lines which don't end with semi-colon from a file

Issue

I am trying to check the ends of code for semi-colons as they are causing me some issues for a server I have running. To do this I am using a bash script (as I am more familiar with bash) to read through the lines and return those that doesn’t end with a semi-colon. My bash script is as follows

while read line
do
if[$line!=*;]
echo $line
fi
done < $1

When I run the script, it says there is an error by fi but I cannot figure it out. I also realize this will return statements like if and while but that will be fine for my needs.

Given the sample input

use CGI;

print "<html>"
print "<head>";
print "</head>";
print "<body><p> HELLO WORLD </p>";
print "</body>";
print "</html>"

this should be the output

print "<html>"
print "</html>"

Solution

I think the easiest way would be with grep. Given an input.txt file like this:

spam
foo;<Space><Space>
sausage
baked;<Tab>
beans
unladen;

You could do

grep -v ';\s*$' input.txt

and obtain

spam
sausage
beans

grep’s -v flag means “return all lines not matching this regular expression”, so it will skip all lines ending with semi-colons.

If your lines have also spaces after the semi-colons, the \s* means “any sequence of space characters” so grep will remove those lines aswell.

Answered By – Ju Liu

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.