Regex with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex with spaces
# 1  
Old 03-30-2018
Regex with spaces

I have a number of files that I need to return a yes for this command.

Code:
CDATE="Feb"
 if [[ "$file" =~ [A-Z[:blank:]] && $CDATE="Feb" ]]; then echo "yes";fi

However, the files look like this:
Code:
CAR LIST DIRECTORY.TXT
CHRYSLER LIST DIRECTORY.TXT

Apparently the files are not picked up because of spaces. Can someone help? The first word only is picked up. It appears that if I do this:
Code:
file='CAR LIST DIRECTORY.TXT'

echo $file returns the correct string but I don't know how to make the regex see the string correctly. How would I be able to send a list of files with the space pattern to a variable?
# 2  
Old 03-30-2018
Wouldn't it be helpful if you posted your OS and shell versions? Above smells like bash, but this assumption can be wrong.
How is your file variable defined? In a for loop (which would explain the cited behaviour)? With a read?

BTW, you don't have problems with a "regex with spaces", but with spaces in variables / data that are incorrectly interpreted.
# 3  
Old 03-30-2018
In addition:
Code:
$CDATE="Feb"

should be:
Code:
$CDATE == "Feb"

# 4  
Old 03-31-2018
Quote:
Originally Posted by newbie2010
Code:
CDATE="Feb"
 if [[ "$file" =~ [A-Z[:blank:]] && $CDATE="Feb" ]]; then echo "yes";fi

Apparently the files are not picked up because of spaces.
I do not think so. One of the differences between [ and [[ is that [[ is a shell-built-in replacement for test and field splitting is not done inside it. To see the difference try

Code:
cd /tmp
mkdir "test dir"
DIR="test dir"
if [ -d $DIR ] ; then echo test1: $DIR exists ; fi
if [[ -d $DIR ]] ; then echo test2: $DIR exists ; fi

You will notice that one produces a syntax error because of the unquoted blank and the other works fine - because field splitting at blanks is not done.

But there is another problem:

Code:
[[ "$file" =~ [A-Z[:blank:]] ... ]]

The quotes will make sure the regexp never matches, because they are retained. Try:

Code:
[[ $file =~ [A-Z[:blank:]] ... ]]

And also the && is not the correct AND-operator for test, it is used only in the shell! So either you couple two test-statements via a shell-AND:

Code:
if [[ "$file" =~ [A-Z[:blank:]] ]] && [[ $CDATE == Feb ]] ; then

or you do it inside test with the test-internal operator:

Code:
if [[ "$file" =~ [A-Z[:blank:]] -a $CDATE == Feb ]] ; then


I hope this helps.

bakunin
# 5  
Old 03-31-2018
Quote:
Originally Posted by bakunin
I do not think so. One of the differences between [ and [[ is that [[ is a shell-built-in replacement for test and field splitting is not done inside it. [..]
To put a fine point on it: Every modern shell has [/test as a shell builtin (utility) and since it is still a utility, variables are expanded and field splitted so that they can be passed as fields to the utility.

[[ on the other hand, is a conditional expression which is part of the shell syntax and therefore no field splitting is performed (similar to what happens to variables in a case statement, which is also shell syntax)


---
On another note: RudiC was not referring to field splitting in the conditional expression, but the possibility that the variable does not contain the full filename and that that may be due to the way the filename was passed to the variable:

Quote:
Originally Posted by RudiC
How is your file variable defined? In a for loop (which would explain the cited behaviour)? With a read?

Last edited by Scrutinizer; 03-31-2018 at 07:05 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-31-2018
Actually, the test looks if there's just any single upper case char OR any whitespace in the variable's contents; so both examples should print yes, as does a a string or a 1234S2345. You may want to reconsider your problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

[BASH] Allow name with spaces (regex)

Hey all, I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them) isAlpha='^*$'However, when I bring this over to BASH it doesn't allow me to enter spaces. I use the... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

5. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

6. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. Shell Programming and Scripting

Regex

How do I write a regular expression to capture the comments of Pascal which are usually delimted by (* and *) or { and }? And also I need a regular expression to express financial quantities in American Notation. They have a leading dollar sign and an optional string of asteriks,a string of decimal... (1 Reply)
Discussion started by: saikrishnan7
1 Replies

8. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

9. Shell Programming and Scripting

Need a regex

Hi, I am trying to grep for the following type of string from a document given below: 12637 1239 3356 12956 7004 7004 7004 13381 13381 *> 12.0.1.63 0 7018 21872 ? * 208.51.134.254 53 0 3549 7018 21872 ?... (1 Reply)
Discussion started by: Legend986
1 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question