How to store regular expression in a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store regular expression in a variable?
# 1  
Old 08-14-2015
How to store regular expression in a variable?

Hi,

Im facing a weird behavior storing regular expression in a vaiable.

Let us consider
Code:
>file_name=CTP02_[0-9]*.tar

when I echo the above variable its displaying below which is file
Code:
CTP02_[0-9]*tar

but when I create a file with name CTP02_1234.tar, and when I echo $file_name its showing CTP02_1234.tar not CTP02_[0-9]*tar.gz Why this issue ? How to overcome ? I need to need to use this in grep

My scenario is, to find the number of files which are of type CTP02_[0-9]*.tar in a folder.

Appreciate your help

Thanks

Last edited by Don Cragun; 08-14-2015 at 05:39 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-14-2015
CTP02_[0-9]*.tar is not a regular expression, it is a glob.
And it gets interpreted and expanded before echo shows.


Code:
[aia@localhost rdineshredy]$ ls
[aia@localhost rdineshredy]$ touch CTP02_{0..9}.something.tar
[aia@localhost rdineshredy]$ ls
CTP02_0.something.tar  CTP02_3.something.tar  CTP02_6.something.tar  CTP02_9.something.tar
CTP02_1.something.tar  CTP02_4.something.tar  CTP02_7.something.tar
CTP02_2.something.tar  CTP02_5.something.tar  CTP02_8.something.tar
[aia@localhost rdineshredy]$ tar_files="CTP02_[0-9]*.tar"
[aia@localhost rdineshredy]$ echo $tar_files
CTP02_0.something.tar CTP02_1.something.tar CTP02_2.something.tar CTP02_3.something.tar CTP02_4.something.tar CTP02_5.something.tar CTP02_6.something.tar CTP02_7.something.tar CTP02_8.something.tar CTP02_9.something.tar
[aia@localhost rdineshredy]$ rm -f CTP02_*
[aia@localhost rdineshredy]$ ls
[aia@localhost rdineshredy]$ echo $tar_files
CTP02_[0-9]*.tar
[aia@localhost rdineshredy]$

# 3  
Old 08-14-2015
And the fix is
Code:
echo "$file_name"

# 4  
Old 08-14-2015
Note that you don't grep a folder (usually called a directory in UNIX and Linux environments). The grep utility searches for text in the contents of text files (not directories on most systems).

If you're trying to count the number of files in a directory that have names matching the globbing pattern stored in your shell variable named (confusingly) file_name, you could try something like:
Code:
ls $file_name | wc -l

which should work as long as there aren't any newline characters in your file names. If you have users who create filenames containing newline characters, or if you just want to use shell built-ins, a fast way to get what you want is:
Code:
set -- $file_name
echo $#

(assuming that you aren't using command line arguments or have already gathered what you need from them, and assuming that at least one file matching your pattern exists) or, if there might not be any matching files (but there also might be a file with a name that is your pattern):
Code:
set -- $file_name
if [ "$file_name" = "$1" ] && [ ! -e "$1" ]
then	echo 0
else	echo $#
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression as a variable

I'm trying to use a series of regular expressions as variables but can't get it to behave properly. You can see below what I'm trying to do. Here with lowercase a-z and the same with uppercase, numbers 0-9 and again with a set of special characters, without having to type out every single... (3 Replies)
Discussion started by: 3therk1ll
3 Replies

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

3. UNIX for Dummies Questions & Answers

grep with variable and regular expression

i have a command line like this in csh script grep -i "$argv$" which i wanted to select the line ending with string provided as argument but it couldn't interpret the '$' (ending with).. any help? (3 Replies)
Discussion started by: ymc1g11
3 Replies

4. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

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

6. Shell Programming and Scripting

Passing Variable to Regular Expression

Hi All, Below is a sample code: print "Enter the Name: "; my $Name = <>; print "Word is $Name"; open (FH,"AIDNameList.txt"); while (<FH>) { my $line; print "Word is $Name"; for $line(<FH>)... (12 Replies)
Discussion started by: jisha
12 Replies

7. Shell Programming and Scripting

AWK - compare $0 to regular expression + variable

Hi, I have this script: awk -v va=45 '$0~va{print}' flo2 That returns: "4526745 1234 " (this is the only line of the file "flo2". However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That... (3 Replies)
Discussion started by: jolecanard
3 Replies

8. Shell Programming and Scripting

Awk's variable in regular expression

Anyone know how I will use awk's variable in a regular expression? This line of code of mine is working, the value PREMS should be a variable: awk '$1 ~ /PREMS/ { if(length(appldata)+2 >= length($1)) print $0; }' appldata=$APPLDATA /tmp/file.tmp The value of APPLDATA variable is PREMS. ... (2 Replies)
Discussion started by: Orbix
2 Replies

9. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies

10. Shell Programming and Scripting

regular expression using a variable

hello, I use AIX with ISM PILOT, I want to match something with a varible like this : $variable = 10 #this variable is the number of the job "$variable STARTED" # the pattern how can use this variable to match it with the word STARTED Tanks (0 Replies)
Discussion started by: barribar
0 Replies
Login or Register to Ask a Question