String regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String regular expression
# 8  
Old 06-27-2013
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:
Code:
#!/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:
Code:
$ cat file
temp=/usr=25,/usr/lib=12
bin=12
etc=45
lib=12:6

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

# 9  
Old 06-28-2013
Hi,

Not able to get the output properly.


Code:
#!/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:
Code:
temp=/usr=25,/usr/lib=12
bin=12
etc=45
lib=12:6

output:
Code:
# 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.
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:
Code:
# sh readfile.sh
/usr /usr/lib and 12:6

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

Last edited by munna_dude; 06-28-2013 at 05:03 AM..
# 10  
Old 06-28-2013
I said associative arrays not indexed arrays:
Code:
typeset -A arr

# 11  
Old 06-28-2013
Quote:
Originally Posted by Yoda
I said associative arrays not indexed arrays:
Code:
typeset -A arr


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

Quote:
# typeset -A
-bash: typeset: -A: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
# 12  
Old 06-28-2013
This feature is available only on certain systems.

I tested this code on below system and version and it works:
Code:
$ 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:
Code:
$ bash --version
GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

# 13  
Old 06-28-2013
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.
# 14  
Old 07-03-2013
Quote:
Originally Posted by munna_dude
Hi,

Not able to get the output properly.


Code:
#!/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:
Code:
temp=/usr=25,/usr/lib=12
bin=12
etc=45
lib=12:6

output:
Code:
# 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.
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:
Code:
# sh readfile.sh
/usr /usr/lib and 12:6

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

Hi,

Observed a strange behavior from the above code.

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

Regular Expression - Switch Entire String

I would like to be able to use a regular expression to find and replace entire strings, but not replace if the string is a substring in a larger string. Example: $string = "ABC ABCDEF ABC ABCDEF ABC"; Something like - $string =~ s/ABC/XYZ/g; ->Desired: $string = "XYZ ABCDEF XYZ ABCDEF... (3 Replies)
Discussion started by: rjulich
3 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

How can awk search a string without using regular expression?

Hello, Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. just represent for "", not "A" or "a" . I don't want to add backslash . (2 Replies)
Discussion started by: 915086731
2 Replies

5. Programming

Perl regular expression to check string ending

Hi, I am trying to write a regular expression in perl to check if the string end's with "numbers-numbers" or "-numbers". I experimented something like m/\d*-\d*$/ , but this is not solving my problem. Can anyone help me in writing this expression? Well spelled titles and proper use of code... (2 Replies)
Discussion started by: successlin
2 Replies

6. Shell Programming and Scripting

Regular Expression doesn't match dot "." in a string

hello, I am writting a regular expression that intend to match any tunnel or serial interface but it doesn't mtach any serial sub-interface. For example, statement should match "Tunnel3" or "Serial0/1" but shouldn't match "Serial0\1.1" (doesn't include dot ".") I tried the following but... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

7. Shell Programming and Scripting

regular expression format string in one line.

Hi All, @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); $day=091023; $day_combine = $day; $day_combine =~ s/({2})({2})({2})/20$1-$months-$3/; Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine... (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

Help: Regular Expression for Negate Matching String

Hi guys, as per subject I am having problem with regular expressions. Example, if i got a string "javax.servlet.http.HttpServlet.service" that may occurred anywhere within a text file. How can I used the negate pattern matching of regular expression? I tried the below pattern but it... (4 Replies)
Discussion started by: DrivesMeCrazy
4 Replies

9. Shell Programming and Scripting

validate a string against a regular expression

Hi there i have a script which will create unix user accounts. Id like to validate the entered string so that it is specifically 8 characters or less and consists of only ! not Is there a way to validate a string against a regular expression.. i.e size=`printf "$var | wc -m` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

10. UNIX for Dummies Questions & Answers

Regular Expression - match 'b' that follows 'a' and is at the end of a string

Hi, I'm struggling with a regex that would match a 'b' that follows an 'a' and is at the end of a string of non-white characters. For example: Line 1: aba abab b abb aab bab baa I can find the right strings but I'm lacking knowledge of how to "discard" the bits that precede bs.... (2 Replies)
Discussion started by: machinogodzilla
2 Replies
Login or Register to Ask a Question