kralco626
kralco626

Reputation: 8624

linux appending files

I have a program that I generally run like this: a.out<guifpgm.txt>gui.html

Were a.out is my compiled c program, guifpgm.txt is the input file and the gui.html is the output file. But what I really want to do is take the output from a.out<guifpgm.txt and rather than just replacing whatever is in gui.html with the output, place the output in the middle of the file.

So something like this: gui.html contains the following to start: <some html>CODEGOESHERE<some more html> output to a.outalert("this is some dynamically generated stuff");

I want gui.html to contain the following: <some html>alert("this is some dynamically generated stuff");<some more html>

How can I do this?

Thanks!

Upvotes: 1

Views: 488

Answers (4)

kralco626
kralco626

Reputation: 8624

I ended up using the linux cat function. output a.out>guifpgm.txt>output.txt. Then did cat before.txt output.txt after.txt > final.txt

Upvotes: 2

jkerian
jkerian

Reputation: 17016

Just for the fun of it... the awk solution is as follows for program a.out, template file template that needs to replace the line "REPLACE ME". This puts the resulting output in output_file.txt.

awk '/^REPLACE ME$/{while("./a.out <input.txt"|getline){print $0}getline} {print $0}' template > output_file.txt 

EDIT: Minor correction to add input file, remove UUOC, and fix a minor bug (the last line of a.out was being printed twice)

Alternatively... perl:

perl -pe '$_=`./a.out <input.txt` if /REPLACE ME/' template > output_file.txt

Although dedicated perlers could probably do better

Upvotes: 0

jkerian
jkerian

Reputation: 17016

A simplification of your cat method would be to use

./a.out < guifpgm.txt | cat header.txt - footer.txt  > final.txt

The - is replaced with the input from STDIN. This cuts down somewhat on the intermediate files. Using > instead of >> overwrites the contents of final.txt, rather than appending.

Upvotes: 1

chrisaycock
chrisaycock

Reputation: 37930

Sounds like you want to replace text. For that, use sed, not C:

sed -i s/CODEGOESHERE/alert(\"this is some dynamically generated stuff\")/g gui.html

If you really need to run a.out to get its output, then do something like:

sed -i s/CODEGOESHERE/`a.out`/g gui.html

Upvotes: 3

Related Questions