I have a problem I need to address with bash and particularly sed. My data set is something like this:
Sims, J- PF 45
Samanic, L- PF 91
Noel, N- C 3
I need to end up with something like this:
uniform number for J. Sims: 45
uniform number for L. Samanic: 91
with a single sed command.
My initial tries were something like this:
sed -r 's/(.*), ([[:upper:]])- ([0-9] )/uniform number for \2. \1: \3/' nba_roster.txt
but I can't seem to get any matches for the number part. I managed to get the first name letter and the last name in an earlier version, but nothing combined with the number...
Any help? what am I doing wrong?
CodePudding user response:
Using sed
sed 's/\([^,]*\), \([A-Z]\)[^0-9]*\(.*\)/uniform number for \2. \1: \3/' input_file
uniform number for J. Sims: 45
uniform number for L. Samanic: 91
uniform number for N. Noel: 3
CodePudding user response:
It looks like you missed .* to match any text between - and the number:
sed -r 's/(.*), ([[:upper:]])-.* ([0-9] )/uniform number for \2. \1: \3/' nba_roster.txt
# ^^
You can also use
sed -E 's/^([^,] ), ([[:upper:]])[^0-9]*([0-9] ).*/uniform number for \2. \1: \3/' nba_roster.txt
See the online demo:
#!/bin/bash
s='Sims, J- PF 45
Samanic, L- PF 91
Noel, N- C 3'
sed -E 's/^([^,] ), ([[:upper:]])[^0-9]*([0-9] ).*/uniform number for \2. \1: \3/' <<< "$s"
Output:
uniform number for J. Sims: 45
uniform number for L. Samanic: 91
uniform number for N. Noel: 3
Pattern details
^- start of string([^,] )- Group 1 (\1): any one or more chars other than a comma,- a comma and space([[:upper:]])- Group 2 (\2): an uppercase letter[^0-9]*- zero or more non-digit chars([0-9] )- Group 3 (\3): one or more digits.*- the rest of the string (if any).
