Advertisement

06.21.2000 at 04:39PM PDT, ID: 10515858
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Replace a string in several text files
Tags: replace, linux, text, string, files
I want to replace a string with another string in several text files. I tried the following command which I read from a Linux book, but it doesn't work. Can anyone give me some help?

[Linux]#find ./ -type f -exec sed 's/string1/string2' {} \;
Start your free trial to view this solution
Question Stats
Zone: OS
Question Asked By: dong
Solution Provided By: markt9
Participating Experts: 5
Solution Grade: B
Views: 783
Translate:
Loading Advertisement...
06.21.2000 at 09:04PM PDT, ID: 3037295

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.21.2000 at 09:17PM PDT, ID: 3037395

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.21.2000 at 10:02PM PDT, ID: 3037812

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2003 at 06:01AM PDT, ID: 8340561

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.29.2003 at 09:49AM PST, ID: 9643890

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.22.2004 at 05:33PM PDT, ID: 11135770

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.23.2004 at 06:36AM PDT, ID: 11137542

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.26.2005 at 09:01PM PDT, ID: 14533558

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
06.21.2000 at 09:04PM PDT, ID: 3037295
Wow, I'm supprised how hard this one is to figure out.  Here is an alternate method:

cd myfiles
for fl in *.txt; do
mv $fl $fl.old
sed 's/January/February/g' $fl.old > $fl
rm -f $fl.old
done

The first line of the sequence simply goes to the directory containing the files which need to be changed. The second line above begins a shell for loop which will execute the following commands once for each *.txt file in the directory. The third line adds an '.old' extension to all of the existing files which need to be changed, and the fourth line performs the search-and-replace on the file in question. The sixth line closes the loop and begins execution of the command.

I couldn't figure out a one liner to do it... I kept getting sed: -e expression #1, char 17: Unterminated `s' command errors....

Pay attention to your book's example, ', `, " all have different meanings in unix...
Accepted Solution
 
06.21.2000 at 09:17PM PDT, ID: 3037395
or maybe:


   To do an entire directory tree, use the Unix utility find, like so
   (thanks to Jim Dennis <jadestar@rahul.net> for this script):

      #! /bin/sh
      # filename: replaceall
      find . -type f -name '*.txt' -print | while read i
      do
         sed 's|foo|bar|g' $i > $i.tmp && mv $i.tmp $i
      done

   This previous shell script recurses through the directory tree,
   finding only files in the directory (not symbolic links, which will
   be encountered by the shell command "for file in *.txt", above). To
   preserve file permissions and make backup copies, use the 2-line cp
   routine of the earlier script instead of "sed ... && mv ...". By
   replacing the sed command 's|foo|bar|g' with something like

      sed "s|$1|$2|g" ${i}.bak > $i

   using double quotes instead of single quotes, the user can also
   employ positional parameters on the shell script command tail, thus
   reusing the script from time to time. For example,

      replaceall East West

   would modify all your *.txt files in the current directory.
 
06.21.2000 at 10:02PM PDT, ID: 3037812
Comment accepted as answer
 
04.16.2003 at 06:01AM PDT, ID: 8340561
i know this q is old, but here's and easy way to do this in one line(if you have perl installed):


perl -pi -e "s/search/replace/g;" *.txt


you could use find or xargs to get fancy with finding the files you want to change.
 
10.29.2003 at 09:49AM PST, ID: 9643890
Can I use a variable as the string instead?
ie.
sed 's/string1/$string2/g' $fl.old > $fl
where $string2 is a variable,

I find it doesn't work
 
05.22.2004 at 05:33PM PDT, ID: 11135770
The sed substitution has a syntax error. It is missing the final '/' and should be written as:
find ./ -type f -exec sed 's/string1/string2/' {} \;

I don't have my Linux box up right now but this works fine in OpenBSD and cygwin.

It will only change the first occurance of string1 on each line so you might want to add 'g' to the end of the substitution command to get all occurances:
find ./ -type f -exec sed 's/string1/string2/g' {} \;

And you might not want to sed every file but just some that have certain names or extensions:
find ./ -name "*.txt" -exec sed 's/string1/string2/g' {} \;
 
05.23.2004 at 06:36AM PDT, ID: 11137542
Oops, I forgot to include the -i so sed edits in-place:
find ./ -type f -exec sed -i 's/string1/string2/' {} \;
 
07.26.2005 at 09:01PM PDT, ID: 14533558
In response to :

"Comment from Fzzy
Date: 04/16/2003 08:01AM CDT
you could use find or xargs to get fancy with finding the files you want to change."

I've found this to work well as a one liner, recursive, search and replace for Linux.

find ./ -type f | xargs perl -pi -w -e 's/SEARCH/REPLACE/g;'

It is handy for mass updates to websites.  Be careful and use your grep -r 'SEARCH' *.* first to make sure you are only grabbing the files you want.

-Mike Putnam
 
 
20080236-EE-VQP-29