Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run search/replace on all files

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to remove leading and trailing whitespace from files in a directory and sub-directories (simple HTML reduction)

I have the following for a single file, how to run over all files and directories according to a file pattern?



If it helps, I'd also like to run:


I have about 10 more, but that's likely to be enough.
[ July 01, 2008: Message edited by: David O'Meara ]
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, David,

Using bash, you can use 'find' to get all the files recursively and do a 'for' loop on the result. I think you can try something like:



I usually test if I'm getting the correct the file set by doing a 'echo $i' inside the 'for' before putting other commands. And... consider having a backup first as well
[ July 01, 2008: Message edited by: Rodrigo Tomita ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm. You can get a list of candidate files with this:


for `find -type f -regex ".*/\s+.*$" -or -regex ".*\s$"`

However, if you've got directory names with leading spaces, it will give false positives, since my leading-space regex doesn't have the smarts to only look at the LAST slash in the path.

Still, it's a start. Usually I do this sort of stuff with a Perl script.
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahm... were you talking about removing spaces in the file names ?

In my post I thought you meant running your 'sed' command in several files editing the file content... Sorry about that.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, these are production files
Thanks Tim, I could barely understand it, so it must be right

I'll be running a local test before verifying in our TEST environment, and backing up clients before running against them.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Word for the novice:
Since sed provides a line by line replacement, don't attempt to redirect back to the same file

Currently I am using
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some versions of sed provide a "-i" option to update files in place, optionally making backups, like Perl. For example, with GNU sed:



will change "foo" to "bar" and update the original files. "-i~" would keep backups with .txt~ extensions.

By the way, mv will replace the destination if it exists, so you don't have to delete it first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic