Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a line in a file - HELP!!! 1

Status
Not open for further replies.

maxmave

Programmer
Jun 3, 2008
5
US
I have a problem in the following 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

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
 
Hi

Sorry Rahul, I do not understand what you want. Neither from your messy code ( next time put your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] tags ), neither your few words ( next time show us some sample input and desired output ).

In meantime some general advices :
[ul]
[li]avoid UUOC[/li]
[li]avoid pointless piping[/li]
[li]enclose [tt]sed[/tt]'s code with quotes[/li]
[li]indent your code[/li]
[/ul]

Feherke.
 
sed doesn't generally change the file you give it as input. You normally put the output of the sed command into a temporary file, then copy that file over the original. If you're using GNU sed, the [tt]-i[/tt] option is more convenient; it will modify the input file in place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top