I receive notification files for different files. I want to extract the filename from these notification files which I would use in another process. The first 4 strings of the notification file will always be ignored. So the filename starts at the 5 position, can start with any character and ends at the .csv. The filename could be any length.

An example:

NotificationFile = 'NFT_PPF_STANDARD_1720156B_NBIRNans_DFT_TILT_EOF_FFP_20160619-10.csv_16062000352520'
NotificationFile = 'NFT_PPF_IMPULSE_172555A_Acc_Account_FFP_20160619-10.csv_16062000352520'
NotificationFile = 'NFT_PPF_IMPULSE_172555A_CRI_Position_FFP_20160619-10.csv_16062000352520'

The filenames respectively:

Filename = 'NBIRNans_DFT_TILT_EOF_FFP_20160619-10.csv'
Filename = 'Acc_Account_FFP_20160619-10.csv'
Filename = 'CRI_Position_FFP_20160619-10.csv'

I use the following command to extract the filename from the notification filename but it only works if the filename contains only 3 '_'s:

Extract = echo $NotificationFile | cut -d "_" -f5-8
# $NotificationFile is the NotificationFile above
# Extract should be equal to Filename 

What is a general command I can use for any filename length?

  • 1
    Please edit your question and clarify. What does $notificationFile contain? Is it the name or the entire line you show? What exactly are you trying to extract? What can and what cannot change in the general case? Will there always be 5 fields at the beginning that you want to ignore? Can there be more? Or less?
    – terdon
    Jun 20, 2016 at 9:40
  • I have edited my question
    – deon
    Jun 20, 2016 at 9:45
  • 1
    Great, but please edit it and answer my questions. You need to explain exactly what you need, don't expect us to guess. For example, it looks like you want to remove the 1st 4 fields defined by _, if so say so. You still haven't explained what is in the variable you echo. Is it the NotificationFile = 'NFT_P... that you show in your question? Is it only the part after the =? Is it something else? And how about the name? Ok, the first 4 (presumably "_" separated) fields must be ignored, but what comes afterwards? How many fields? Edit your question and explain exactly what you need.
    – terdon
    Jun 20, 2016 at 9:59

3 Answers 3


For what you want, you could use sed. This is how I would do it.

sed -ne 's/.*\(NB.*\)\_.*$/\FILENAME = \1 /p' 
  • Thanks but this only works if the filename starts with N whereas I have other files that start without N
    – deon
    Jun 20, 2016 at 11:01
  • 1
    @deon that's what terdon asked you. edit your post and show us some more example
    – Rahul
    Jun 20, 2016 at 11:03
  • @deon Your question changes constantly. In your example you had 'surprisingly enough' two lines of filenames which were exactly the same and that's why I assumed the files would start with NB. Again as many times suggested, edit your question and show us what you exactly want Jun 20, 2016 at 11:05
  • Sorry for the confusion. I have edited my question.
    – deon
    Jun 20, 2016 at 11:19
  • @deon for the third time, you need to edit and explain what you need not just edit and add one more example. You need to tell us what is constant in your data and what can change. You need to explain if you are parsing a file containing FILENAME = theNameOfTheFile or a variable with the file name or what?
    – terdon
    Jun 20, 2016 at 11:25

If all the filenames end with .csv, and don't contain another .csv anywhere, use eg. sed to remove everything after that:

Extract=$(echo $NotificationFile | sed 's/\.csv.*/.csv/')

Combining that with what you had before:

Extract=$(echo $NotificationFile | sed 's/\.csv_.*/.csv/' | cut -d "_" -f5-)

The range N- means "to end of line", ie. not stopping at the 8th if there weren't exactly 3 _s in the filename.


Use the string manipulation constructs of parameter expansion.


The first line sets filename to the value of NotificationFile with the first four characters stripped off. The second line removes everything from filename starting with .csv, and re-adds .csv to compensate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .