[Solved] HP-UX awk sub multiple patterns

Hi,

I am using sub to remove blank spaces and one pattern(=>) from the input string. It works fine when I am using two sub functions for the same. However it is giving error while I am trying to remove both spaces and pattern using one single sub function.

Working:

$ echo " OK => " |awk '{sub(/^ *+| *$/,"",$0);sub(/=>/,"",$0);print $0;}'
OK
$

Error

$ echo " OK => " |awk '{sub(/^ *+| *$+//=>/,"",$0);print $0;}'
awk: There is a regular expression error.
        ?, *, or + not preceded by valid regular expression
 The source line number is 1.
 The error context is
                {sub(/^ *+| >>>  *$+/ <<< /=>/,"",$0);print $0;}
$

Need your help in combining the both patterns in single sub function.

Thanks in Advacne....

1) You are using sub which will attempt only 1 substitution if possible.
2) The regex quantifier + is incorrectly placed/used.

Try:

echo " OK => " |awk '{gsub(/^ +| +$|=>/,"");print $0}'
1 Like

Thank u so much elixir for prompt response...