how to remove a substring

I want to modify a string in shell script (/bin/sh). Could you please help me.

I have a variable which can store the values like below.
v10.5.2.1p001C_TN1
or v10.5.2.1p002_TN1

I have to search first whether �p' character exists into the string which is stored in a variable.. If yes, then I have to remove the �p001C� and �p002' correspondingly. I mean I have to remove the content from p and before _TN

Thanks in advance.

does this suits you?

-bash-3.2$ cat test
v10.5.2.1p001C_TN1
v10.5.2.1p002_TN1
v10.5.2.1_TN1
v10.5.2.2_TN1
-bash-3.2$ awk -F[p_] '{print $1"_"$NF}' test
v10.5.2.1_TN1
v10.5.2.1_TN1
v10.5.2.1_TN1
v10.5.2.2_TN1
-bash-3.2$

Try this:

sed 's/p..*_/_/' test 

Awesome! Thank you.

Could you please do another favor. By using your command, i am trying to store the value into a variable, but it seems it is not going Could you please see how can i do so.

As per the following code I am reading file .activate_ver and replace the string stored into it. But myAppWebVersion is still empty.

bash-4.0$ more .activate_ver
v6.5.2.1p001C_MR1

bash-4.0$ more test.sh
#!/bin/sh
cat ./.activate_ver | awk -F[p_] '{print $1"_"$NF}' | sort > myAppWebVersion
echo "version is $myAppWebVersion"

bash-4.0$ test.sh
version is
bash-4.0$

IF YOU CAN COULD YOU PLEASE ALSO EXPLAIN ME awk -F[p_] '{print $1"_"$NF}' COMMAND.

you can do it like this. you can try edidataguy's code also :slight_smile:

#!/bin/sh
myAppWebVersion=`cat ./.activate_ver | awk -F[p_] '{print $1"_"$NF}' | sort`
echo "version is $myAppWebVersion"

the code "awk -F[p_] '{print $1""$NF}'" which means that there are two delimiters which is "p" and "". Therefore the string is divided into three

$1           $2      $3/$NF - Last column
v10.5.2.1 p 001C _ TN1

i just printed $1, underscore (_), and $NF

if there is no "p" then it would be the same

$1           $2/$NF - Last column
v10.5.2.1 _ TN1

This was my first posting on this forum.

Thanks a lot to both of you. You are doing a great service to the development community, especially when economy is bad and work pressure is too much.