Truncate a file: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 781: | Line 781: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|jq}}== |
|||
jq cannot itself perform the specified task |
|||
because it cannot "truncate a file" in the sense of the question. |
|||
In particular, it is not "binary safe", and cannot itself write to files named on the command line. |
|||
Another issue is that jq can only read UTF-8-encoded files, and generally speaking is oriented |
|||
to codepoints rather than bytes. |
|||
So for this task, we'll assume that `sponge` is available for overwriting a file, |
|||
and that the "size" is specified in codepoints rather than bytes. |
|||
<pre> |
|||
< input.txt jq -Rr --argjson size $size ' |
|||
if length < $size then "file size is less than the specified size" | error |
|||
else .[:$size] |
|||
end |
|||
' | sponge input.txt |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |