Teju Priya
Teju Priya

Reputation: 665

Replace characters in shell variable

Hi I have a shell variable like this

input="/local/1/hadoop/MISDATA1/Risk_Analytics/blocker/risk/"

I want to replace the last part 'risk' to 'risk_unzip' like this

/local/1/hadoop/MISDATA1/Risk_Analytics/blocker/risk_unzip/

I tried like this

output="$( echo -e "$input" | tr 'risk' 'risk_unzip' )"

Not works for me. Any help would be appreciated.

Upvotes: 1

Views: 42

Answers (3)

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

Try with sed like below:-

input="/local/1/hadoop/MISDATA1/Risk_Analytics/blocker/risk/"
input=$(echo "$input" | sed 's/risk/risk_unzip/g')

Upvotes: 1

rici
rici

Reputation: 241881

If you are using bash:

output=${input/risk/risk_input}

If you want it to work with any Posix shell (in which case, the bash tag is incorrect), but you know that risk is the last component:

output=${input%risk/}risk_input/

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133650

Following may help you on same.

echo "$input" | awk -F"/" '$(NF-1)=="risk"{$(NF-1)="risk_unzip"} 1' OFS="/"

Upvotes: 0

Related Questions