Removing a line based in a criteria
I just want to delete the line which contain the number of selecte开发者_运维问答d rows in a query. I mean the one in the last line. please help.
[root@machine-test scripts]# ./hit_ratio.sh
193830 432
185260 125
2 rows selected.
If you know you want to delete the last line, but not other lines which contain similar text, or you don't know what text it will contain, sed
is uniquely suitable.
./hit_ratio.sh | sed '$d'
You don't need the power of sed
or the super-powers of awk
if all you want is to delete a line based on a pattern. You can use:
./hit_ratio.sh | grep -v ' rows selected.'
You can do it with awk
and sed
but it's a bit like trying to kill a fly with a thermo-nuclear warhead:
pax> ./hit_ratio.sh | sed '/ rows selected./d'
193830 432
185260 125
pax> ./hit_ratio.sh | awk '$2!="rows"{print}'
193830 432
185260 125
Alternatively, do something with your SQL script. Sometimes, turning on the set nocount on
statement eliminates the "rows affected" line.
My first recommendation is not to have that line outputted, list hit_ratio.sh here maybe it can be modified not to output that line
Anyway if you have to remove only the last line the easiest would be to use head:
./hit_ratio.sh | head -n -1
Using -n and a negative number, makes head print all but the last N lines of the input
Use head
to get the first N - 1
lines of your file, where N is the length of the file (calculated with wc -l
)
head -n $(($(cat lipsum.log | wc -l) - 1)) lipsum.log
works
Pipe through
sed -e '/\w*[0-9]\+ rows\? selected/d'
精彩评论