LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-24-2002, 09:11 AM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
one liner needed for search and replace


Hi guys,

I need a quick oneliner - either perl or bash to search all files in the current directory for the expression "fred_flintstone" and replace it with "barny_rubble". I wrote a perl script to do this a few weeks ago, but it's at home on my laptop.. and I'm up to /here/ right now at work !

any help would be much appreciated.

thanks
christo

so far I have got to
$ find -type f | xargs /bin/sed s/fred_flintstone/barny_rubble/

but I can't work out how to redirec the output back into the file found!
 
Old 09-24-2002, 09:30 AM   #2
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
$perl -pi -e 's/fred_flintstone/barney_rubble/' *

seems to do the trick!

Christo
 
Old 09-24-2002, 10:00 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,003
Blog Entries: 54

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
If you like using Bash, then you better resort to a quick function or script. Sed is a stream editor, so you'll have to feed it input and get the output. Else there's a small tool called "rpl" (see freshmeat) I've been using for years to replace text, works like a charm.

I extended the script a wee bit because if it is a file (find) but doesn't include the term (grep -q) then we need not (cat) it.

#!/bin/sh
x="fred_flintstone"; y="barny_rubble"; find -type f | while read z; do grep "$z" -qie "$x"; case "$?" in 0) cat "$z" | sed -e "s/$x/$y/g" > "$z".tmp; mv -f "$z".tmp "$z";; esac; done
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 02:39 AM
Emacs search and replace neil Linux - Software 1 01-28-2005 06:04 AM
search and replace C code vexer Programming 3 05-18-2004 11:11 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


All times are GMT -5. The time now is 02:15 PM.

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