Extract substring of unknown length from string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract substring of unknown length from string
# 1  
Old 01-31-2009
PHP Extract substring of unknown length from string

I have a string:

Code:
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:

Code:
"$hgLogOutput" | awk '{ print index($0, "files: ") }'

to print the location of the start of files: to the console but I can't assign that to a variable. If I could I think I could use something like:

Code:
fileStrings=${hgLogOutput:$startIndex:$endIndex-$startIndex}
echo $fileStrings

Can anyone assist? I have been hunting around on how to do this for a couple of hours and my eyes are now bleeding.

Last edited by klogger; 01-31-2009 at 10:04 AM..
# 2  
Old 01-31-2009
Do you want the filenames as a string value?
Something like this:
Code:
% hgLogOutput=" +0000 files: forum/web/hook-test.txt /forum/web/hook-test-2.txt description: test"
% f="${hgLogOutput#*files: }" f="${f% description*}"                                              
% printf "%s\n" "$f"                                                                              
forum/web/hook-test.txt /forum/web/hook-test-2.txt

# 3  
Old 01-31-2009
Thanks radoulov, that is exactly what I was after.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C: inputting string of unknown length

I realize this general issue (inputting strings of variable length in C) has been addressed in myriad locations before, but I'm interested in knowing why my specific approach is not working. (BTW I'm intentionally keeping the size increments small so that I can more easily follow what's going on.... (5 Replies)
Discussion started by: DevuanFan
5 Replies

2. Shell Programming and Scripting

Extract substring specif position and length from file line

Hi gurus, I am trying to figure out how to extract substring from file line (all lines in file), as specified position and specified legth. Example input (file lines) dhaskjdsa dsadhkjsa dhsakjdsad hsadkjh dsahjdksahdsad sahkjd sahdkjsahd sajkdh adhjsak I want to extract substring on... (5 Replies)
Discussion started by: ProsteJa
5 Replies

3. Shell Programming and Scripting

Removing characters from end of line (length unknown)

Hi I have a file which contains wrong XML, There are some garbage characters at the end of line that I want to get rid of. Example: <request type="product" ><attributes><pair><name>q</name><value><!]></value></pair><pair><name>start</name><value>1</value></pair></attributes></request>�J ... (7 Replies)
Discussion started by: dirtyd0ggy
7 Replies

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

5. Shell Programming and Scripting

Delimted to padded conversion with unknown field length

I’m looking for an elegant way to convert a delimited file (comma delimited in this case) to padded columns (for printing in non-proportional font) but the length of each column is not known ahead of time. It needs to be calculated for each column from the longest entry in that column in a given... (3 Replies)
Discussion started by: Michael Stora
3 Replies

6. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: kirkg
2 Replies

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

8. Shell Programming and Scripting

perl newbie: how to extract an unknown word from a string

hi, im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string. Eg. input string(s) : char var1 = 'A'; int var2 = 10; char *ptr; and what i want to do is... (3 Replies)
Discussion started by: wolwy_pete
3 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