![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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? |
|||
| Google The UNIX and Linux Forums |