![]() |
|
|
|
|
|||||||
| 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 lines from multiple text files | vrms | Shell Programming and Scripting | 3 | 04-25-2008 08:01 AM |
| replacing new lines in all files of a directory containing old lines | rooster005 | Shell Programming and Scripting | 1 | 03-25-2008 12:38 PM |
| How to delete first 5 lines and last five lines in all text files | ragavendran31 | Shell Programming and Scripting | 10 | 02-21-2008 04:58 AM |
| Replacing text | chrchcol | Shell Programming and Scripting | 3 | 07-25-2006 09:30 AM |
| replacing text | ajaya | Shell Programming and Scripting | 2 | 04-12-2006 09:31 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: " 0.0000 0.0000 0.0000 ANTENNA: DELTA H/E/N" Example from set b " 0.2160 0.0000 0.0000 ANTENNA: DELTA H/E/N" The bold bit is the field I'm trying to change and will be a random float in both sets. I've been trying to do it like this: #!/bin/ksh old_line=`grep ANTENNA ./file1.txt` new_line=`grep ANTENNA ./file2.txt` sed "s/${old_line}/${new_line}/" file1.txt >tmp2 #substitute old line with new line in file 1 and output to tmp2 But this doesn't work, probably because I have forward slashes in my grep lines which sed interprets as some sort of regexp. How can I escape these when they are embedded in a variable? Am I quoting incorectly? Or is there a much better way? Jon |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
use escape sequence
sed "s\/${old_line}\/${new_line}\/" f
use escape sequence "\" and then try. |
|
#3
|
|||
|
|||
|
This results in the error:
sed: -e expression #1, char 81: unknown option to `s' |
|
#4
|
||||
|
||||
|
Code:
sed "s#${old_line}#${new_line}#" file1.txt
|
|
#5
|
|||
|
|||
|
Much appreciated.
|
|
#6
|
|||
|
|||
|
But if you have # in the lines you will get the same problem. Does sed not have a quotemeta function/option you can use for more general purposes and not have to worry about the specific delimiter in the search expression?
|
|
#7
|
||||
|
||||
|
not that I'm aware of......
|
||||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|