Extract a string up to the first occurence of a substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a string up to the first occurence of a substring
# 1  
Old 12-12-2009
Extract a string up to the first occurence of a substring

Hi,

I'm a newbie to shell scripting and have searched the forum but couldn't find what i was looking for.

Basically I have a list of filenames like...
123-fileone.txt

I want to be able to extract the prefix up to the first '-'. So I'd end up with 123. I have attempted it using a pretty basic regex ({F%-*}) but that trips up when there is more than 1 occurrence of '-' in the filename, which there can be.

I'm sure it's a simple thing to do...just not for me Smilie

Thanks.

Kirk.
# 2  
Old 12-12-2009
Code:
$ string="123-fileone.txt"
$ echo ${string%%-*}
123

# 3  
Old 12-12-2009
Perfect! Thanks...

(Don't I feel stupid now Smilie)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract until nth occurence

Hi, I couldn't figure how to extract until last occurence of a character. I have the string ./dir1/file1/abc.sh The output should be /dir1/file1 So, the command should display the path until last occurence of "/". Thanks. (3 Replies)
Discussion started by: rajivn786
3 Replies

2. Shell Programming and Scripting

How to extract a substring from a string

Hi, I have an input string say for example: ABC,DEF,IJK,LMN,...,XYZ The above string is comma delimited. Now I have to extract the last part after the comma i.e. XYZ. :b: (3 Replies)
Discussion started by: bghosh
3 Replies

3. Solaris

Extract substring from a string

i have srtring i.e. "NAME,CLASS,AGE" (length of string is not constant) and from this string i've extract each word delimited by "," (comma). INPUT: "NAME,CLASS,AGE" OUTPUT: NAME CLASS AGE how can i do that? i have tried some string manipulation function like... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

4. Shell Programming and Scripting

how to extract last occurence of the field

path = /first/second/third/fourth. i want to extract /first/second/third from path.my program something like this .... path=/first/second/third/fourth noc=`echo $path|tr '/' '\n'|wc -w` var='echo $noc|cut -d'/' -f 1-$noc --> is giving error. why $noc is not working here.any other... (3 Replies)
Discussion started by: kcp_pavan
3 Replies

5. UNIX for Dummies Questions & Answers

Extract substring of unknown length from string

I have a string: hgLogOutput=" +0000 files: forum/web/hook-test.txt /forum/web/hook-test-2.txt description: test" and I want to extract the file names from it, they will always appear between the files: and the description:. I have worked out that I can do this: "$hgLogOutput" | awk '{... (2 Replies)
Discussion started by: klogger
2 Replies

6. Shell Programming and Scripting

Extract a substring.

I have a shell script that uses wget to grab a bunch of html from a url. URL_DATA=`wget -qO - "$URL1"` I now have a string $URL_DATA that I need to pull a substring out of..say I had the following in my string <p><a href="/scooby/929011567.html">Dog pictures check them out! -</a><font... (3 Replies)
Discussion started by: shellpower
3 Replies

7. Shell Programming and Scripting

Need Help... to extract the substring

> tnsping $TWO_TASK | grep HOST Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.12.10.212)(PORT = 1540)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = OMTST15))) I want to extract like this HOST = 10.12.10.212 PORT = 1540 SERVICE_NAME = OMTST15 I... (4 Replies)
Discussion started by: dashok.83
4 Replies

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

9. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 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