Truncate a file: Difference between revisions
Content added Content deleted
Line 715: | Line 715: | ||
# 4. trunc(): trunc file size # truncate at length |
# 4. trunc(): trunc file size # truncate at length |
||
-bash-4.2$ trunc() { read -N $2 b<"$1"; echo -n "$b">"$1"; } |
-bash-4.2$ trunc() { IFS= read -N $2 b<"$1"; echo -n "$b">"$1"; } |
||
-bash-4.2$ echo "abcdef">bad; ls -l bad; cat bad; trunc bad 3; ls -l bad; cat bad; echo "_EOF_" |
-bash-4.2$ echo "abcdef">bad; ls -l bad; cat bad; trunc bad 3; ls -l bad; cat bad; echo "_EOF_" |
||
+ echo abcdef |
+ echo abcdef |
||
Line 731: | Line 731: | ||
_EOF_ |
_EOF_ |
||
</lang> |
</lang> |
||
# NOTE: Designed for regular text files. Does not work when files contain: chars 0x00 and 0xFF as of bash 4.2 - these chars are treated differently in read -N. For this reason use of method#4 is not prefer over dd(1) or truncate(1) when using binary files, or large files. |
|||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |