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
  Search this Thread
Old 09-14-2006, 12:59 AM   #1
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Rep: Reputation: 21
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

Last edited by nadavvin; 09-14-2006 at 01:02 AM.
 
Old 09-14-2006, 01:24 AM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
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.
 
Old 09-14-2006, 01:45 AM   #3
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
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?
 
Old 09-14-2006, 01:48 AM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

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

Could you tell me what output you expected?
 
Old 09-14-2006, 02:00 AM   #5
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
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

Last edited by nadavvin; 09-14-2006 at 02:01 AM.
 
Old 09-14-2006, 02:03 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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?
 
Old 09-14-2006, 02:18 AM   #7
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
I need that it replace the files itself.

Why doesn't it replace?
 
Old 09-14-2006, 07:12 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
how about sed?

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

worked for me
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 10:50 PM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM
replace text in files and directories rincewind Linux - Software 4 10-27-2004 11:29 AM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration