Alternative to strings
Opened this issue · 4 comments
Deleted user commented
strings() {
# Usage: strings [FILE]
local IFS line
while read -r line; do
printf -- '%s\n' "${line//[^[:alnum:][:blank:][:punct:]]}"
done < "${1}"
}
rasa commented
A slight improvement:
strings() {
if (($#)); then
strings < "$1"
return
fi
local IFS line
while read -r line; do
printf -- '%s\n' "${line//[^[:alnum:][:blank:][:punct:]]}"
done
}
will allow reading from stdin. For example:
cat /bin/bash | strings
wyfhust commented
A slight improvement:
strings() { if (($#)); then strings < "$1" return fi local IFS line while read -r line; do printf -- '%s\n' "${line//[^[:alnum:][:blank:][:punct:]]}" done }
will allow reading from stdin. For example:
cat /bin/bash | strings
Great! Thank you
stringles commented
Additionally [:alnum:][:blank:][:punct:]
can be shorten to [:print:]
:
strings() {
if (($#)); then
strings < "${1}"; return 0
fi
local IFS line
while read -r line; do
printf -- '%s\n' "${line//[^[:print:]]}"
done
}
sensemon-san commented
The above is skipping spaces/tabs at the lines' start in a file. Fixed here:
strings() {
if (($#)); then
strings < "${1}"; return 0
fi
local IFS line
while IFS="" read -r line; do
printf -- '%s\n' "${line//[^[:print:]$'\t ']}"
done
}