dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.
MIT License
36.41k stars 3.27k forks source link

Alternative to strings #110

Open ghost opened 3 years ago

ghost commented 3 years ago
strings() {
# Usage: strings [FILE]
local IFS line
while read -r line; do
    printf -- '%s\n' "${line//[^[:alnum:][:blank:][:punct:]]}"
done < "${1}"
}
rasa commented 3 years ago

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 3 years ago

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 3 years ago

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 1 year ago

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
}