String regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String regular expression
# 1  
Old 06-27-2013
String regular expression

Hi,

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

How to get only dir names with out values.

I tried like below but no use.

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

o/p:
Code:
/usr/lib=12


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

Last edited by Scrutinizer; 06-27-2013 at 01:19 PM.. Reason: code tags
# 2  
Old 06-27-2013
In bash, try:
Code:
IFS=, read -a ARR <<< "$temp"
printf "%s\n" "${ARR[@]%=*}"

# 3  
Old 06-27-2013
In awk you could do:

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

# 4  
Old 06-27-2013
Quote:
Originally Posted by dagamier
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:
Code:
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[@]}"

# 5  
Old 06-27-2013
Quote:
Originally Posted by Scrutinizer
In bash, try:
Code:
IFS=, read -a ARR <<< "$temp"
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
Code:
temp=/usr=25,/usr/lib=12
bin=12 
etc=45 
lib=12:6

output should be
Code:
echo $temp
/usr /usr/lib

echo $bin
12

echo $etc
45

echo $lib
12:6

Can we get like above.
# 6  
Old 06-27-2013
Those are different requirements than what is in post #1. Please specify more clearly what it is you are trying to achieve..
# 7  
Old 06-27-2013
Quote:
Originally Posted by Yoda
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:
Code:
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,

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

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

output:
Code:
/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 ----------

Quote:
Originally Posted by Scrutinizer
Those are different requirements than what is in post #1. Please specify more clearly what it is you are trying to achieve..
Hi,

Please find the above post for more details.
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