Reputation: 97
I need to remove [PR:]
from the [PR:Parker]
which only print "ParkerS
"
Note:[PR:xxxxxxx] "xxxxxxx" Part is changed time to time.
Upto now I have create a following sed command:
sed 's/[PR:]//g' | sed 's/[][]//g'
But it prints "arkerS
" which missing the "P
" in name too.
Upvotes: 1
Views: 242
Reputation: 133458
1st solution: With awk
, with your shown samples, please try following code once. Using gsub
function to globally substituting starting [
followed by PR:
and ]
ending with NULL and printing rest of the values of line.
awk '{gsub(/^\[PR:|\]$/,"")} 1' Input_file
2nd solution: Using different field separator(s) in awk
code to grab 2nd last value as per shown samples, try following.
awk -F':|\\]' '{print $(NF-1)}' Input_file
3rd solution: Using match
function of awk
try following. Matching regex /:[^]]*/
from 1st occurrence of :
to before ]
occurs and printing the matched part only as per requirement.
awk 'match($0,/:[^]]*/){print substr($0,RSTART+1,RLENGTH-1)}' Input_file
4th solution: Using bash capability of parameter expansion here. In case you have this value in a shell variable then this will be BEST solution to go for.
##If your shown sample is in a shell variable, use parameter expansion then.
var="[PR:Parker]"
##Create interim variable var1 to remove everything from starting till : here.
var1="${var##*:}"
echo "$var1"
Parker]
##Then on var1 remove ] and get needed value here.
echo "${var1%*]}"
Parker
5th solution: Using perl
one liner try following, performing global substitution to remove starting [PR:
and ending ]
with null.
perl -pe 's/^\[PR:|\]$//g' Input_file
Upvotes: 3
Reputation: 626747
You can use
sed 's/\[PR:\([^][]*\)]/\1/' <<< "[PR:Parker]"
Here, the \[PR:\([^][]*\)]
matches [PR:
, then any zero or more chars other than [
and ]
are captured into Group 1 and a ]
is matched, and the match is replaced with the Group 1 value (with \1
placeholder).
Or,
sed -E 's/\[PR:|]//g' <<< "[PR:Parker]"
See the online demo. Here, \[PR:|]
matches either [PR:
or ]
and the s
command removes them.
Upvotes: 2