Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 29 Jul 2014, 02:49
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Substitute chars in part of a line
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [11 Posts]  
Author Message
aragon

Joined: 15 Oct 2007
Posts: 1698
Location: Germany

PostPosted: Thu 31 Dec 2009, 10:21    Post subject:  Substitute chars in part of a line  

for a little menu-project, i need to substitute " " with "-" ... but only some ...

here is an example line
Code:
Calcoo scientific calculator "calcoo"

the result should be
Code:
Calcoo-scientific-calculator "calcoo"

so the substitution should substitute
all " " with "-" but not
within double-quotes and not
direct before a double quote.

alternatively, i could output the line with 'control-characters' for the substitution-array like
Code:
#Calcoo scientific calculator# "calcoo"


any help?

thanks in advance
aragon

_________________
PUPPY SEARCH: http://wellminded.com/puppy/pupsearch.html
Back to top
View user's profile Send private message 
jemimah


Joined: 26 Aug 2009
Posts: 4309
Location: Tampa, FL

PostPosted: Thu 31 Dec 2009, 12:04    Post subject:  

Code:
echo $INPUT | tr ' ' - | sed 's/-\"/ \"/'


There's probably a more elegant way to do that in one step, but it's a start.

Edit, nope that won't work for spaces inside the double quotes, let me think a minute...
Back to top
View user's profile Send private message Visit poster's website 
jemimah


Joined: 26 Aug 2009
Posts: 4309
Location: Tampa, FL

PostPosted: Thu 31 Dec 2009, 12:46    Post subject:  

I tried really hard for a one liner, but I can't figure it out, so here's the boring way.

Code:

echo $INPUT | while read line ; do
quoted-part=`echo $line|cut -f2 -d\" `
hyphen-part=`echo $line|cut -f1 -d\"|tr " " - |sed 's/-$//'`
echo "$hyphen-part \"$quoted-part\""
done
Back to top
View user's profile Send private message Visit poster's website 
2byte

Joined: 09 Oct 2006
Posts: 357

PostPosted: Thu 31 Dec 2009, 12:56    Post subject:  

Quote:
I tried really hard for a one liner, but I can't figure it out, so here's the boring way.

Ditto. Here's my lame attempt.
Code:
#!/bin/bash
line='Calcoo scientific calculator "calcoo calculator"'
x="`expr "$line" | tr ' ' - | sed 's/-\"/ \"/' | cut -d' ' -f1`"
y="`expr "$line" | cut -d'"' -f2`"
newline=$x' "'$y'"'
echo $newline

_________________

Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2221

PostPosted: Thu 31 Dec 2009, 13:02    Post subject:  

bash baby! don't need all those external calls. If you run this looped a few hundred times, you'll see a big speed difference by just using what the shell can do.

Code:
#!/bin/bash
STRING='Calcoo scientific calculator "calcoo"'
PART1=${STRING%% \"*} PART2=${STRING#*\"} ; echo ${PART1// /-} \"$PART2
Back to top
View user's profile Send private message 
jemimah


Joined: 26 Aug 2009
Posts: 4309
Location: Tampa, FL

PostPosted: Thu 31 Dec 2009, 13:18    Post subject:  

Yeah I figured there was some Bash voodoo like that you could do. Can you recommend a good (fun!) bash programming book?
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2221

PostPosted: Thu 31 Dec 2009, 14:13    Post subject:  

No, I don't really know of a fun book or manual. There is, of course, the bash advanced scripting guide:
http://tldp.org/LDP/abs/html/

I do nearly everything in bash, but I don't even touch any of the really advanced techniques, even though they would save lines or make even more possible.

You might like to look and learn from some examples I have done as a hobby. I have put together pure-bash clones of over a dozen command-line utilities -even a version of wget. Most of them follow a common methodology and syntax, so studying them gets easier as you go along. You can find them here:
http://distro.ibiblio.org/pub/linux/distributions/amigolinux/download/AmigoProjects/BashTrix/

To explain what I did above:
PART1=${STRING%% \"*}
separates what is to the left of ' "' (space+double-quote)
PART2=${STRING#*\"}
separates what is to the right of the first '"'

To illustrate this a little better, use a longer string which includes more than one quoted word, and then try various combinations using one '#' or two '#' chars, and one '%' or two '%' chars
Using one of either % or # means the shortest match, using two mean the longest match.
This:
echo ${PART1// /-}
substitutes the spaces with '-' chars. If you do this:
echo ${PART1/ /-} \"$PART2
it only substitutes the first occurrence(using the double'//' makes it act like the sed 'g' (global) option.
The double-quote chars always have to be escaped.

I dislike using regex's because it makes my head spin to try and read them. I used to use cut, grep and rev a lot, but mostly I do it with bash these days, even though it takes more lines to do the same thing. BTW, 'case' statements can be used to substitute for grep to nice effect.

Code:

case
 someword*) #the line begins with someword: grep '^someword'
 *someword) #the line ends with someword: grep 'someword$'
 *someword*) # the line contains someword
esac

ad nauseum...
Real sed/awk/perl gurus call it bash doodoo, not voodoo -LOL.
Back to top
View user's profile Send private message 
jemimah


Joined: 26 Aug 2009
Posts: 4309
Location: Tampa, FL

PostPosted: Thu 31 Dec 2009, 15:08    Post subject:  

Thanks! This will be helpful.
Back to top
View user's profile Send private message Visit poster's website 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 31 Dec 2009, 22:40    Post subject:  

jemimah wrote:
Yeah I figured there was some Bash voodoo like that you could do. Can you recommend a good (fun!) bash programming book?


Links to Tutorials, info, one liners etc - give it a bash" jemimah
http://puppylinux.org/wikka/BashAsh

" 'give it a bash' = (might be colloquial to England) try it out / give it a go
Is this used in other parts of the world?

_________________
Puppy WIKI
Back to top
View user's profile Send private message Visit poster's website 
technosaurus


Joined: 18 May 2008
Posts: 4280

PostPosted: Fri 01 Jan 2010, 01:17    Post subject:  

Code:
sed -i "s/\([a-zA-Z]*\) \([a-zA-Z]*\)/\1-\2/g;s/-\"/ \"/g" $1


This should work if the inside quotes are flags for command line arguments that begin with a non-alpha character like --help or -verbose but may have issues for html references

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
aragon

Joined: 15 Oct 2007
Posts: 1698
Location: Germany

PostPosted: Fri 01 Jan 2010, 20:20    Post subject:  

girls and guys,

this is why i love this forum, you're not only getting an answer, no you have the possibility to learn something.

thank you all.

aragon

_________________
PUPPY SEARCH: http://wellminded.com/puppy/pupsearch.html
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [11 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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
[ Time: 0.0713s ][ Queries: 12 (0.0050s) ][ GZIP on ]