Extract string in shell script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string in shell script
# 1  
Old 07-04-2011
Error Extract string in shell script

Hi All,

i've a string

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.

# 2  
Old 07-04-2011
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/'

# 3  
Old 07-04-2011
HI Michael,

But if the string is

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..

# 4  
Old 07-04-2011
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'
# 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'

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

Last edited by ctsgnb; 07-04-2011 at 10:10 AM..
# 5  
Old 07-04-2011
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/'

This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 07-06-2011
Hi All,

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies

2. Shell Programming and Scripting

How can I extract digits at the end of a string in UNIX shell scripting?

How can I extract digits at the end of a string in UNIX shell scripting or perl? cat file.txt abc_d123_4567.txt A246_B789.txt B123cc099.txt a123_B234-012.txt a13.txt What can I do here? Many thanks. cat file.txt | sed "s/.txt$//" | ........ 4567 789 099 012 13 (11 Replies)
Discussion started by: mingch
11 Replies

3. UNIX for Beginners Questions & Answers

Shell script to extract data in a file

I have this 5GB file, and i want to extract from the file particulars pattern. this is my script: // count=`grep -wc "MSISDN" file_name` k=1 >OUTPUT >OUTPUT_Final while do cat file_name | awk -F":" -v var="$k" '$1=="MSISDN" {m++}m==var{print; exit}' >> OUTPUT cat file_name |awk -F":"... (33 Replies)
Discussion started by: gillesi
33 Replies

4. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

5. Shell Programming and Scripting

Help with shell script to extract certain information

Hi, I have a file which I need to programmatically split into two files. All the information in the file before pattern "STOP HERE" is to be stripped and output into one file while everything after "STOP HERE" is to be output into a separate file. I would appreciate help on how to do... (8 Replies)
Discussion started by: PTL
8 Replies

6. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

7. Shell Programming and Scripting

Shell Script to extract string from file

Hello, I have a file whose contents look as follows <ID="24" name="Kobe Bryant" Position="Shooting Guard"> <name="Artest" Position="Forward" ID="37"> I need to get the output which contains everything in the quotes for Position (Shooting Guard and Forward). I tried this but it doesn't work... (3 Replies)
Discussion started by: grajp002
3 Replies

8. Shell Programming and Scripting

Shell Script to extract Soft

...Software and Disk information... Hey guys.... I need to extract a bunch of information from a set of servers running Solaris 10 where I need to know for each server: - all OS Versions and patch levels - 3rd party COTS or opensource software/processes running on all servers (several... (0 Replies)
Discussion started by: northern1969
0 Replies

9. Shell Programming and Scripting

Using Awk in shell script to extract an index of a substring from a parent string

Hi All, I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this. Requirement : Need to get the index of a substring from a parent string Eg : index("Sandy","dy") should return 4 or 3. My Approach : I used Awk function index to... (2 Replies)
Discussion started by: sandeepms17
2 Replies

10. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies
Login or Register to Ask a Question