Extract string in shell script

Hi All,

i've a string

/u/asak/DATE/TEST_LOGS/20110704_033429_test11_w_2_120/HDD/test11_w_2_12/hd-120

i need to extract string which is test11_w_2_120, this can be start with anything, numbers, characters with underscore etc.. but the time stamp 20110704_033429_ with two underscores will be there and string ends with /.

string may contain like this also 20110704_043944_test11_22_w_2_120, but it always has timestamp with two underscores. even the path may change (/u/asak/DATE/TEST_LOGS) only timestamp with two underscore is constant.

i need it in shell script.
please help ...

Thanks in advance.

Regards,
ASAK

One way through Sed..

echo '/u/asak/DATE/TEST_LOGS/20110704_033429_test11_w_2_120/HDD/test11_w_2_12/hd-120' | sed 's/.*[0-9][0-9]*_[0-9][0-9]*_\([^/]*\).*/\1/'

HI Michael,

But if the string is

/u/asak/DATE/TEST_LOGS/20110704_033429_2_shtest11_w_2_120/HDD/test11_w_2_12/hd-120

with this string it'll not work, how to modify your pattern to accept this. after two underscores, it can contain any thing, any numbers, or characters,
with your code, it works fine if we give number with character. but only number as specified is not working..

please let me know how to do this..

Thanks,
Asak

echo '/u/asak/DATE/TEST_LOGS/20110704_033429_test11_w_2_120/HDD/test11_w_2_12/hd-120' | nawk -F/ '{x=$(NF-3);sub("[^_]*_[^_]*_","",x);$0=x}1'

This second one will remove everything that is before "test" so it should work with your second example as well (if you just want to keep the test* pattern)

echo '/u/asak/DATE/TEST_LOGS/20110704_033429_test11_w_2_120/HDD/test11_w_2_12/hd-120' | nawk -F/ '{x=$(NF-3);sub(".*test","test",x);$0=x}1'

Depending on which behaviour you need :

# echo '/u/asak/DATE/TEST_LOGS/20110704_033429_2_shtest11_w_2_120/HDD/test11_w_2_12/hd-120' | nawk -F/ '{x=$(NF-3);sub(".*test","test",x);$0=x}1'
test11_w_2_120
# echo '/u/asak/DATE/TEST_LOGS/20110704_033429_2_shtest11_w_2_120/HDD/test11_w_2_12/hd-120' | nawk -F/ '{x=$(NF-3);sub("[^_]*_[^_]*_",z,x);$0=x}1'
2_shtest11_w_2_120

If the depth of your 20110704_033429_2_shtest11_w_2_120 directory doesn't vary, then you can replace my $(NF-3) with $6

Ok..Try

echo '/u/asak/DATE/TEST_LOGS/20110704_033429_2_shtest11_w_2_120/HDD/test11_w_2_12/hd-120' | sed 's/.*\/[0-9][0-9]*_[0-9][0-9]*_\([^/]*\).*/\1/'
1 Like

Hi All,

Thanks for all... It's working now.. Thank you very much..

Regards,
asak