![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | 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 07:20 AM.. Reason: added code tags |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|