1
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?

2
  • Have you tried anything? Commented May 15, 2018 at 11:14
  • @AndreyTyukin already tried find \{[0-9]{4,}\} as variable and replace with sed, but i have lot of same lines Commented May 15, 2018 at 11:43

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

lot of thnx! used re='\{([0-9]{4,})\}'
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.