Tuesday, October 25, 2022

[SOLVED] Linux test command with sed is not working

Issue

Here is my code which always returns 'false' only:-

export STR="MYC-14:: This is sample string only."
echo $STR
test -z "$(echo "$STR" |  sed '/^MYC-/p' )" && echo "true" || echo "false" 

I'm trying to match the starting characters "MYC-" from the variable called "STR" but seems like the regular expression within sed condition is wrong due to which test command is returning false.


Solution

Finally, I'm able to make it work:-

export STR="MYC-14:: This is sample string only."
echo $STR

test -z "$(echo "$STR" |  sed '/^MYC-/d' )" && echo "truee" || echo "falsee" 


Answered By - vinod827
Answer Checked By - Katrina (WPSolving Volunteer)