String regular expression

Hi,

temp="/usr=25,/usr/lib=12"

How to get only dir names with out values.

I tried like below but no use.

tmp=${temp##*,}
echo $tmp

o/p:

/usr/lib=12

expected o/p:
/usr /usr/lib ---> in array

In bash, try:

IFS=, read -a ARR <<< "$temp"
printf "%s\n" "${ARR[@]%=*}"

In awk you could do:

awk -F\= '{print $1}' INfile >> OUTfile

I don't understand how this awk code is going to help solve OP's problem.

Another approach to get required values into an indexed array would be:

temp="/usr=25,/usr/lib=12"

typeset -a arr
typeset -i idx

for key in ${temp/,/ }
do
        arr[$idx]="${key/=*/}"
        (( idx++ ))
done

printf "%s\n" "${arr[@]}"

Hi,

Thanks for the solution.

When I try to apply the code to read a file. I am not getting the proper output.

readfile

temp=/usr=25,/usr/lib=12
bin=12 
etc=45 
lib=12:6

output should be

echo $temp
/usr /usr/lib

echo $bin
12

echo $etc
45

echo $lib
12:6

Can we get like above.

Those are different requirements than what is in post #1. Please specify more clearly what it is you are trying to achieve..

Hi,

I tried this logic to read a file and get t he values but no use.

function read_params()
{
    until [ -z "$1" ]
    do
      if [ `expr index "$1" =` != 0 ] ; then
          tmp=$1

          typeset -a arr
          typeset -i idx
          parameter=${tmp%%=*}
          for key in ${tmp/,/ }
          do
             arr[$idx]="${key/=*/}"
             value="${arr[@]}"
             (( idx++ ))
             eval $parameter=\"$value\"
           done
      fi
      shift
    done
}

function main()
{
read_params `cat readfile.txt`

echo $temp
echo $bin
echo $lib
}

main

readfile.txt

temp=/usr=25,/usr/lib=12
bin=12 
etc=45 
lib=12:6

output:

/usr /usr/lib
12
12:6

please let me know what went wrong in my code

---------- Post updated at 02:45 AM ---------- Previous update was at 01:33 AM ----------

Hi,

Please find the above post for more details.

Please always state your problem clearly. By changing the requirement you are simply wasting the time of the member who tried to help you.

I would suggest using Associative Arrays for this requirement:

#!/bin/bash

typeset -A arr

while read line
do
        var="${line%%=*}"
        for key in ${line//,/ }
        do
                val="${key#${var}=}"
                val="${val%=*}"
                if [ ! -z arr["$var"] ]
                then
                        arr["$var"]="${arr["$var"]} $val"
                else
                        arr["$var"]="$val"
                fi
        done
done < file

for k in "${!arr[@]}"
do
        printf "%s %s\n" "$k" "${arr["$k"]}"
done

Input:

$ cat file
temp=/usr=25,/usr/lib=12
bin=12
etc=45
lib=12:6

Output:

$ ./script.bash
bin  12
etc  45
lib  12:6
temp  /usr /usr/lib

Hi,

Not able to get the output properly.

#!/bin/bash

typeset -a arr

while read line
do
        var="${line%%=*}"
        for key in ${line//,/ }
        do
                val="${key#${var}=}"
                val="${val%=*}"
                if [ ! -z arr["$var"] ]
                then
                        arr["$var"]="${arr["$var"]} $val"
                else
                        arr["$var"]="$val"
                fi
        done
done < file

for k in "${!arr[@]}"
do
        printf "%s %s\n" "$k" "${arr["$k"]}"
done

file:

temp=/usr=25,/usr/lib=12
bin=12
etc=45
lib=12:6

output:

# sh readfile.sh
0  /usr /usr/lib 12 45 12:6

Can we write the code in multilple function like below and need to use "eval" command to get the parameter name and respective value from the main function.
Overall I have to get the correct output for the below code.

#! /bin/sh
typeset -a arr

read_params()
{
#while read line
until [ -z "$1" ]
do
    if [ `expr index "$1" =` != 0 ] ; then
        var="${1%%=*}"
        for key in ${1//,/ }
        do
                val="${key#${var}=}"
                val="${val%=*}"
                if [ ! -z arr["$var"] ]
                then
                        arr["$var"]="${arr["$var"]} $val"
                else
                        arr["$var"]="$val"
                fi
        done
            eval $var=\"${arr["$var"]}\"
    fi
shift
done
#done < $1

for k in "${!arr[@]}"
do
        printf "%s %s\n" "$k" "${arr["$k"]}"
done

}

main()
{
read_params `cat file`

echo "$temp  and $lib"

}

main

Expected output:

# sh readfile.sh
/usr /usr/lib and 12:6

Please let me know what would be the mistake I did in the above code.

I said associative arrays not indexed arrays:

typeset -A arr

Correct but the below error caused to modified it as "-a".

This feature is available only on certain systems.

I tested this code on below system and version and it works:

$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

But it will not work on below system and version:

$ bash --version
GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

Bash 3 does not have associative arrays. That is a bash 4 feature. If it happens to work in a particular version of bash 3 it probably is an undocumented feature and should not be relied upon.

As an alternative one could use ksh93 instead, which is a fast and advanced shell that also knows associative arrays.

Hi,

Observed a strange behavior from the above code.

arr["$var"]="${arr["$var"]} $val"

above snippet will have the space in front of the text.
It requires if the string having two variable(Ex: temp=/usr=25,/usr/lib=12) but it is not required when the string contains only a variable(Ex: etc=45).

value=/bin/$etc
resulting value="/bin/ 45" it should be value="/bin/45".

The space has to avoid when we do "eval"
eval $var=\"${arr["$var"]}\"

Is this possible, please let me know the way.