eboni
eboni

Reputation: 913

Can't extract pattern from filename

Getting errors from the following sed command:

echo 20130521_OnePKI_p107336_APP.pfx | sed -e 's/_\([pP][0-9]+\)_/\1/'

Instead of returning p107336, it is returning the full filenam 20130521_OnePKI_p107336_APP.pfx.

Any ideas why this is happening, and how I can restrict the output to only the pattern I would like?

Upvotes: 1

Views: 359

Answers (4)

user1019830
user1019830

Reputation:

The regex [pP][0-9]+ in principle matches any substring that begins with either p or P followed by one or more digits. The string "20130521_OnePKI_p107336_APP.pfx" has a substring matching that pattern so the whole string matches the regex.

When grouping with parenthesis around the whole regex on the left side and referring to it on the right side like you did in 's/([pP][0-9]+)/\1/' you're basically saying "replace the match with itself", which will naturally result in the same string as had in the first place.

What you need here is to match the whole string from beginning and then group a part of that string, as already indicated. Then you can refer to that part on the right side to extract it from the bigger string.

You will need to appropriately escape the expression when working in a shell.

Upvotes: 1

perreal
perreal

Reputation: 97958

The captures should be escaped parentheses and you can use case-insensitive match i, also, you are replacing the capture part with the captured part so no changes are made. This one matches the entire line and replaces it with the captured pattern:

sed -e 's/.*_\([pP][0-9][0-9]*\)_.*/\1/'

Upvotes: 1

chooban
chooban

Reputation: 9256

An easier way might be to use grep:

echo 20130521_OnePKI_p107336_APP.pfx | egrep -o "[pP][0-9]+"

The "-o" tells grep to only print the matching part of the input.

Upvotes: 1

Birei
Birei

Reputation: 36262

You must escape parens and +. Also match all the string and substitute all it only with the part you wish (.* before and end your string):

... | sed -e 's/^.*\([pP][0-9]\+\).*$/\1/'

Upvotes: 0

Related Questions