| Author |
Message |
aragon
Joined: 15 Oct 2007 Posts: 1691 Location: Germany
|
Posted: 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
|
|
 |
jemimah

Joined: 26 Aug 2009 Posts: 4309 Location: Tampa, FL
|
Posted: 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
|
|
 |
jemimah

Joined: 26 Aug 2009 Posts: 4309 Location: Tampa, FL
|
Posted: 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
|
|
 |
2byte
Joined: 09 Oct 2006 Posts: 356
|
Posted: 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
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1776
|
Posted: 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
|
|
 |
jemimah

Joined: 26 Aug 2009 Posts: 4309 Location: Tampa, FL
|
Posted: 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
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1776
|
Posted: 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
|
|
 |
jemimah

Joined: 26 Aug 2009 Posts: 4309 Location: Tampa, FL
|
Posted: Thu 31 Dec 2009, 15:08 Post subject:
|
|
Thanks! This will be helpful.
|
|
Back to top
|
|
 |
Lobster
Official Crustacean

Joined: 04 May 2005 Posts: 15109 Location: Paradox Realm
|
Posted: 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
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3845
|
Posted: 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
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
aragon
Joined: 15 Oct 2007 Posts: 1691 Location: Germany
|
Posted: 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
|
|
 |
|