LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2008, 02:46 AM   #1
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Rep: Reputation: 16
Question Replacing line in multiple file at a time


Hi Guys,

Consider the following scenario.

I have a folder by the name clients. In this folder i have around 100 files. In each file there is line which has following phrase as common.

Workdir:/var/www/html/

Example in file for UBL customer this line is

Workdir:/var/www/html/ubl

For customer HMC this line is

Workdir:/var/www/html/hmc


Now i need to replace only the Workdir:/var/www/html to Workdir:/var/www/html/london in all the files of this folder.

So that for UBL customer it becomes

Workdir:/var/www/html/london/ubl

Any clue how can i do it with script.

Regards
 
Old 02-14-2008, 04:55 AM   #2
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Original Poster
Rep: Reputation: 16
Guys i have done it with following script.

First i get list of all files in a file via


#ls > lisofallfile
Then i run this script


#/bin/sh

while read line; do
/bin/sed 's/\/var\/www\/html/\/var\/www\/html\/khi/g' < $line > tempfile
cat tempfile > $line
done < listofallfiles
 
Old 02-14-2008, 04:57 AM   #3
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post With sed

Use sed s/oldpattern/newpattern
 
Old 02-14-2008, 10:22 AM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by talat View Post
#ls > lisofallfile
Then i run this script

#/bin/sh

while read line; do
/bin/sed 's/\/var\/www\/html/\/var\/www\/html\/khi/g' < $line > tempfile
cat tempfile > $line
done < listofallfiles
Code:
sed -i 's/pattern/replacement/g' *
this will modify the files in place. If you're just doing an ls, then '*' should work fine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script for displaying a file one line at a time. panchosansa Programming 6 10-12-2006 10:54 AM
replacing a file in a zip from command line davee Linux - General 3 02-28-2006 01:06 PM
reading a line at a time in a file topcat Programming 2 12-14-2004 05:49 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
perl: replacing a special line in a file markus1982 Programming 1 09-26-2002 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration