replacing new lines in all files of a directory containing old lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing new lines in all files of a directory containing old lines
# 1  
Old 03-25-2008
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 08:20 AM.. Reason: added code tags
# 2  
Old 03-25-2008
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. Shell Programming and Scripting

Replacing specific lines with another lines

Hi, I have a file with many lines, then i have following list of lines(line number 5,12,19,5,and 28) i need to replace these lines of a file with another lines as shown below these text contains special charecter like= (/:;){} Line_number Text to replace with 5 abc... (1 Reply)
Discussion started by: MILAN KUMAR
1 Replies

6. Shell Programming and Scripting

Replacing certain lines with another set of lines

i have a huge script that i need to replace a bunch of lines in. in that script, i have a chunk similar to: JABABA=$(if ; then echo afafafa afafafafaf afafafafe fdfdfdfadfadl fafafafkkkk fi) I would like to replace the above chunk with: (1 Reply)
Discussion started by: SkySmart
1 Replies

7. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

8. Shell Programming and Scripting

Replacing lines between two files with awk

Hello Masters, I have two subtitles file with different language like below First file : 1 00:00:41,136 --> 00:00:43,900 2 00:00:55,383 --> 00:00:58,477 <i> Ladies and gentlemen,</i> <i>this is Simon Barsinister,</i> 3 00:00:58,553 --> 00:01:00,521 <i>the wickedest man in the... (8 Replies)
Discussion started by: rk4k
8 Replies

9. Shell Programming and Scripting

Count files lines in a directory?

Hy! I have some problem. Problem is that i don't now how to solve problem of average lines of files in a directory. I have managed to get number of files in a directory, but i don't know the command to count average lines of these files. I have one "for" loop that goes true whole... (13 Replies)
Discussion started by: davidoff
13 Replies

10. Shell Programming and Scripting

Replacing lines in text files

Hi, I have 2 sets of text files. I need to take a field from a certain line in set 1 and put it in the same place in set b. The line appears once per file, in different places but is a set format and has the unique word "ANTENNA" in it and is always 81 characters long. Example from set a: ... (7 Replies)
Discussion started by: Jonny2Vests
7 Replies
Login or Register to Ask a Question