0

I have a bash script that involves the output of yad. By default, yad outputs its results separated by pipe characters "|".

I have a string variable output. Echoing it shows it contains this:

/dev/sde|
name0|name1|name2|

I want all those vertical bars changed to line breaks.

This does not work: echo "$output" | tr '|' '\n'
And neither does this: echo "$output" > /tmp/output; echo $(cut -d'|' -f1 < /tmp/output)
Both attempts result in only a line break being outputted. /tmp/output was created, and it contained two empty lines but not any text.

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
Botspot
  • 151
  • 6
  • What shell are you using? Your `tr` command is working for me. – rusty shackleford Feb 11 '20 at 13:51
  • 1
    If `echo "$output" > /tmp/output` produces a file that contains two empty lines, then there is something else going on with your variable; please [edit] your question to include the output of `printf '%s' "$output" | xxd` or similar – steeldriver Feb 11 '20 at 14:12
  • Please [edit] your question and i) tell us your operating system. Your `tr` command should work, so if it doesn't, it might be specific to whatever `tr` your operating system uses. ii) Explain what actually happens when you run the `tr`. Just telling is it doesn't work isn't very helpful. How does it fail? Are there any errors? Does it just not do anything at all? iii) Clarify if your `$output` variable contains multiple lines, with newlines, or just one. – terdon Feb 11 '20 at 14:40

2 Answers2

1

Your tr should be fine.

Try to escape "\|"

realpclaudio
  • 528
  • 3
  • 10
0

I finally figured out what was going on: the variable was being set incorrectly. Shortened:

yad --plug=12345 --tabnum=1 &
yad --plug=12345 --tabnum=2 &
output="$(yad --notebook --key=12345)"
echo "$output"

$output appeared to have been echoed since the results were printed to the terminal.
But removing the echo had no effect. Then I realized that the first two commands were the ones outputting the data, not the third & outer yad command.
Moving output="$( to the start of the first yad command solved the problem of the empty variable.

And, I browsed through man yad and discovered that there is a cli flag for just what I wanted:

--separator='\n'
Botspot
  • 151
  • 6