LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   remove whitespace at end of file (https://www.linuxquestions.org/questions/programming-9/remove-whitespace-at-end-of-file-238842/)

FunkyRes 10-04-2004 08:30 PM

remove whitespace at end of file
 
I've got a shell script that generates an rpm spec file for a certain group of data modules (the modules are used by a front end application that lets the user do things with their content)

Anyway the modules are packaged in a .zip file containing an info file that tells the gui app about the app, and is absolutely perfect for ripping information from with which to generate a spec file.

The problem is the Description.
The descriptions are long and all on one line - which makes it easy to grab (grep "^About=")

The issue is that I need to format the description (and do some clean up of past formatting attempts in the description part (as it sucks, combination of spaces and tabs and \par to make it somewhat justified depending upon the creators whim, some using none and some using some in some places etc. - it's ugly) as in replace \par\par with two newlines, then replace a single \par with a space, replace tabs with a space, remove leading white space, replace multiple spaces with a single space, and then pass it off to nroff for proper linewrap formatting for the generated spec file)

Everything is working perfectly - except there is a HUGE amount white space at the end of it after nroff is through with it.

I need to remove that white space. I can't just remove trailing whitespace because that gets rid of the legitimate paragraph breaks (where there use to be \par\par in the original Description)

Anyone know of any cool way to have perl remove only the trailing white space after the last non white space character?

aluser 10-04-2004 09:42 PM

I'm not completely sure if I read you right. Is this what you mean?
Code:

perl -i -e 'while (<>) { print $last if $last; $last = $_; } $last =~ s/\s+$//; print "$last\n"' myfile
(completely untested)

FunkyRes 10-05-2004 12:31 AM

That didn't work for me - but I solved it another way.

I used wc and head and tail and grep to go by the output line by line, so that I would get the value of the last line that contains content.

Then I just used head with that.


All times are GMT -5. The time now is 11:50 PM.