LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 12-25-2007, 03:29 AM   #1
TL_CLD
Member
 
Registered: Sep 2006
Posts: 366

Rep: Reputation: 45
Search and replace


Hey all,

I need to do a search and replace on a bunch of files located in different folders. So far I've come up with this solution:

Code:
find /path/ -name FILE -exec sed -i 's/oldphrase/newphrase/' {} \;
But I'm wondering if there's a better way?

Regards,
Thomas
 
Old 12-25-2007, 04:05 AM   #2
samwise17
Member
 
Registered: Jul 2007
Location: Sydney
Distribution: Arch,Slackware,Puppy
Posts: 87

Rep: Reputation: 15
Looks ok to me. But the only real way to be confident it works is to test it. You could use the File::Find module in Perl (I'm sure there's more than one way to do it with Perl). Depends on whether you're only doing it this once, or if you want to write a generally applicable, robust and reusable script.

What are the brackets {} for? I don't think you need it for sed. Some obscure 'find' syntax? I hate the way you have to add the escaped ';' for an -exec.
 
Old 12-25-2007, 04:06 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
does it do what you want? if you have many files to search and replace, you can use xargs instead of -exec in your find command.
 
Old 12-25-2007, 04:09 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by samwise17 View Post

What are the brackets {} for? I don't think you need it for sed. Some obscure 'find' syntax? I hate the way you have to add the escaped ';' for an -exec.
"{}" signifies "arguments" passed to -exec command.
 
Old 12-25-2007, 01:45 PM   #5
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Rep: Reputation: 50
2 Perl *so called help* (my cheat sheet(s) if you please) perl scripts. 1 is named clplh (command line perl help) the other is named findh

Since these are made for me to run them from the commandline (and they merely print to STDOUT) it *is* possible that some item(s) are escaped in these scripts so that when they are run that they print correctly (in konsole, I just copy/paste so as to end up with the specific command that I want.

Scripts (not konsole or console output) follow next:

al@AB60R:~/bin$ pwd
/home/al/bin
al@AB60R:~/bin$ cat clplh
#!/usr/bin/perl

print <<STUFF;
# tr/\xA0/ /
perl -pi.bak -e 'tr/\\xA0/ /' filenames*

# all files in a folder
find YourDirectory -type f -print0 | xargs -0 sed -i 's/xxxx/yyyyy/g'

find . | xargs perl -p -i.bak -e 's/oldstring/newstring/g'

perl -e 's/string/stringier/gi' -p -i.bak *.html

via commandline, runs that command, replaces string with
stringier using g global and i insensitive options.
Operates on all .html files in a folder (CWD) also makes
a backup of each file prior to the operation.

STUFF
al@AB60R:~/bin$
al@AB60R:~/bin$
al@AB60R:~/bin$ cat findh
#!/usr/bin/perl
# findh prints example find useage
print "\nfilename accepts wildcards || Or try: locate filename\ncmd | top dir (recurses down) | opt -n denotes-> | name_of_file2find\n\n";
print "find /home -name filename\n--\n";

print <<STUFF;
find / -perm +04000 -exec ls -ld {} \\; 2> /dev/null > /tmp/SUID.files
find / -perm +02000 -exec ls -ld {} \\; 2> /dev/null > /tmp/SGID.files

# files larger than 2 megs. size.txt ordered list
find /home/al/ -xdev -size +2000k -print | xargs ls -ldS > ~/size.txt

# caution searches from / (all of al files on entire disk)
find / -xdev -user al -print | xargs ls -ldS > /home/al/alfil.txt

# change group
find /bin /sbin /usr/bin /usr/sbin -type f -group bin | xargs chgrp root

# all files under yum_repo that are less than 26 days old
find /home/al/yum_repo -mtime -26

# quote when pattern used in name portion
find /mnt/usbhd/rhel/yum_repo -name '*'

find /usr -name foobar.txt

find / -name foobar.txt # srch from root of HD

find / -iname '*.MP3' # case insensitive

find -amin -60 # files accessed less than 60 minutes ago

find / -xdev -ctime -1 # finds new(ly) (software installed) files

perl -MFile::Find -le 'find sub{print if /\\.txt\$/}, "."'

my \@files = `find $dir -name '*.txt'`;

# files only also in cur dir only
find . -name '*' -maxdepth 1 -type f > ~/myfiles.txt
find . -name '*.htm' -maxdepth 1 -type f > ~/myfiles.txt

# cp files to /tmp/slackupdate (ok prompts, exec just does it)
find . -type f -name "*.txt" -maxdepth 1 -ok cp {} /tmp/slackupdate \\;
find . -type f -name "*.txt" -maxdepth 1 -exec cp {} /tmp/slackupdate \\;

locate command. To upgrade the database for the locate command do this while in the superuser mode: slocate -u. Then wait a while while it upgrades. To use the command just type locate mozilla (for example) and it will post all of the mozilla references in your computer.

slocate -u
STUFF
al@AB60R:~/bin$

--
Alan.
 
  


Reply



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
search & replace raj_sony2001 Linux - General 4 10-05-2006 02:05 PM
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 10:37 PM.

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