bash:files:read_a_file:troubleshooting:files_lack_their_final_newlines
Differences
This shows you the differences between two versions of the page.
bash:files:read_a_file:troubleshooting:files_lack_their_final_newlines [2021/01/26 14:14] – created peter | bash:files:read_a_file:troubleshooting:files_lack_their_final_newlines [2021/01/26 14:17] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH - Files - Read a file - Troubleshooting - Files lack their final newlines ====== | ====== BASH - Files - Read a file - Troubleshooting - Files lack their final newlines ====== | ||
+ | |||
+ | ===== My text files are broken! | ||
+ | |||
+ | If there are some characters after the last line in the file (or to put it differently, | ||
+ | |||
+ | You can process this after the loop: | ||
+ | |||
+ | <code bash> | ||
+ | # Emulate cat. | ||
+ | while IFS= read -r line; do | ||
+ | printf ' | ||
+ | done < " | ||
+ | [[ -n $line ]] && printf %s " | ||
+ | </ | ||
+ | |||
+ | or: | ||
+ | |||
+ | <code bash> | ||
+ | # This does not work: | ||
+ | printf 'line 1\ntruncated line 2' | while read -r line; do echo $line; done | ||
+ | |||
+ | # This does not work either: | ||
+ | printf 'line 1\ntruncated line 2' | while read -r line; do echo " | ||
+ | |||
+ | # This works: | ||
+ | printf 'line 1\ntruncated line 2' | { while read -r line; do echo " | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | The first example, beyond missing the after-loop test, is also missing quotes. | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | Alternatively, | ||
+ | |||
+ | <code bash> | ||
+ | while IFS= read -r line || [[ -n $line ]]; do | ||
+ | printf ' | ||
+ | done < " | ||
+ | |||
+ | printf 'line 1\ntruncated line 2' | while read -r line || [[ -n $line ]]; do echo " | ||
+ | </ | ||
+ | |||
+ | ---- | ||
bash/files/read_a_file/troubleshooting/files_lack_their_final_newlines.1611670466.txt.gz · Last modified: 2021/01/26 14:14 by peter