I have a series of lines form.
Agenda HR-1 Presented by XYZ HR-2 Debate-1 - All HR-3 Debate-2 - All (Cov-4) Conclusion
Each line has an ID of the (sed) pattern
[A-Za-z]\+-[0-9]\+, i.e. one or more alphabets follows by a dash (-) follows by one or more numbers. They occur anywhere in the line.
I need to extract the IDs. My thought was to stick a
.* at the beginning and end and print
\1, but I can't get it to work.
This reply says sed replaces the first match only and that is correct:
$ cat /tmp/scratch/x | sed -n 's/\<\([A-Za-z]\+-[0-9]\+\)/ID:\1/p' Agenda ID:HR-1 Presented by XYZ ID:HR-2 Debate-1 - All ID:HR-3 Debate-2 - All (ID:Cov-4) Conclusion
But of course a
.* at start would greedily go to the last match:
$ cat /tmp/scratch/x | sed -n 's/.*\<\([A-Za-z]\+-[0-9]\+\).*/ID:\1/p' ID:HR-1 ID:Debate-1 ID:Debate-2 ID:Cov-4
The only way I can think of doing this in
sed is by adding markers around the ID in one command and extract using another, as follows.
Is there a better way to do this in sed?
$ cat x | sed -n 's/\<\([A-Za-z]\+-[0-9]\+\)/<id>\1<~id>/;s/.*<id>\(.*\)<~id>.*/\1/;p' HR-1 HR-2 HR-3 Cov-4