LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help for simple bash script - searching strings (https://www.linuxquestions.org/questions/linux-software-2/help-for-simple-bash-script-searching-strings-902664/)

lpallard 10-09-2011 11:12 AM

Brilliant! we're on the exact same page!

Now I implemented your suggestions in my script but I am not entirely sure of the logic and syntax of the first string extractor...

Code:

${x%% *}
I understand the whitespace in the command above is the delimiter. I tried substituting the space for a plus sign, and added a + sign somewhere in the string to search, and I got everything from the start top the + sign...

But if I wanted to extract the 3rd string, how would you do this? Using whitespaces as delimiters would work. Lets say for

Code:

StringA 123 4567 4568 RandomStringA StringB RandomStringB
I want to return what is between the 4th & 5th space, i.e. the "RandomStringA"

Thanks, I appreciate your mentoring :)

lpallard 10-09-2011 11:22 AM

I may have found something... Reading the tutorials on awk, I found this:

Code:

echo $string | awk '{split($position-of-the-delimiter,a,"delimiter");print a[1]}'
so if I want to return the 5th string (separated by spaces) I'd use:

Code:

echo $string | awk '{split($5,a," ");print a[1]}'
which returns

Code:

RandomStringA
Is there a better way to do that?

grail 10-09-2011 11:28 AM

Code:

echo $string | awk '{print $5}'
Your code was floored as it would grab the fifth field and split on spaces of which there is none and just return the full string anyway.

I am curious ... how is any of what we are doing now related to the original question of splitting html code to be used by curl??

lpallard 10-09-2011 12:07 PM

Quote:

I am curious ... how is any of what we are doing now related to the original question of splitting html code to be used by curl??
not related at all. I thought keeping the same type of questions in the same thread so I can collate the learning better...

With your help, I think I am done!

I just tried with a bunch of test directories and files, works pretty well!

Thanks a lot!

grail 10-09-2011 06:35 PM

Helping is not so much the problem but if someone else tries to search for your second set of solutions it may be harder to find as there is no specific topic :)

General rule of thumb here is that if you want to ask an alternate question which diverges enough from the original then it is probably best to close one question and ask
another.

Glad you got to where you were going though :)

lpallard 10-09-2011 06:54 PM

Quote:

General rule of thumb here is that if you want to ask an alternate question which diverges enough from the original then it is probably best to close one question and ask
another.
Yeah I had the feeling it would be better... "Should have"...

Sorry about that!

grail 10-09-2011 08:01 PM

nah ... no biggee :)


All times are GMT -5. The time now is 08:50 PM.