Need help to extract part of the string

Hi,

I have a string with name as 20140412-s1-Potopolive_promos_20140412 . So I want to extract only Potopolive string. Could you please help me the command.

O/p : Potopolive

Thx in advance

Hello lkeswar,

Please use code tags for commands/codes used in your posts as per forum rules. Following may help you in same.

i-

echo "20140412-s1-Potopolive_promos_20140412" | awk '{sub(/.*-/,X,$0);sub(/_.*/,X,$0);print}'

ii-

A="20140412-s1-Potopolive_promos_20140412"
B=`echo ${A##*-}`
echo ${B%%_*}

Output will be same in both the cases, hope this helps.

Thanks,
R. Singh

Note: instead of B=`echo ${A##*-}` one could use: B=${A##*-}

1 Like

Try:

echo "20140412-s1-Potopolive_promos_20140412" | awk -F "-" '{print $3}' | awk -F "_" '{print $1}'

HTH!!

Hello Mannu2525,

Your code can be written as follows without using 2 times awk .

echo "20140412-s1-Potopolive_promos_20140412" | awk -F"-|_" '{print $3}'

Thanks,
R. Singh

[akshay@localhost tmp]$ echo '20140412-s1-Potopolive_promos_20140412' | awk 'gsub(/.*-|_.*/,"")'
Potopolive