The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 11
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-28-2008
Registered User
 

Join Date: Aug 2007
Posts: 41
use escape sequence

sed "s\/${old_line}\/${new_line}\/" f

use escape sequence "\" and then try.
Reply With Quote
  #3  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 11
This results in the error:

sed: -e expression #1, char 81: unknown option to `s'
Reply With Quote
  #4  
Old 01-28-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
sed "s#${old_line}#${new_line}#" file1.txt
Reply With Quote
  #5  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 11
Much appreciated.
Reply With Quote
  #6  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
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?
Reply With Quote
  #7  
Old 01-28-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by KevinADC View Post
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?
not that I'm aware of......
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0