deleting blank lines ONLY at the end of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting blank lines ONLY at the end of the file
# 1  
Old 04-27-2011
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 work:
Code:
head -`grep -nv '^[[:space:]]*$' /your/file | cut -d ':' -f 1` /your/file

May be some has another idea, thanks a lot.
# 2  
Old 04-27-2011
Is this just for a single file? If so, probably what you're going to want to do is count either characters or lines and start deleting blank lines after that.

If it's for multiple files, can you give some example data?
# 3  
Old 04-27-2011
This is for the single file
# 4  
Old 04-27-2011
Here's a quick perl script to do this trick. The idea is to store the contents of the file into an array. Then pop the end of the array until it reaches a none blank row.

Code:
#!/usr/bin/perl
my $file = 'mytestfile';
my @tmparray;
open MYFILE, "$file";
foreach (<MYFILE>) {
  chomp;
  push @tmparray, $_;
}
close MYFILE;
my $last_row = pop @tmparray;
while ($last_row eq '') {
  $last_row = pop @tmparray;
}
push @tmparray, $last_row;
foreach (@tmparray) {
  print "$_\n";
}


Output:
Code:
 
# cat mytestfile
hello
this
is
a
test
 
# ./remove_last_blanks.pl
hello
this
is
a
test

# 5  
Old 04-27-2011
Thanks a lot
# 6  
Old 04-27-2011
If by blank line you mean an empty line:
Code:
perl -ne '$s.=$_; if (/./) {print $s; $=""}' file

If by blank line you mean an empty line or a line that may contain whitespace characters:
Code:
perl -ne '$s.=$_; if (/[^[:space:]]/) {print $s; $=""}' file

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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... (6 Replies)
Discussion started by: swathi reddy1
6 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

4. 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

5. Shell Programming and Scripting

Perl : blank lines are displayed in the output after deleting few rows from excel

I am working on an assignment to pull all the records from excel sheet programatically and use the data for further calculations. In this process, I first defined 10 records in excel sheet and executed the below code. In the first run it is OK. But after deleting last few rows in excel sheet and... (0 Replies)
Discussion started by: giridhar276
0 Replies

6. UNIX for Dummies Questions & Answers

Add blank line to end of file

Alright, so I was looking around a bit on the forum before posting and still don't really understand so I figured I'd post my own question. I am appending two files using cat file_1.txt >> file_2.txt The problem is that I need a blank line in between the two different text files and it does... (2 Replies)
Discussion started by: cgagnon
2 Replies

7. Shell Programming and Scripting

deleting the lines at the end of the file.

I have a text file with two coulmn first column is just used in to show the line number, these line number are not there in the real file. I want to delete the line 16(in this file) here, even tough there is no data inside it . this empty line is causing me a problem by throwing me garbage... (12 Replies)
Discussion started by: shashi792
12 Replies

8. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies

9. 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

10. Shell Programming and Scripting

Blank Lines - End of file

Hi all I need to strip blank lines from the end of a file. I have searched and found topics on how to strip lines from the entirety of a file - however I need to limit this to only the last 3-4 lines. Any ideas? Thanks (4 Replies)
Discussion started by: saabir
4 Replies
Login or Register to Ask a Question