LinuxQuestions.org
Review your favorite Linux distribution.
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 02-20-2006, 06:18 AM   #1
kevingpo
Member
 
Registered: Nov 2004
Location: Edinburgh, UK
Distribution: Fedora Core 3
Posts: 188

Rep: Reputation: 30
QUERY: strip trailing whitespace ... woes :S


I am wondering about what's the best and easiest way to strip trailing whitespace from every single file in a folder, recursively.

I want to write a program/script so that you pass in a folder name and it'll recursively go through all sub-folders and strips trailing whitespace from every single file.

Trailing whitespace includes:

1. whitespaces at end of text lines, strip whitespace but retain text
2. whitespaces on empty lines, strip whitespace but leave the empty line
 
Old 02-20-2006, 06:57 AM   #2
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
Homework?

You could create a tempfile that lists all the files recursively with find. Then make a for loop go through the tempfile and check the types of all the files listed in it with the file command (or you could simply have find only find .txt files if that's approproate). Finally just sed the files that are text.
 
Old 02-20-2006, 07:20 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
What scripting/programming language?

Assuming a shell script:
find allows you to recursively go through all files in adirectory tree. Use its -exec option to execute a command on each file.

In that command rename the file to <original name>.old (or similar), run a sed command to strip off the whitespace of the file's contents redirecting the output of sed to a new file with the original name, and finally delete the original file (xxx.old).

Should be doable in a (long) one-liner.
 
Old 02-20-2006, 08:15 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
look at something like
perl -pi -e 's/ *$//'
 
  


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
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
QUERY: K editors woes :S kevingpo Fedora 2 09-17-2005 10:32 PM
QUERY: how to install more apps from install cds? woes :S kevingpo SUSE / openSUSE 2 09-10-2005 05:07 AM
QUERY: about SUSE 9.3 5th mysterious CD woes :S kevingpo SUSE / openSUSE 3 09-07-2005 03:04 PM
HELP/QUERY: Knoppix rules woes :S kevingpo Linux - Distributions 1 09-07-2005 01:42 PM

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

All times are GMT -5. The time now is 12:06 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