variable redirect messing up a sed command.

Not sure if that title is decent, but here we go. I'm running this string of commands:

qstat -f $1 | sed 's/^[ \t]*//' | sed -n -e ":a" -e "$ s/\n//gp;N;b a" | sed 's/\\,/,/' | awk -F"PBS_O_WORKDIR=" '{print $2}' | awk -F",PBS_O_SYSTEM" '{print $1}'

In case you're curious is takes the output of a PBS queue, removes all the leading spaces, removes all the linebreaks, removes the backslash escape character in front of any commas, then grabs the output between two strings, effectively extracting a variable from a large mess of output from PBS.

When I run that, it works just fine, all steps work perfectly. However, when I direct the output into a variable, the sed command to remove the backslash fails.

So, I simplified the problem:

echo "Hello\,World" | sed 's/\\,/,/' --> "Hello,World"
var=`echo "Hello\,World" | sed 's/\\,/,/'`; echo $var --> "Hello\,World"

any thoughts?

"Cascading backslashes" a.k.a "Leaning toothpicks"!:wink:

var=`echo "Hello\,World" | sed 's/\\\,/,/'`; echo $var --> "Hello,World"

seems to work!

It's because the backticks are spawning another subshell, but the original backslashes have already been interpreted by the first one.

this simple tr will do that

echo "Hello\,World"|tr -d '\\'

Or

var=$(echo "Hello\,world" | sed 's/\\,/,/g'); echo $var

-Devaraj Takhellambam