Author Topic: [SOLVED] Bash - extract and rename words from a variable  (Read 1474 times)

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10623
  • MLUs Forever!
[SOLVED] Bash - extract and rename words from a variable
« on: October 06, 2011, 05:15:43 PM »
Hi, I am back again :D

I will try to explain as best I can but please ask for clarification if it will help.

A variable called $WORDS contains words separated by a space (or other separator if I wish).
The contents will be One Two Three Four ......  or any number of them can be missing ....  so the contents might be Two Four or One Four or One Two Four or .....  so on

I want to extract whatever words are present and rename them as variables for use elsewhere.
So
One=$One
if it exists in the $Words variable, and so on.

I would prefer to use a Bash method .....  not good with syntax for the likes of Sed or Awk  :(

Maybe substitution would be the best method? using something like  echo ${BOOT%% *} for the first word .... or a small 'for i in etc' loop ..... ?

for i in $WORDS; do echo "$"$i;  done  (EDIT)

Your ideas would be appreciated.

Thank you.

EDIT:
           It just occurred to me that I have no need to extract the words from the variable, if they could be hacked while within it ....  so that

One Two Three Four

became

$One
$Two
$Three
$Four


..... replacing the separator with a line feed (or whatever its called) and adding the  $  before each word present.

That might be a neater way ......  if anyone has ideas   :D

regards
« Last Edit: October 07, 2011, 07:14:20 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: Bash - extract and rename words from a variable
« Reply #1 on: October 07, 2011, 01:05:09 AM »
Example bash, using positional parameters:
Code: [Select]
WORDS="One Two Three Four"
set $WORDS
echo "this is the 1st paramenter: $1"
echo "this is the 2nd parameter: $2"
echo "this is the 3rd parameter: $3"
echo "this is the 4th parameter: $4"

when run will produce the following output:
Quote
this is the 1st parameter: One
this is the 2nd parameter: Two
this is the 3rd parameter: Three
this is the 4th parameter: Four

Code: [Select]
WORDS="One Two Three Four"
set $WORDS
echo -e "\$$1\n\$$2\n\$$3\n\$$4"
the above will produce (note the backslash before the '$' to prevent bash interpret '$$':
Quote
$One
$Two
$Three
$Four

not neater, ... but different  :D

AS



Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10623
  • MLUs Forever!
Re: Bash - extract and rename words from a variable
« Reply #2 on: October 07, 2011, 05:04:18 AM »
Thanks AS  :D

Late last night I tried a different approach but of course got myself into a muddle again  ;D

I will post a small script below to show what is being attempted ......  I want a user to be able to choose any or all of (for instance) four variables.
What is assigned to those variables is determined elsewhere, but I have assigned digits to the variables in the example below for clarity.

In the first part of the script I get the expected outcome, but when Zenity is used it appears that the contents of WORDS are no longer variables - or at least do not appear to be so.

Maybe there is some extra syntax I can use in the Zenity part to get the desired result?

This is now the main focus, as I believe the simplest (for me to understand) method is being employed ...  but maybe it is causing problems?

Any thoughts or corrections appreciated. ;)

regards

Code: [Select]
#!/bin/bash
set -x
One=1
Two=2
Three=3
Four=4

WORDS="$One $Two $Three $Four"

# This gives the outout 1 2 3 4  as required
for i in $WORDS; do echo $i;  done
echo ""
echo ""
echo ""
##################
# Now to try to get a user choice of what words should contain
WORDS=$(zenity --window-icon=$ICON --list --title="    $TITLE    " --text=$"                    <b>Select the options you wish to Include</b>    \n    Please select the Options which you wish to include from the list below   
" --checklist --hide-column=2 --multiple --separator=" " --height=320 --width=350 --column $"Select" --column $" Option " --column $"            - Option Detail -        " TRUE  "$"One     $"First Option.
" FALSE "$"Two     $"Second option.
" FALSE "$"Three   $"Third Option.
" FALSE "$"Four    $"Fourth Option.
")
echo ""
echo "WORDS contains " "$WORDS"
echo ""
echo $One
echo $Two
echo $Three
echo $Four
echo ""
for i in $WORDS; do echo $i;  done

exit 0
MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10623
  • MLUs Forever!
Re: Bash - extract and rename words from a variable
« Reply #3 on: October 07, 2011, 05:15:35 AM »
Dang!   I think I spotted it!  ........  the variables are not properly specified in the Zenity part -----  a left over from my late night last night I guess.

Have some testing to do and will report back ....
MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10623
  • MLUs Forever!
Re: Bash - extract and rename words from a variable
« Reply #4 on: October 07, 2011, 07:13:59 AM »
Problem worked out .....  was in the Zenity part above .....  may not be the method originally intended, but it works ---- so far  :D

Thanks for the assist ;)

regards.
MLUs rule the roost!

Linux XPS 3.2.18-pclos2.pae.bfs  32 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: Bash - extract and rename words from a variable
« Reply #5 on: October 07, 2011, 07:17:33 AM »
please check also this one, IMHO it's unrelated to zenity, unless you decided to pass to zenity the values of the vars ...

Quote
#!/bin/bash
set -x
One=1
Two=2
Three=3
Four=4

WORDS="$One $Two $Three $Four"

# This gives the outout 1 2 3 4  as required
for i in $WORDS; do echo $i;  done
echo ""
echo ""
echo ""
##################
# Now to try to get a user choice of what words should contain
WORDS=$(zenity --window-icon=$ICON --list --title="    $TITLE    " --text=$"                    Select the options you wish to Include    \n    Please select the Options which you wish to include from the list below
" --checklist --hide-column=2 --multiple --separator=" " --height=320 --width=350 --column $"Select" --column $" Option " --column $"            - Option Detail -        " TRUE  "$"One     $"First Option.
" FALSE "$"Two     $"Second option.
" FALSE "$"Three   $"Third Option.
" FALSE "$"Four    $"Fourth Option.
")
echo ""
echo "WORDS contains " "$WORDS"
echo ""
echo $One
echo $Two
echo $Three
echo $Four
echo ""
for i in $WORDS;
do
        v=`eval echo $i`
        echo $v;
done

exit 0