Solved

How do I search and replace text in a batch of files?

Posted on 2006-10-26
3
403 Views
Last Modified: 2010-04-21
In a folder I have about 150 files. I want to search for all instances of header.txt and replace it with header_lu.txt. Is this possible thru the a command? Thanks in advance for all the help.
0
Comment
Question by:bemara57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 500 total points
ID: 17812249
Like anything in Unix, there are many ways to skin this cat.

Here is one that might work for you:

tmpfile="$(mktemp -c -p "replacement")"
<find or ls to get list of files on stdout> | while read file
do
    awk '{
        gsub(/header[.]txt/, "header_lu.txt", $0)
        print $0
        }' < "$file" > "$tmpfile"
    cat "$tmpfile" > "$file"
done
rm -f "$tmpfile"
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17812787
Hi bemara57,

find /path/to/folder | while read fname
do
      sed 's/header\.txt/header_lu\.txt/g' $fname > $fname.new
      mv -f $fname.new $fname
done

Cheers!
sunnycoder
0
 
LVL 84

Expert Comment

by:ozo
ID: 17813076
perl -i -pe 's/header\.txt/ header_lu.txt/g' folder/*
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question