LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2004, 08:30 PM   #1
FunkyRes
Member
 
Registered: Mar 2004
Distribution: CentOS
Posts: 174

Rep: Reputation: 30
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?

Last edited by FunkyRes; 10-04-2004 at 08:34 PM.
 
Old 10-04-2004, 09:42 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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)
 
Old 10-05-2004, 12:31 AM   #3
FunkyRes
Member
 
Registered: Mar 2004
Distribution: CentOS
Posts: 174

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No new line at end of file? What does this mean? BajaNick Programming 12 05-19-2014 08:39 PM
file with ~ at the end ddaas Red Hat 5 07-13-2005 01:47 PM
vim remove LF at the end michal017 Linux - Software 4 11-19-2004 12:26 AM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration