The time now is Thu 19 Apr 2018, 10:26
All times are UTC - 4 |
Page 2 of 2 [21 Posts] |
Goto page: Previous 1, 2 |
Author |
Message |
amigo
Joined: 02 Apr 2007 Posts: 2641
|
Posted: Sat 08 Sep 2012, 13:16 Post subject:
|
|
Offhand I don't even remember what this bash-centric feature is called! Maybe it's 'variable substitution'.
But this one means "everything(*) to the left of the first '-'":
INDEX=${OUTSTR%%-*}
And this one means "everything to the right of the first '-'":
FILENAME=${OUTSTR#*-}
You can play with it in a terminal to understand it better:
Code: |
TEST=59170-LazY-Fred-English-Locals.tar.gz
echo ${TEST%%-*}
echo ${TEST#*-}
|
Then vary that like this:
Code: |
echo ${TEST%-*}
echo ${TEST##*-}
|
Using one or two '#' characters parses the string from left to right. Using one or two '%' chars parses from right to left. Using doublke characters means to use the longest match, using a single char returns the shortest match.
Anyway, a little trick like that can make your code run hundreds of times faster than some multi-command pipeline. For a single instance you'd not notice the difference, but if that line is being used inside a loop which runs many times, then the difference can be dramatic.
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Sat 08 Sep 2012, 22:35 Post subject:
|
|
There are many possibilities using cut, awk, sed, grep . Since cut is already known in combination with echo ,
here the others :
Code: | STRING="1234-\"attached.file.pet\"" |
awk :
Code: | echo "$STRING" |awk -F '"' '{print "\""$2"\""}' |
sed :
Code: | echo "$STRING" |sed 's:.*-::' |
grep :
Code: | echo "$STRING" |grep -o '".*"' |
*
On a large database file :
Code: | awk -F '"' '{print "\""$2"\""}' /database_file.db >/newfile.filenames-only.awk.list |
Code: | sed 's:.*-::' /database_file.db >/newfile.filenames-only.sed.list |
Code: | grep -o '".*"'/database_file.db >/newfile.filenames-only.grep.list |
might be faster than a loop .
http://www.grymoire.com/Unix/Sed.html is the reference i download whenever i have/had sed questions .
http://www.grymoire.com/Unix/Awk.html is not as good as the sed tutorial as my impression is,
still have to checkout
http://www.grymoire.com/Unix/Grep.html .
Google for " $COMMAND tutorial " brings up a lot of stuff .
*
Other chars for shell variable substitution are '/' & '//' and since bash-4 '^' ',' & '^^' ',,' :
Code: | STRING="ABCDCEFG-123.456.tar"
echo "${STRING//C/cccCccc}"
echo "${STRING/C/cccCccc}" |
[code=bash-4]echo "${STRING,C}"[/code]
Learn here :
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Sun 09 Sep 2012, 02:10 Post subject:
|
|
You can mostly automate this by downloading the page as html only. Then run the script against the page(s). And have that script download the attachments with both numbers and filenames.
Is this even what you are thinking about?
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 917
|
Posted: Sun 09 Sep 2012, 12:41 Post subject:
|
|
amigo wrote: | Offhand I don't even remember what this bash-centric feature is called! Maybe it's 'variable substitution'.
................................
Anyway, a little trick like that can make your code run hundreds of times faster than some multi-command pipeline. For a single instance you'd not notice the difference, but if that line is being used inside a loop which runs many times, then the difference can be dramatic. |
amigo,
Yes, I spent time avoiding using bash string manipulations because when I read the explanations, I thought I understood how they worked, and then later when I went to use them, I'd draw a blank and have to extensively play with the expressions in a terminal to get it right, until this change in thought process......
Think of what you don't want in the string (what you want to cut out)
# (first instance) = left side (beginning) of string ##=last instance encountered
% (first instance) = right side (end) of string %%=last instance encountered
str="don't do what I do, do what I say"
eliminate 'don't do' from the left (return "what I do, do what I say")
${str#*do }
starting from the left (#) eliminate *(all chars) up to the first "do " encountered -notice the space after do, if it's not there, it will only eliminate the first "do" in "don't" , leaving "n't do what I do....."
eliminate "do what I say" from the right
${str%do*}
starting from the right (%) eliminate all chars (*) up to the first encountered "do"
In addition to speed and efficiency, another advantage over "cut" is that you can use more than one char as a delimiter.
Regards,
s
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 4785 Location: Kingwood, TX
|
Posted: Sun 09 Sep 2012, 13:30 Post subject:
|
|
stu91 wrote: | amigo wrote: | All these loops and extraneous use of echo, sed and cut... Bash makes this very simple:
Code: |
INDEX=${OUTSTR%%-*}
FILENAME=${OUTSTR#*-}
|
|
Hi Amigo,
Could you give a breakdown on what the different characters represent in you code - or any links etc that might expand on such code further.
Thanks in advance. | see "substring manipulation" in the advanced bash scripting guide.
_________________ Check out my github repositories. I may eventually get around to updating my blogspot.
|
Back to top
|
|
 |
Moose On The Loose

Joined: 24 Feb 2011 Posts: 778
|
Posted: Sun 09 Sep 2012, 16:25 Post subject:
|
|
technosaurus wrote: | stu91 wrote: | amigo wrote: | All these loops and extraneous use of echo, sed and cut... Bash makes this very simple:
Code: |
INDEX=${OUTSTR%%-*}
FILENAME=${OUTSTR#*-}
|
|
| see "substring manipulation" in the advanced bash scripting guide. |
Just as a BTW a trick I use fairly often is to make the thing I am working on contain spaces at key points and then do:
Code: |
MyFunctionName $TheStringInQuestion
|
This way all the bits of the string come into the function as $1 ...
Inside the function I can then do all manner of stuff to the string. This is good when what you want to do is more complex than the example here because it makes it easier to document what is done.
|
Back to top
|
|
 |
|
Page 2 of 2 [21 Posts] |
Goto page: Previous 1, 2 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|