Eric
Eric

Reputation: 996

How to pipe file path for parsing?

I have a file path that I need to parse, however, I am pretty new to shell and am not really sure what the appropriate or conventional process would be. Let's say I have a variable representing a file path:

EX=/home/directory/this/might/have/numbers23/12349_2348/more/paths

I need to obtain 12349_2348, which will then become a file name for some other things that I already know how to do. How can I extract this? I know a basic way to do this using regex, which would match with /([0-9])\d+/, however, I determined that by playing around with regexr and have no idea what to do with it from there. I have tried using sed as follows:

echo $EX | sed /([0-9])\d+/ 

but this does not do anything and just gives me an error. What is a better way to do this, and if sed is the best way to do it, what am I doing wrong? I have looked at tutorials and it seems like I should be able to just match the regular expression this way.

Upvotes: 1

Views: 958

Answers (1)

denw
denw

Reputation: 21

It depends on how you know what you're looking for. So for example if you know it's some digits followed by underscore followed by some more digits, you could do this:

dwalker$ EX=/home/directory/this/might/have/numbers23/12349_2348/more/paths
dwalker$ echo $EX | egrep -o '\d+_\d+'
12349_2348

5 digits followed by underscore followed by 4 digits:

dwalker$ EX=/home/directory/this/might/have/numbers23/12349_2348/more/paths
dwalker$ echo $EX | egrep -o '\d{5}_\d{4}'
12349_2348

If you know you need to take off 2 subdirectories off the end, and then what remains is your directory, you can do this:

$ EX1=`dirname $EX`
$ EX1=`dirname $EX`
$ basename $EX1
12349_2348

So there are a couple of ways to do it.

egrep is "extended" grep. It lets you use \d for digits and other things. You can see the man page for more details, and for the explanation of -o.

Upvotes: 2

Related Questions