0

I would like to create a temp-folder from some generated string input, so for example I want to get only the letters and numbers from this imput:

"m'i;x&e\"d u(p\nmulti)\nlines'\nand\015ca&rr\015re;t"

should result in

mixedupmultilinesandcarret

Is there a command that creates such a simple string to use it as temp filename in a script?

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • 1
    `tr -dc '[[:alpha:]]'` will translate your string to only have alpha characters, just as the linked duplicate shows doing the same with `[[:print:]]` to limit to printable characters. – Charles Duffy Dec 19 '21 at 23:51
  • BTW, does your input string really have the two-character sequence `\n` in it, or a newline you're representing that way? Calls for a different answer if it's the former you want to strip (same for `\015` as an escape sequence, or a specifier for a single character -- if you want to evaluate escape sequences before removing invalid characters, you might need to interpret them with something like `printf %b` before you run the result through `tr`). – Charles Duffy Dec 19 '21 at 23:53
  • 1
    BTW, it's considerably more efficient than an external process like `tr` (assuming you're only running one name through each instance) to rely on parameter expansions to do the substitution built into bash; see https://wiki.bash-hackers.org/syntax/pe -- if no answers on the linked duplicate demonstrate the approach, I might add one. (Edit: Added just such an answer there). – Charles Duffy Dec 19 '21 at 23:55

0 Answers0