Search code examples
sedhpcpbstorque

'sed' command does not work normally in PBS scripts


I use torque to submit the test script shown blow

#!/bin/bash
#PBS -N test
#PBS -l nodes=1:ppn=1
#PBS -q ser
#PBS -V
#PBS -S /bin/bash

sed 's/a//' <<< aaabbbaaa
sed 's/\(a\)//' <<< aaabbbaaa
sed 's/a\+//' <<< aaabbbaaa

The expect output should be

aabbbaaa
aabbbaaa
bbbaaa

but actually when i use qsub to submit this file, the output becomes

aabbbaaa
aaabbbaaa
aaabbbaaa

The last two commands do not work. And it seems that the character '\' leads to the fault. But why does this happen.


Solution

  • The sed implementation on the server probably has a different syntax than what you have locally.

    Without details about the remote system, this is mildly speculative; but look for the manual page and search for an option to enable Extended Regular Expression syntax (usually -E or -r; if available, probably lose the backslash before the regex specials then).

    As a partial and obvious workaround, aa* is equivalent to a\+ but no similar workaround exists for grouping.

    Maybe also read up on the differences between POSIX Basic Regular Expression syntax (BRE) and ERE; though chances are your PBS system is using something which even predates POSIX if it's legacy big iron.

    If you want to write code which is portable between Linux, MacOS, and whatever behemoth dinosaur OS you have on the PBS system, maybe try Perl instead of sed. Even if you can only rely on Perl 4.x constructs, that's a platform which is a lot more uniform (and also a lot more versatile) than trying to figure out how to write portable sed scripts.

    perl -pe 's/(a+)//'