grep meister

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
sgrogan
Posts: 5211
Joined: Wed Oct 22, 2014 5:02 pm

grep meister

Postby sgrogan » Sat Feb 10, 2018 4:06 pm

I need a one liner to replace <string1> with <string2> recursively. Folder Names, File Names, and File contents.
Thanks in advance.
lambda
Posts: 34
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: grep meister

Postby lambda » Sat Feb 10, 2018 7:34 pm

You are asking a lot, and it's not clear what you mean recursively: walk a folder tree and process all files or actually recursively substitute strings?

The tool to replace file contents is sed. Something like sed -i -e 's/string1/string2/g' filename1 filename2 ... should work.

The tool to change file names (including folder names) is mv.

The tool to recursively walk a folder tree is find.

So for changeing file contents something like
find targetfolder -exec sed -i -e 's/string1/string2/g' {} \;
should work and for renaming
find targetfolder -name *string1* | while read name; do mv $name $(echo $name | sed -e s/string1/string2/g'); done
or
find targetfolder -name *string1* -exec mv {} $(echo {} | sed -e s/string1/string2/g') \;

Disclaimers:
* I didn't test this
* I probably got some quoting/escaping wrong
* This will break horribly on filenames with whitespace, etc.

I hope this gets you started, but you will need to tweak this to what you acutally need. Solving this kind of problem generally (ie robust against strange filenames, strange patterns, etc.) is hard, so I hope your actual problem is easier then what you stated.

Also if you allowed things like creation of temporary files, there might be much more efficient solutions. Of course this is only a concern if there are really lot's of files affected and you need to run this repeatedly.
wandererfan
Posts: 2796
Joined: Tue Nov 06, 2012 5:42 pm

Re: grep meister

Postby wandererfan » Sun Feb 11, 2018 1:09 pm

sgrogan wrote:
Sat Feb 10, 2018 4:06 pm
I need a one liner to replace <string1> with <string2> recursively. Folder Names, File Names, and File contents.
Thanks in advance.
This is to go into a script, right? So regexxer and PyRenamer are no help?
User avatar
NormandC
Posts: 18534
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: grep meister

Postby NormandC » Sun Feb 11, 2018 4:06 pm

wandererfan wrote:
Sun Feb 11, 2018 1:09 pm
This is to go into a script, right?
I know why sgrogan asks for this. We've discussed of providing a freecad-legacy package for v0.16.6712 when v0.17 is released. Because when it is, existing freecad-stable PPA users will see their 0.16 version be automatically upgraded to 0.17. But considering migration issues with some models that mix Part and PartDesign work flow, it may be best at least at first to edit legacy models in 0.16. So having both versions installed is desirable.

Basically what's needed is to reproduce what we did for the freecad-daily package, which is to replace all occurrences of "freecad" in filenames and inside files with "freecad-legacy" in the debian folder shown in the gitpackaging release branch. Of course a new "legacy" branch needs to be created first. I did all of that manually for freecad-daily, sgrogan is smarter to look for an automated way.
lambda
Posts: 34
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: grep meister

Postby lambda » Sun Feb 11, 2018 5:37 pm

Ah, you only expect to run this once on a rather small set of files. In this case my solution is clearly overengineered - probably would take longer to debug the command line then to do things semi-manual:

I propose just renaming the files manually and for the content use this simplified command:
sed -i -e 's/freecad/freecad-legacy/g' * */*

This is still untested, but you have the files under version control, so nothing bad should happen. Right?

HTH,
Harald
User avatar
sgrogan
Posts: 5211
Joined: Wed Oct 22, 2014 5:02 pm

Re: grep meister

Postby sgrogan » Mon Feb 12, 2018 11:49 pm

lambda wrote:
Sat Feb 10, 2018 7:34 pm
You are asking a lot, and it's not clear what you mean recursively: walk a folder tree and process all files or actually recursively substitute strings?
Both I think, but the string substitution is the same for folders, files, and content strings.
Thank You!, this gives me a path forward.
lambda wrote:
Sun Feb 11, 2018 5:37 pm
Ah, you only expect to run this once on a rather small set of files. In this case my solution is clearly overengineered - probably would take longer to debug the command line then to do things semi-manual:
As NormandC has explained this happens enough to try and debug the command line. And yes I have version control so that will help.
wandererfan wrote:
Sun Feb 11, 2018 1:09 pm
This is to go into a script, right? So regexxer and PyRenamer are no help?
This looks very good. I just need to be a better python scripter.

Thanks everyone for the help!