I have a few folders named folder1 folder2 etc.. under a root folder. In each folder there are many xml files. In each xml file there are number of occurence of text patern (in case of under folder1) folder1.xxx folder1.yyy . I want to replace all the folder1.xxx with just xxx and folder2.yyy with yyy etc. I know first I need to get all the folder list and process each files under each folder and replace the texts accordingly. I using Ubuntu linux distro I tried sed but didnt succeed. Could you show me a qucik way to do this no matter whether it perl or bash or any other script. Thanks
2 Answers
(Edited as per your comment):
for dir in folder1 folder2; do sed -i "s/$dir\.//" $dir/* done What this will do is for each directory you list (the folder1 folder2 bit) it will do a simple "stream edit" (sed) on every file in that directory and search for the directory name followed by a dot and replace it with nothing.
- Womble xxx is any string (I want to replace "folder1." with "") and folders names are changing script must get all the folder names. ThanksGok Demir– Gok Demir2009-08-19 07:52:07 +00:00Commented Aug 19, 2009 at 7:52
- especially sed "-i" option what I looked for thanks. You saved my day.Gok Demir– Gok Demir2009-08-19 09:17:46 +00:00Commented Aug 19, 2009 at 9:17
If you have mmv installed, you might try something like
for dir in *; do mmv "$dir/$dir*" "$1"; done mmv should be part of most distributions, but is not installed by default some cases. I'm not sure if it is in Ubuntu.