简体   繁体   中英

how to remove last lines from CSV file

I am inserting SQL query result into a csv file. At the end of file two rows are getting added as shown below. Can anyone please tell me how to delete these two rows (bank row and '168 rows selected'). Also, it will be great if these rows don't get inserted into csv file in the first place while inserting sql query result into csv file.

pvm:0a12dd82,pvm:0a12dd84,TechnicalErrorProcess,21-JUN-19 07.01.58.560000 AM,pi_halted,38930,1
pvm:0a12dd77,pvm:0a12dd79,TechnicalErrorProcess,20-JUN-19 12.36.27.384000 PM,pi_halted,1572846,1
pvm:0a12dd6t,pvm:0a12dd6v,TechnicalErrorProcess,20-JUN-19 12.05.22.145000 PM,pi_halted,38929,1
pvm:0a12dd4h,pvm:0a12dd4l,TechnicalErrorProcess,17-JUN-19 07.11.43.522000 AM,pi_halted,9973686,1

168 rows selected.

For MSSQL, before select query append the following,

set nocount on; select ...

I'm not sure if that will work for other databases.

Filter the output of the command above to exclude to two last lines.

I see two ways for doing it with a bash command:

head --lines=-2

Or

sed -e '/rows selected/d' -e '/^ *$/d'

(Indeed, this was a placeholder).

You can specify a negative number for the -n parameter using the head command:

-n, --lines=[-]K print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file

so:

head -n -2 input-file.txt > output-file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM