Deleting the last non blank line from a file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Deleting the last non blank line from a file
# 1  
Old 01-23-2018
Deleting the last non blank line from a file

Code:
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.

Code:
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

Last edited by Don Cragun; 01-23-2018 at 01:22 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 01-23-2018
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.
# 3  
Old 01-23-2018
Hi sir,
I am using below command to print the last non blank line
Code:
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
Code:
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
Moderator's Comments:
Mod Comment So far you have 4 posts. 4 of those posts have had to be edited by a moderator or administrator to add the CODE and ICODE tags that you should have included in your posts when displaying sample input, sample output, and code segments.

Continued refusal to properly use CODE tags may result in you being placed in read-only status or being banned from the UNIX & Linux Forums.

Last edited by Don Cragun; 01-23-2018 at 02:20 AM.. Reason: Add CODE tags again.
# 4  
Old 01-23-2018
Quote:
Originally Posted by swathi reddy1
Hi sir,
I am using below command to print the last non blank line
Code:
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
Code:
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
Moderator's Comments:
Mod Comment So far you have 4 posts. 4 of those posts have had to be edited by a moderator or administrator to add the CODE and ICODE tags that you should have included in your posts when displaying sample input, sample output, and code segments.

Continued refusal to properly use CODE tags may result in you being placed in read-only status or being banned from the UNIX & Linux Forums.
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?
# 5  
Old 01-23-2018
I am using below command to print the last non blank line

Code:
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.


Code:
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
# 6  
Old 01-23-2018
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:
Code:
sed "$(awk '/./{line=NR} END{print line}' file1)d" file1>file2

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

And, third, just using ed:
Code:
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).
# 7  
Old 01-23-2018
The following solution works like Don's first solution, needs two passes thru the input file
Code:
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
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Issue deleting blank line from a file

I'm trying to delete the blank lines from the file $Sfile. tried the below set of commands. Both are giving the same error (: bad interpreter: No such file or directory) awk 'NF > 0' $Sfile > $Tfile cat $Tfile sed -i '/^$/d' $Sfile cat $Sfile Not sure if there's any other problem with... (17 Replies)
Discussion started by: Pandee
17 Replies

3. Shell Programming and Scripting

deleting blank lines ONLY at the end of the file

Hi Guys, I have a quetion which was already discussed in the forum, but for some reason all approches suggested fail for me. I have a file which have blank lines at the body of the text as well as at the end. I need to delete ONLY blank lines at the end. Unfortunatly the approach below does not... (5 Replies)
Discussion started by: aoussenko
5 Replies

4. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

5. Solaris

deleting blank space in beginning of line in vi

How can we delete all the blank spaces in the beginning of some lines in a text in vi? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

6. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

7. Shell Programming and Scripting

how to replace a line in file with blank line

Hi I nned cmd to which will help me to replace a line in file with blank line e.g. file1 a b c d e after running cmd I shud get file1 b c d e (5 Replies)
Discussion started by: tarunn.dubeyy
5 Replies

8. Shell Programming and Scripting

Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank, I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file. Is there a way I can recursively... (2 Replies)
Discussion started by: ruchimca
2 Replies

9. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies

10. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies
Login or Register to Ask a Question