I'm writing a little shell script to generate a directory listing. To make the output easily customizable, the script just builds a HTML-table and than should replace a specific token in a template file with that table and write it to stdout
.
Example:
Template file:
<!doctype html>
<html>
<head><title>Directory Listing</title></head>
<body>
{{LISTING}}
</body>
</html>
{{LISTING}}
should be replaced with the listing (which is stored in a shell variable).
The problem here is, that a simple sed 's/{{LISTING}}/$listing/'
would be limited to the maximum argument length which doesn't work with long listings.
How can I replace a pattern in a file with a long string using POSIX utilities?
EDIT:
To add more clarification: My current solution is like that:
awk '{ gsub(A, B); print; }' A="{{LISTING}}" B="$listing" < $template
$listing
gets expanded as awk is invoked, but this can exceed argument length limits, since $listing
can be really long. For example, when generating a listing of /usr/lib
, I get an error:
/bin/awk: Argument list too long
I'm thinking whether there is a solution to replace a pattern in a textfile with stdin
.