The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:08 AM
Count files lines in a directory? davidoff Shell Programming and Scripting 13 04-08-2008 02:04 AM
How to delete first 5 lines and last five lines in all text files ragavendran31 Shell Programming and Scripting 10 02-21-2008 07:58 AM
Replacing lines in text files Jonny2Vests Shell Programming and Scripting 7 01-28-2008 08:29 PM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 01:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-25-2008
rooster005 rooster005 is offline
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 07:20 AM.. Reason: added code tags
  #2 (permalink)  
Old 03-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0