I have a huge file with SQL queries on each line. But that file has some bugs, and re-creating it would take too much time. I have some lines with missing semicolon. I see that these lines always end with ) followed by the newline. So what I did is following:
cat file.sql | tr ')\n' ');\n' > new_file.sql
But this simply adds semicolon on ALL lines. I don't understand, it seems it doesn't detect the parentheses. I simply want to find all lines ending with parentheses without a semicolon, and add it before newline.
How can I do that? I know sed
cannot deal with newlines, so I thought tr
is the best choice.
Don't use tr
for this, it works on sets of characters rather than strings - the command you have will translate all )
into )
and all \n
into ;
, ignoring the excess character in set 2:
pax> echo 'line1()
...> line2();
...> line3()' | tr ')\n' ');\n'
line1();line2();;line3();
sed
is a better tool for this job:
sed 's/)$/);/' file.sql > new_file.sql
What that does is to replace any )$
sequence (closing parenthesis at the end of a line) with );
. You can see it in action in the following transcript:
pax> echo 'line1()
...> line2();
...> line3()' | sed 's/)$/);/'
line1();
line2();
line3();