Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Alternative to strings #110

Open
ghost opened this issue Nov 27, 2020 · 4 comments
Open

Alternative to strings #110

ghost opened this issue Nov 27, 2020 · 4 comments

Comments

@ghost
Copy link

ghost commented Nov 27, 2020

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

rasa commented Dec 22, 2020

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
Copy link

wyfhust commented Jan 3, 2021

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
Copy link

stringles commented Jan 3, 2021

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
Copy link

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
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants