• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Run search/replace on all files

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic