Sponsored Content
Top Forums Shell Programming and Scripting using regular expression in for loop Post 302405689 by drewk on Friday 19th of March 2010 02:09:18 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
REGEX(3)						     Library Functions Manual							  REGEX(3)

NAME
re_comp, re_exec - regular expression handler SYNOPSIS
char *re_comp(s) char *s; re_exec(s) char *s; DESCRIPTION
Re_comp compiles a string into an internal form suitable for pattern matching. Re_exec checks the argument string against the last string passed to re_comp. Re_comp returns 0 if the string s was compiled successfully; otherwise a string containing an error message is returned. If re_comp is passed 0 or a null string, it returns without changing the currently compiled regular expression. Re_exec returns 1 if the string s matches the last compiled regular expression, 0 if the string s failed to match the last compiled regular expression, and -1 if the compiled regular expression was invalid (indicating an internal error). The strings passed to both re_comp and re_exec may have trailing or embedded newline characters; they are terminated by nulls. The regular expressions recognized are described in the manual entry for ed(1), given the above difference. SEE ALSO
ed(1), ex(1), egrep(1), fgrep(1), grep(1) DIAGNOSTICS
Re_exec returns -1 for an internal error. Re_comp returns one of the following strings if an error occurs: No previous regular expression, Regular expression too long, unmatched (, missing ], too many () pairs, unmatched ). 3rd Berkeley Distribution May 15, 1985 REGEX(3)
All times are GMT -4. The time now is 02:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy