Deleting the last non blank line from a file

PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
Total number of rows: 5
<<Blank line >>
<<Blank line >>
<<Blank line >>

Now I want to delete the last non blank line ( Total number of rows: 5 )
and paste the remaining text in to another file. so the another file should look like below.

PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 
PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21
<<Blank line >>
<<Blank line >>
<<Blank line >>

which unix command/commands are used for this ?

Thanks,
swathi

The awk utility would be one way to do this. The ed and ex utilities could easily be used to do this in a shell script. The vi utility is an excellent way to do this if you want to do it interactively.

Depending on what operating system you're using, you might want to use the tac utility if your operating system includes that utility. You could do this entirely in the shell command language, but it would be easier with some shells than it would be with others.

There are probably thousands of ways to do this on most UNIX and UNIX-like operating systems. Without knowing what shell and operating system you're using, what experience you have, and what you have tried to solve this problem on your own it is hard to make any meaningful suggestions.

Hi sir,
I am using below command to print the last non blank line

awk '/./{line=$0} END{print line}' file1

Now i want to delete that line and create one more file

But the below command can only delete the last line and copy the remaining lines to the new file

sed '$d' file1>file2

i am looking for a single command which can delete the last non blank record and copy the remaining content to the new file

What operating system are you using?

What shell are you using?

The awk script you showed us does not print the last non-blank line; it prints the last non-empty line. Do you understand the difference? Which is it that you want to delete? A non-blank line or a non-empty line?

I am using below command to print the last non blank line

awk '/./{line=$0} END{print line}' file1

Now i want to delete that line and create one more file

But the below command can only delete the last line and copy the remaining lines to the new file.

sed '$d' file1>file2

i am looking for a single command which can delete the last non blank record and copy the remaining content to the new file

Thanks in advance

---------- Post updated at 01:57 AM ---------- Previous update was at 01:39 AM ----------

sir,

Yes,I wanted to delete non empty line only sir.

Operating system-Linux

Shell-bash

Any of the following three scripts should remove the last non-empty line in a file assuming that there is at least one non-empty line in the file.

First, using awk and sed like you were using:

sed "$(awk '/./{line=NR} END{print line}' file1)d" file1>file2

Second, just using awk :

awk '
{	line[NR] = $0
}
/./ {	lastNB = NR
}
END {	for(i = 1; i <= NR; i++)
		if(i != lastNB)
			print line
}' file1 > file2

And, third, just using ed :

ed -s file1 <<-EOF
	?.?d
	w file2
	q
EOF

The first one is the least efficient (needing to invoke two utilities in addition to the shell).

The second one will copy file1 to file2 unchanged if there are no non-empty lines in file1 . The other two may give you errors in this case.

The last one is probably the most efficient on most systems (since ed is usually smaller and less complex to load than awk ).

The following solution works like Don's first solution, needs two passes thru the input file

awk 'NR==FNR { if (/./) lastNB=FNR; next } lastNB!=FNR' file1 file1 

The following solution needs only one pass (can work on an input stream), and deletes the last line and the following empty lines

awk 'NR>1 && /./ { print buf; buf=rs=""} { buf=(buf rs $0); rs=RS }' file1

With some effort in the END section it would be possible to restore the trailing empty lines.