The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deleting specific lines from all files in a directory vrms UNIX for Dummies Questions & Answers 3 04-25-2008 08:08 AM
Count files lines in a directory? davidoff Shell Programming and Scripting 13 04-07-2008 11:04 PM
How to delete first 5 lines and last five lines in all text files ragavendran31 Shell Programming and Scripting 10 02-21-2008 03:58 AM
Replacing lines in text files Jonny2Vests Shell Programming and Scripting 7 01-28-2008 04:29 PM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-24-2007 10:21 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 1
replacing new lines in all files of a directory containing old lines

Hi all,


I am trying to replace a few lines with other lines of all files in a directory which contain those few lines.

say - there are some 10 files in a dir having the same 4 lines as 1.txt at the starting
1.txt
line 1
line 2
line 3
line 4
....................................
....................................
....................................


i would like to replace all the 10 files with these lines in place of the pervious four lines.

this is one
this is two
this is three
..........................................
.........................................
........................................


i was able to change upto a line ie one line with no new line character ,i could not do it for multiple lines.

here is the code for one line replacement

Code:
#!/bin/sh

echo "Enter the string to be changed :"
read stringToBe
clear


echo "Enter the string to be replaced"
read repWord
clear


for file in $( grep -il $stringToBe   * )
do
sed -e "s/$stringToBe/$repWord/" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done

could the forum help me in replacing old multiple lines with new multiple lines.

Last edited by Yogesh Sawant; 03-25-2008 at 03:20 AM. Reason: added code tags
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Traditional Unix text handling tools are generally line oriented, and don't work on contexts of multiple lines. Your best bet is probably to write a small awk or perl script. How modular does it need to be? Do you expect to need to do this often? On many files? Always at the beginning of file? How large are the files?

Or ... if you grab one file and can generalize from that, maybe you could do some in-line editing of the output from diff and reapply it to each of the other files in turn. Do you have the patch command where you are attempting this?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0