I dont want to add those delimiters in print manually because _,- is not fixed i may get all delimiters as _ or - or both.
Is there any way to print first 5 words of filename along with the delimiters?
Note that echo on many systems will interpret some sequences of characters as escape sequences and perform various transformations that you don't want. Using printf as shown above instead of echo avoids that issue.
Scrutinizer,
I think gnnsprapa is saying that any of the underscores (not just the first one) could be hyphens instead of underscores. Your suggestion assumes that the number of words stored in the variable is a constant (which has not been stated) and that the last five words are separated by underscores only.
As long as the given value assigned to the variable f contains at least six words, the following should work:
with his version of bash or a recent ksh . The awk suggestion above should work even if there are only five words in the value assigned to f and should work with any shell using Bourne shell syntax.
The awk command extracts count characters (sum of lenghts of the first 5 fields) plus 4 field separators from $0, the unmodified input line, starting at the begin of line.
Try also