LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I replace text with perl with a list fo files? (https://www.linuxquestions.org/questions/programming-9/how-do-i-replace-text-with-perl-with-a-list-fo-files-483336/)

nadavvin 09-14-2006 12:59 AM

How do I replace text with perl with a list fo files?
 
Hello

I want to replace "root" to "tux" in all the files in some directory.

I get them by using tree:
Code:

$ tree -af --noreport -i
.
./a
./b
./c
./u
./u/m
./u/m/root
./u/s
./u/t
./u/v

How do I pass it to perl -pi -e "s/root/tux/g;"?

How do I list only files and not directories?
There is -d for only directories

zhangmaike 09-14-2006 01:24 AM

Quote:

How do I pass it to perl -pi -e "s/root/tux/g;"?
Use a pipe:
Code:

$ tree -af --noreport -i | perl -pi -e "s/root/tux/g;"
Quote:

How do I list only files and not directories?
I don't know how to do this with tree (or if it's possible), but you can do it with find.

This will find all files that aren't directories (including symbolic links, fifos, device files, etc):
Code:

$ find -not -type d
This will find only normal files (files which aren't directories, symbolic links, fifos, device files, etc):
Code:

$ find -type f
See the find manpage. You may also want to read up about pipes online or in your shell's manpage.

nadavvin 09-14-2006 01:45 AM

thanks, but it's not work with perl:

Code:

$ find -type f|perl -pi -e "s/root/tux/g;"
./a
./b
./c
./u/t
./u/v
./u/m/tux
./.s

What is the problem?

zhangmaike 09-14-2006 01:48 AM

How is what you just posted not working? It looks like exactly what you asked for.

Could you tell me what output you expected?

nadavvin 09-14-2006 02:00 AM

It's not replace the content between root to tux.

Code:

$ cat a
root
nadav@nadav-desktop:~/tmp/b$ perl -pi -e "s/root/tux/g;" a
nadav@nadav-desktop:~/tmp/b$ cat a
tux
nadav@nadav-desktop:~/tmp/b$ echo "root" >a
nadav@nadav-desktop:~/tmp/b$ cat a
root
nadav@nadav-desktop:~/tmp/b$ find -type f|perl -pi -e "s/root/tux/g;"
./a
./.s
nadav@nadav-desktop:~/tmp/b$ cat a
root


ghostdog74 09-14-2006 02:03 AM

Quote:

Originally Posted by nadavvin
thanks, but it's not work with perl:

Code:

$ find -type f|perl -pi -e "s/root/tux/g;"
./a
./b
./c
./u/t
./u/v
./u/m/tux
./.s

What is the problem?

"root" is changed to "tux", but it does not change the actual file name yet, or are you after something else?

nadavvin 09-14-2006 02:18 AM

I need that it replace the files itself.

Why doesn't it replace?

chrism01 09-14-2006 07:12 PM

how about sed?

find -type f -exec sed -i -e 's/tux/root/' {} \;

worked for me


All times are GMT -5. The time now is 11:10 AM.