Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now