LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inserting text into a file (https://www.linuxquestions.org/questions/programming-9/inserting-text-into-a-file-82328/)

DavidPhillips 08-15-2003 02:18 PM

inserting text into a file
 
Howdy,

This is what I am looking at

sed /$expression/a\ "$comment" $fname > $fname.cmt

no problem with it except that i want to edit the file $fname itself

the problem is $fname is being written with

cat /dev/ttyUSB0 >> $fname

I would like to be able to actually insert the comment into $fname without causing a problem with the file as it is being written.

sed /$expression/a\ "$comment" $fname > $fname

results in an empty file


may I need to use echo or something

thanks,

david_ross 08-15-2003 02:28 PM

Re: inserting text into a file
 
I think I have doen something similar before and I had to use echo - eg:
echo `sed /$expression/a\ "$comment" $fname` > $fname

DavidPhillips 08-15-2003 03:21 PM

It seems to work, except that the line is inserted without a new line at the end

~090 File Number:9030, Shot Number:2977, Shot Point Line:562 , Station:123
Observers comment: Hello ~090 Mag Tape Usage: MT2U0-raw

I have not checked to see what is happening to the cat process yet.

david_ross 08-15-2003 03:39 PM

What am I thinking - when I was doing something similar it was with a different script (perl search and replace type script). With sed you can just use the -i switch.
sed -i /$expression/a\ "$comment" $fname

DavidPhillips 08-15-2003 04:45 PM

that's it

hard to believe it was that simple

learning sed is fun
:)

david_ross 08-15-2003 04:53 PM

Yeah - the funy thing was the first time I really used it was for modifying shadow password files - just as well I made backups first :D


All times are GMT -5. The time now is 08:25 PM.