LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 07-12-2011, 07:29 PM   #16
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,191

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784

I agree with MTK, this is not difficult and a solution has been presented to rename the objects.
Here is another alternative:
Code:
while read -r line
do
    dir=${line%/*}/
    name=${line##*/}

    mv "$line" "$dir${name//AA/BB}"
done< <(find Dir1 -depth -name '*AA*')
As for the sed, have a look at the -i option. If using something like the above, I would probably test if a file prior
to move and perform sed then.
 
Old 07-12-2011, 09:20 PM   #17
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yeah, I want to rename files and change the contents that match certain patterns. Since I have many levels of directories; the contents changes are complex, I plan to use "sed -f" to do it. Any suggestion?
 
Old 07-12-2011, 11:03 PM   #18
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,191

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
I am not sure I understand your most recent question? What suggestion do you require? The '-f' option
sounds fine if you already have the changes mapped out in a file.
 
Old 07-13-2011, 06:43 AM   #19
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
How about this:

Code:
while read file
do
    mv "$file" "$(dirname "$file")/$(basename "$file" | sed 's/AA/BB/g')"
    if [[ ! -d $file ]]
    then
        # edit the file with sed
    fi
done < <(find Dir1 -depth)
 
Old 07-13-2011, 08:12 AM   #20
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
My question is how to use SED to change the contents of multiple files. The shell does allow me to use sed with both input and output are the same file as following:
sed 's/AA/ZZ/g' < $sourcefile > $sourcefile


As to the script you suggested, it failed when I tried as followings:

while read -r sfile
do
echo "sfile=$sfile"
done<< ( find $1 -depth )

The error message is:
line 4: syntax error near unexpected token '('
line 4: `done<< (find $1 -depth)`
 
Old 07-13-2011, 08:23 AM   #21
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by mail4mz View Post
My question is how to use SED to change the contents of multiple files. The shell does allow me to use sed with both input and output are the same file as following:
sed 's/AA/ZZ/g' < $sourcefile > $sourcefile
Use sed's "-i" option:

Code:
sed -i 's/AA/ZZ/g' "$sourcefile"
Also, as I said, always double-quote variables that contain a filename.

Quote:
Originally Posted by mail4mz View Post
As to the script you suggested, it failed when I tried as followings:

while read -r sfile
do
echo "sfile=$sfile"
done<< ( find $1 -depth )

The error message is:
line 4: syntax error near unexpected token '('
line 4: `done<< (find $1 -depth)`
You typed it wrong. What I said:

Code:
done < <(
What you wrote:

Code:
done<< (
Also, use code tags when posting code.
 
Old 07-15-2011, 10:18 AM   #22
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
It works well. Thanks you MTK!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to rename files recursively rs232 Linux - General 3 05-08-2011 02:05 PM
Rename recursively unihiekka Linux - Newbie 10 05-26-2010 01:55 AM
[SOLVED] how to rename files recursively, only keeping last x digits plus extension furryspider Programming 2 11-29-2009 12:55 PM
Unrar and Rename to Parent Folder Recursively Vasto Linux - Newbie 9 10-16-2008 06:36 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM


All times are GMT -5. The time now is 12:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration