0

I am trying to replace \t \r and \n with single space from a string like this in bash

message="${message//[$'\t\r\n']/ }"

But this step is taking too much time. These are my observations wc on message

1. "$message" | wc gives
46     116    2518
time taken:   2.513635815s

2. "$message" | wc 
 85     232    5029
 time taken: 14.649522474s

For larger strings, time increases rapidly.

Note: In message variable, I have random english characters only.

What is the reason for this ? How can I make it quicker?

Prajwal
  • 563
  • 7
  • 22
  • 1
    This suggests to me that bash is moving the whole rest of the string at each update (that is O(n^2)), rather than maintaining a reading and writing pointer (which would be O(n)). From an algorithmic perspective in a base language like C, that's a relatively easy change to make. But at the shell level, to make it quicker you'd need to find something that already implemented an O(n) method, as there's too much overhead to implement that with bash code. I don't see patching bash as a viable answer for this SE, so this is just a comment. – Ed Grimm Feb 10 '19 at 19:35
  • 1
    Did you try **TR** command to [replace the whitespace](https://stackoverflow.com/questions/1271222/replace-whitespace-with-a-comma-in-a-text-file-in-linux#1271384) characters? – hc_dev Feb 10 '19 at 19:35
  • The code appears to be **executing** a `$message` string as though it were a command, then piping that to `wc`. Is `$message` a list of commands, a script name, or is there some other code that we need to see? Please correct the question. – agc Feb 10 '19 at 19:50
  • with set -x being set, i can see the command is taking so much time. I can't see any other commands being executed. @agc – Prajwal Feb 10 '19 at 19:54
  • Bash is meant entirely for invoking other programs and utilities. It's not meant for any kind of data processing. Many of its string operations are quadratic at best. Use a more suitable tool. – that other guy Feb 10 '19 at 21:22

0 Answers0