'SHELL SCRIPT NAME PARSE query 2

I have a file name say:

File_name_something_somthing_20220215ab

Or

File-name-something-somthing-20220215cd

I parse the name in a variable file_nm

I want to get only pattern removing the date part in it.

Output i want is File_name_somthing_something (in first case)

File-name-somthing-something (in second case)

Irrespective of hyphen or underscore.

How can we do that? ${FILE_NM%_*}

work for underscores but for hyphen it fails.



Solution 1:[1]

Does ${file_nm%?[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*} do what you want?

Alternatively, if the separator for the date part is always either - or _, and never something else, ${file_nm%[-_]*} may be what you need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1