Substitute chars in part of a line

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

Substitute chars in part of a line

#1 Post by aragon »

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

here is an example line

Code: Select all

Calcoo scientific calculator "calcoo"
the result should be

Code: Select all

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: Select all

#Calcoo scientific calculator# "calcoo"
any help?

thanks in advance
aragon

User avatar
jemimah
Posts: 4307
Joined: Wed 26 Aug 2009, 19:56
Location: Tampa, FL
Contact:

#2 Post by jemimah »

Code: Select all

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

User avatar
jemimah
Posts: 4307
Joined: Wed 26 Aug 2009, 19:56
Location: Tampa, FL
Contact:

#3 Post by jemimah »

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

Code: Select all

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

2byte
Posts: 353
Joined: Mon 09 Oct 2006, 18:10

#4 Post by 2byte »

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: Select all

#!/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 


amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#5 Post by amigo »

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: Select all

#!/bin/bash
STRING='Calcoo scientific calculator "calcoo"'
PART1=${STRING%% \"*} PART2=${STRING#*\"} ; echo ${PART1// /-} \"$PART2

User avatar
jemimah
Posts: 4307
Joined: Wed 26 Aug 2009, 19:56
Location: Tampa, FL
Contact:

#6 Post by jemimah »

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

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#7 Post by amigo »

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/dis ... /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: Select all

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.

User avatar
jemimah
Posts: 4307
Joined: Wed 26 Aug 2009, 19:56
Location: Tampa, FL
Contact:

#8 Post by jemimah »

Thanks! This will be helpful.

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#9 Post by Lobster »

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 Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#10 Post by technosaurus »

Code: Select all

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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

#11 Post by aragon »

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

Post Reply