ykembayev
ykembayev

Reputation: 106

Replace string and move part to result using Bash

text="TASK {5662}: Task definition"
result="TASK [5662](htttps://somelink.com/task/5662): Task definition"

need to "{5662}" replace with "[5662](htttps://somelink.com/task/5662)" using bash

count of task's number maybe equals or more than 4 digits

any ideas?

Upvotes: 1

Views: 56

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133458

Solution 1st: Following awk may help you on same. (where I am not hard coding the digits simply matching it by regex and putting new values then)

awk --re-interval -v val="[5662](htttps://somelink.com/task/5662)" 'match($0,/[0-9]{4}/){print substr($0,1,RSTART-1) val substr($0,RSTART+RLENGTH+1)}' Input_file

Also --re-interval is for old versions of awk for new versions of awk it may not needed too.

Solution 2nd: With sed by hard coding digits:

sed 's#{5662}#[5662](htttps://somelink.com/task/5662)#'  Input_file

Upvotes: 1

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

using bash regex and expansion features

text="TASK {5662}: Task definition"
re='^TASK \{([0-9]{4,})\}: Task definition$'
if [[ $text =~ $re ]]; then
    id=${BASH_REMATCH[1]}
    result=${text/"{$id}"/"[$id](htttps://somelink.com/task/$id)"}
else
    result=
fi

Upvotes: 2

Related Questions