Solved

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

Posted on 2006-10-26
3
400 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

770 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