LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: How to change a line in file? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-change-a-line-in-file-283207/)

gmitra 01-28-2005 04:43 AM

BASH: How to change a line in file?
 
Hello,
I need a simple script which changes one line in input file. Concretely, I want to replace the
title of eps file. The file before:
Code:

.
.
.
%%Title: Original name of the eps file
.
.
.

and after
Code:

.
.
.
%%Title: My new name of the eps file
.
.
.

has to be processed.
Thanks.

slakmagik 01-28-2005 05:03 AM

sed 's/Original name of the eps file/My new name of the eps file/'

But somehow I suspect you're actually asking for more than that.

gmitra 01-28-2005 05:47 AM

Yes, actually for more. If you know how to put to the sed for replacement
value of variable, then it could be done e.g.:
[code]
NEW_TITLE="This is my new title"
OLD_TITLE=$(grep "%%Title:" $FILE | cut -d: -f2)
sed 's/$OLD_TITLE/$NEW_TITLE/' $FILE > output.eps
[\code]
But it doesn't work! :(

slakmagik 01-28-2005 05:59 AM

Would double quotes and an address work?

OLD=foo
NEW=bar
sed "/%%Title/s/$OLD/$NEW/" $FILE > out

-- I mean, I gather this file has other lines in it you *don't* want to change? Still kind of confused about the objective and restrictions here. But that may do what you want.

jlliagre 01-28-2005 07:26 AM

I suspect the sed solution can't work, as it choke on binary contents or very long lines which are likely to appear on an eps file.


All times are GMT -5. The time now is 04:50 PM.