part of a filename

Hi,

I need to extract only a part of the filenames of some files. The files are named this way :

.tap_profile_SIT02

I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash :

find .tap_profile_* | grep -o "[A-Z]\{3\}[0-9]\{2\}$"

slight modification to your command

where "." (dot) specifies the command to find from current directory

I'll do the modification later, it never hurt to be more rigorous :slight_smile:

Also, to make things clearer, let's say there is a file named ".tap_profile_SIT02old", I don't need it in the resulting file

Try this with sed:

find .tap_profile_* | sed 's/.*_\(.*\)/\1/'

Regards

find .tap_profile_*
.tap_profile_DEV07
.tap_profile_DEV07_6.2.1.0
.tap_profile_DEV08
.tap_profile_GFER1
.tap_profile_SIT02
find .tap_profile_* | sed 's/.*_\(.*\)/\1/'
DEV07
6.2.1.0
DEV08
GFER1
SIT02

That's why I was trying with a regular expression, not that your idea wasn't a good one.

@bobbygsk

I tried your modification, but the result I get aren't the same. It matches a lot more files

I have not read your 2nd post before I post the solution, try this with awk:

find .tap_profile_* |awk -F"_" '{print $3}'

Regards

find .tap_profile_* |awk -F"_" '{print $3}'
DEV07
DEV07
DEV08
GFER1
SIT02

I'm still getting results that I don't need (.tap_profile_DEV07_6.2.1.0 should be ignored)

What happened when you ran it in ksh ?

Put that grep statement into a sed

find .tap_profile_* | sed -n -e "s/.tap_profile_\([[:alpha:]]\{3\}[[:digit:]]\{2\}\)$/\1/p"

It didn't recognize the -o argument

I was able to combine what I tried with Franklin52's suggestion to achieve what I needed.

find .tap_profile_* | grep "[A-Z]\{3\}[A-Z0-9][0-9]$" | awk -F"_" '{print $3}'

I made some modifications to my regular expression because of the "GFER1" file that never matched.

Anyway, thanks to everyone