using regular expression in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using regular expression in for loop
# 1  
Old 03-19-2010
using regular expression in for loop

Hi,

i want to use regular expression in a for loop like this.

Code:
for var in "[1-3][0-6]/Mar/2010"
do
echo "Usage for the date $var"
-----and want to grep $var partten from a file--------
done

and it is not working.

I am new to shell scripting, any one has any idea how to do it.

Last edited by pludi; 03-19-2010 at 10:28 AM..
# 2  
Old 03-19-2010
1.: Put your code in code tags (select it and use the '#' button).
2.: What isn't working? error? output?
2.: What is the desired effect of your script?
3.: What values do you want to give to your $var?
# 3  
Old 03-19-2010
Code:
for var in "[0-3][1-9]/Mar/2010"
do
echo "Usage for the date $var"
-----and want to grep $var partten from a file--------
done

here wat i am expecting is, this string "[0-3][1-9]/Mar/2010" give me string from 01/Mar/2010-30/Mar/2010 in the for loop.

and this regular expression is not giving the desired result.

Any suggestion ???
# 4  
Old 03-19-2010
If it should work it would give 01 to 39...
if you have seq you can
Code:
for i in $(seq -w 30)
do
    var="$i/Mar/2010"
    echo "Usage for the date $var"
done

# 5  
Old 03-19-2010
Or you can use the while loop.
In my Bash shell, for example -

Code:
$
$
$ day=1
$ while [ $day -le 31 ]; do
>   var=`printf "%02d/Mar/2010\n" $day`
>   echo "Usage for the date: $var"
>   day=`expr $day + 1`
> done
Usage for the date: 01/Mar/2010
Usage for the date: 02/Mar/2010
Usage for the date: 03/Mar/2010
Usage for the date: 04/Mar/2010
Usage for the date: 05/Mar/2010
Usage for the date: 06/Mar/2010
Usage for the date: 07/Mar/2010
Usage for the date: 08/Mar/2010
Usage for the date: 09/Mar/2010
Usage for the date: 10/Mar/2010
Usage for the date: 11/Mar/2010
Usage for the date: 12/Mar/2010
Usage for the date: 13/Mar/2010
Usage for the date: 14/Mar/2010
Usage for the date: 15/Mar/2010
Usage for the date: 16/Mar/2010
Usage for the date: 17/Mar/2010
Usage for the date: 18/Mar/2010
Usage for the date: 19/Mar/2010
Usage for the date: 20/Mar/2010
Usage for the date: 21/Mar/2010
Usage for the date: 22/Mar/2010
Usage for the date: 23/Mar/2010
Usage for the date: 24/Mar/2010
Usage for the date: 25/Mar/2010
Usage for the date: 26/Mar/2010
Usage for the date: 27/Mar/2010
Usage for the date: 28/Mar/2010
Usage for the date: 29/Mar/2010
Usage for the date: 30/Mar/2010
Usage for the date: 31/Mar/2010
$
$

tyler_durden
# 6  
Old 03-19-2010
Learning the shell can be a bit eccentric sometimes ;-}

I >>think<< that you are confusing a few basic concepts.

Bash regular expressions would be more likely used in string manipulation.

You can read extensively about it here:

Manipulating Strings

Reading your code, it looks suspiciously like what you are trying to use is brace expansion of this type:

Code:
$ for var in {0..3}{1..9}; do echo -n "$var "; done
01 02 03 04 05 06 07 08 09 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39

Double braces expansion is similar to a nested loop. This is not a great shortcuts for dates, however, since there are different number of days per month.

Single brace expansion is a great shortcut for this though:

Code:
$ # First day of every month in 2010:
$ for var in {1..12}; do echo -n "$var/1/2010 "; done 
1/1/2010 2/1/2010 3/1/2010 4/1/2010 5/1/2010 6/1/2010 7/1/2010 8/1/2010 9/1/2010 10/1/2010 11/1/2010 12/1/2010

or this:

Code:
$ # Every day in March 2010 (US Style...)
$ for var in {1..31}; do echo "3/$var/2010 "; done
3/1/2010 
3/2/2010 
3/3/2010 
3/4/2010 
3/5/2010 
3/6/2010 
3/7/2010 
3/8/2010 
3/9/2010 
3/10/2010 
3/11/2010 
3/12/2010 
3/13/2010 
3/14/2010 
3/15/2010 
3/16/2010 
3/17/2010 
3/18/2010 
3/19/2010 
3/20/2010 
3/21/2010 
3/22/2010 
3/23/2010 
3/24/2010 
3/25/2010 
3/26/2010 
3/27/2010 
3/28/2010 
3/29/2010 
3/30/2010 
3/31/2010


Last edited by drewk; 03-19-2010 at 03:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Fedora

Use of regular expression

Hi, I need some help. My task is, to write a "one-line" command, which must use ls and awk. Task: Write a command-line, which should rename all files in dir from form "value1.dok" to "value2.doc". And value2=value1+1. For example: ls | awk -F: '{print "mv "$0" "$1+1".doc"}' | sh But... (3 Replies)
Discussion started by: John_Light
3 Replies

2. Shell Programming and Scripting

Need help on regular expression

Hi , I am trying to write a mod_header module rule which will look a specific url (https://partner.testing.com) and rewrite it. The header line is given below. where the url comes in between of the line. i know ^ expression can be used for match the beginning of the line. but not sure how to... (3 Replies)
Discussion started by: arumon
3 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. 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

5. Shell Programming and Scripting

help in regular expression

<ATTR name="ABCDEFGH" value=""/> <ATTR name="HJYR" value=""/> what would be the regular expression to match both the above strings... Always end with value=""/> always start with <ATTR name=" the ATTR name can be anything.. I need to use this with match() in awk. Thanks.. (1 Reply)
Discussion started by: shekhar2010us
1 Replies

6. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

7. Shell Programming and Scripting

regular expression [^ ]

Hi, Could anybody explain why will evaluate to true for a string that is not null, not empty string, and has at least one non-space char? My understanding is that ^ means exclude all chars inside . So I thought it should mean anything except space. This seems a big mystery to me. (9 Replies)
Discussion started by: iengca
9 Replies

8. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

9. Shell Programming and Scripting

regular expression help

hello all.. I'm a bit new to this site.. and I hope to learn alot.. but I've been having a hard time figuring this out. I'm horrible with regular expressions.. so any help would be greatly appreciated. I have a file with a list of names like this: LASTNAME, FIRSTNAME, MIDDLEINITIAL how can... (5 Replies)
Discussion started by: mac2118
5 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question