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 and shell scripting languages 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 12:01 PM
replacing new lines in all files of a directory containing old lines rooster005 Shell Programming and Scripting 1 03-25-2008 03: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 07:58 AM
Replacing text chrchcol Shell Programming and Scripting 3 07-25-2006 01:30 PM
replacing text ajaya Shell Programming and Scripting 2 04-12-2006 01:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2008
Jonny2Vests Jonny2Vests is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
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
  #2 (permalink)  
Old 01-28-2008
raman1605 raman1605 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 41
use escape sequence

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

use escape sequence "\" and then try.
  #3 (permalink)  
Old 01-28-2008
Jonny2Vests Jonny2Vests is offline
Registered User
  
 

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

sed: -e expression #1, char 81: unknown option to `s'
  #4 (permalink)  
Old 01-28-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
sed "s#${old_line}#${new_line}#" file1.txt
  #5 (permalink)  
Old 01-28-2008
Jonny2Vests Jonny2Vests is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
Much appreciated.
  #6 (permalink)  
Old 01-28-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0