Speed Video to Audio Converter 1.1.1 and activation code

≡ Menu
by Vivek Gite on November 27, 2007 last updated March 9, 2011

in BASH Shell, Linux, Perl, UNIX

How do I replace a string with another string in all files? For example, /foo directory has 100s of text file and I’d like to find out xyz string and replace with abc. I’d like to use sed or any other tool to replace all occurrence of the word.

The sed command is designed for this kind of work i.e. find and replace strings or words from a text file under Apple OX, BSD, Linux, and UNIX like operating systems. The perl can be also used as described below.

sed replace word / string syntax

The syntax is as follows:
sed -i 's/old-word/new-word/g' .txt

GNU sed command can edit files in place (makes backup if extension supplied) using the -i option. If you are using an old UNIX sed command version try the following syntax:

sed 's/old/new/g' input.txt > output.txt

You can use old sed syntax along with bash for loop:

#!/bin/bash OLD="xyz" NEW="abc" DPATH="/home/you/foo/.txt" BPATH="/home/you/bakup/foo" TFILE="/tmp/out.tmp.$$" [ ! -d $BPATH ] && mkdir -p $BPATH || : for f in $DPATH do if [ -f $f -a -r $f ]; then /bin/cp -f $f $BPATH sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f" else echo "Error: Cannot read $f" fi done /bin/rm $TFILE

A Note About Bash Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path “/nfs/apache/logs/rawlogs/access.log” with “__DOMAIN_LOG_FILE__”:

#!/bin/bash ## Our path _r1="/nfs/apache/logs/rawlogs/access.log" ## Escape path for sed using bash find and replace _r1="${_r1//\//\/}" # replace __DOMAIN_LOG_FILE__ in our sample.awstats.conf sed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf > /nfs/apache/logs/awstats/awstats.conf # call awstats /usr/bin/awstats -c /nfs/apache/logs/awstats/awstats.conf

The $_r1 is escaped using bash find and replace parameter substitution syntax to replace each occurrence of / with \/.

perl -pie Syntax For Find and Replace

The syntax is as follows:
perl -pie 's/old-word/new-word/g' input.file > new.output.file

Share this tutorial on:

TwitterFacebookGoogle+Download PDF version Found an error/typo on this page?

About the author: Vivek Gite is a seasoned sysadmin and a trainer for the Linux/Unix & shell scripting. Follow him on Twitter. OR read more like this:
  • Vi / VIM: Find And Replace All Text Substitute Command
  • UNIX / Linux: vi / vim perform search and replace operation
  • Linux / UNIX: Sed Replace Newline
  • Bash Continue Command / Script On The Next Line
  • vi Find And Replace Text Command
  • Bash Script Replace Empty Spaces String
  • Awk Find And Replace Fields Values
  • sed Case Insensitive Search Matching
  • HowTo: Merge Apache / Lighttpd / Nginx Server Log Files
  • sed Find and Replace ASCII Control Codes / Nonprintable Characters

{ 63 comments… add one }

Leave a Comment




   Tagged with: apple, BASH Shell, fi, gnu, gnu sed, input file, Linux, mkdir, mkdir command, mv, mv command, Perl, perl -pie, perl command, perl replace words, replace word string, sed command, sed replace words, shell script replace strings, shell script replace words, tmp, UNIX

Next FAQ: How to: create a temporary file securely

Previous FAQ: Understanding Bash fork() Bomb :(){ :|:& };:

Featured Articles:

  • 30 Cool Open Source Software I Discovered in 2013
  • 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
  • Top 30 Nmap Command Examples For Sys/Network Admins
  • 25 PHP Security Best Practices For Sys Admins
  • 20 Linux System Monitoring Tools Every SysAdmin Should Know
  • 20 Linux Server Hardening Security Tips
  • Linux: 20 Iptables Examples For New SysAdmins
  • Top 20 OpenSSH Server Best Security Practices
  • Top 20 Nginx WebServer Best Security Practices
  • 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
  • 15 Greatest Open Source Terminal Applications Of 2012
  • My 10 UNIX Command Line Mistakes
  • Top 10 Open Source Web-Based Project Management Software
  • Top 5 Email Client For Linux, Mac OS X, and Windows Users
  • The Novice Guide To Buying A Linux Laptop

Don’t Miss Any Linux Tips

©2000-2016 nixCraft. All rights reserved. Privacy - Terms of Service - Questions or Comments