LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2012, 09:32 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
matching a postix regex in bash


I am trying to teach myself the postix regex in bash
If I ls this directory, I do have a few instances of casper1234, casper4325, etc.
For eachone it finds i want it to say horay.

Code:
     1  #!/bin/bash
     2
     3  regex='\bcasper[[:alpha:]]{4}'
     4
     5  for i in $(ls -ltr /var/ftp/)
     6  do
     7  if [ $i =~ $regex ] ; then
     8  echo "horay"
     9  sleep 1
    10  fi
    11  done
    12
error is:

Code:
 
./get_postfix: line 7: [: =~: binary operator expected
./get_postfix: line 7: [: =~: binary operator expected
./get_postfix: line 7: [: =~: binary operator expected
./get_postfix: line 7: [: =~: binary operator expected
./get_postfix: line 7: [: =~: binary operator expected
 
Old 05-29-2012, 11:33 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What is the error message if you change from [ ... ] to [[ ... ]] ?

EDIT: it might also be informative to print $i before the if test.

Last edited by catkin; 05-29-2012 at 11:42 PM.
 
Old 05-29-2012, 11:36 PM   #3
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
Try double brackets around your test expression to prevent the regex from being expanded by the shell.

EDIT: beaten by minutes...
 
Old 05-30-2012, 12:46 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by casperdaghost View Post
Code:
for i in $(ls -ltr /var/ftp/)
Do not use ls this way to obtain file lists; the output varies by locale. If you are only using it to produce interesting strings for testing, then that is okay, but if you intend to parse the output in any way, DON'T.

If you want a list of files in a directory (or directory tree) sorted by last modification time, oldest last, use something like the following in Bash:
Code:
export LANG=C LC_ALL=C
IFS=$'\t'
while read -rd "" year month day hour min sec filename ; do
    # To get integer seconds, use "${sec%%.*}" instead of "$sec".

    echo "$year-$month-$day+$hour:$min:$sec" "$filename"

done < <( find DIR(s) -maxdepth 1 -type f -printf '%TY\t%Tm\t%Td\t%TH\t%TM\t%TS\t%p\0' | sort -rz )
If you want files in subdirectories too, just drop the -maxdepth 1 option. To get oldest first, use sort -z . If you want the times in UTC instead of local time, add export TZ=UTC at the start. You can also access other metadata on the files, like owner, group, size, access mode and so on; see find format directives for details.

This construct uses ASCII NULs as separators. It means all possible file names will work correctly; even the truly strange ones that have newlines or whatnot. Using the C locale as shown, this is guaranteed to handle all file names correctly in Linux.
 
Old 05-30-2012, 03:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
SO I see a few problems with your initial attempt:

1. As NA pointed out, please don't use ls (reasons here)

2. Regex operator (=~) requires the use of [[]]

3. You say you have files of the format casper1234, yet your regex says:
Code:
regex='\bcasper[[:alpha:]]{4}'
Here you are looking for casper followed by 4 alpha characters. This would mean it will never match any of your examples.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pattern matching in a bash case statement using regex ciphyre Programming 1 01-31-2009 12:20 PM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 03:05 PM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM
regex matching things like å õ í ë ã è, etc. aunquarra Programming 2 05-04-2005 07:53 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration