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
Replacing the last data of each line ina file jisha Shell Programming and Scripting 6 08-04-2008 08:47 AM
Replacing end of line with " in a UNIX file The Observer Shell Programming and Scripting 2 05-17-2008 06:20 AM
replacing a line of unknown charecters in a file malavm Shell Programming and Scripting 12 07-26-2007 05:25 AM
Replacing characters in file with line break johnemb Shell Programming and Scripting 10 04-26-2007 08:38 AM
replacing first line or lines in a file Terrible UNIX for Advanced & Expert Users 3 06-28-2006 09:23 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 06-05-2008
maxmave maxmave is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 21
Replacing a line in a file - HELP!!!

I have a problem in the following code ...


Code:
while read line
do
   #Get Line Number 
   OLDLINE=`sed -n $Lineno $filename`
   echo "Un Changed Line : "$OLDLINE
   echo "Enter a New Pattern : "
   read NewPattern <&1
   echo "NewPattern :"$NewPattern 
   NEWLINE=`cat $filename | sed -n $Lineno | sed s/$OldPattern/$NewPattern/`
   echo $NEWLINE
   LINECHANGE=`sed s/$OLDLINE/$NEWLINE/ $filename`
   echo $LINECHANGE
   ChangedLine=`sed -n $Lineno $filename`
   echo "Changed Line : "$ChangedLine
done < $INT_FILE

I am trying to read a line from a file and get the line and change a pattern in the line and i want to replace the
OLD line with the NEW line.

I have problem in the following line

Code:
LINECHANGE=`sed s/$OLDLINE/$NEWLINE/ $filename`

the OLD Line is not changed in NEW Line in the file.

is there any problem with the command i gave or should i need to correct it

Can anyone please help.


Thanks

Rahul

Last edited by Yogesh Sawant; 06-05-2008 at 02:06 AM.. Reason: added code tags
  #2 (permalink)  
Old 06-05-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 167
See this:


Code:
$ cat ea.txt
root 1933 1923 localhost calm 32rhewr

$ A=root

$ B=ROOT

$ sed 's/$A/$B/g' ea.txt
root 1933 1923 localhost calm 32rhewr

**No change above

So use double quotes instead of single quote: 

$ sed "s/$A/$B/g" ea.txt
ROOT 1933 1923 localhost calm 32rhewr

So 

LINECHANGE=`sed s/$OLDLINE/$NEWLINE/ $filename`

should be something like this:

LINECHANGE=`sed "s/$OLDLINE/$NEWLINE/" $filename`

//Jadu
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 10:01 AM.


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