My records are like this
BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97|
BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97|
BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97|
I want to extract the value before _97|
This command
BSC_ID=`echo $DATA | cut -f5 -d"_"`
gives me
_97|, 4, 11
and by using the command
echo $DATA | awk -F_ '{print $(NF-1)}'
I get LIMJM1-3, 4, 11.
when i use the BSC_ID=`echo $DATA | awk -F[_-] '{print $(NF-1)}`
i get
BSC403_JAIN03
BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97|
BSC403_JAIN03
BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97|
BSC406_BMIN02
BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97|
if you take a a closer look at the previous suggestions, you'll see that there's no assumptions of the 'number of underscores/dashes in the file. The only assumption (based on your sample file] is that you want to get the 'next to last' field in the underscoreORdash separated record/line.
Is the above correct description of the objective?
.* - any character repeated 0 or more times - greedy - will consume ALL the character leading to the LAST non-underscore/non-dash char followed b dashORunderscore.
[-] - followed by either a '-' or a '' char
\([^-_][^-]*\) - followed by a 'capture' of any character other then '-' or '' repeater 0 or more times.
[-] - followed by either a '-' or a '' char
.* - any character repeated 0 or more times - greedy
\1 - replace the 'matched' string with the FIRST 'capture'
I know it might be a bit confusing reading the regEx expressions at times, but try to think 'pattern matching'....