The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regular expression help Bob565656 Shell Programming and Scripting 6 09-09-2008 02:32 PM
regular expression ag79 Shell Programming and Scripting 3 03-19-2008 07:10 AM
awk and regular expression maskot Shell Programming and Scripting 4 05-22-2007 07:22 AM
help in regular expression Rakesh Ranjan High Level Programming 5 10-25-2006 01:00 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-24-2009
blondie53403 blondie53403 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 10
Regular Expression on Directory Contents

This should be an easy question for you gurus.

How can I create a regular expression to match all files in the current directory that have only one period in their file name, and also not contain the string "abc" before the period?

It would match:
foo.txt
foobar.log

It would not match:
foo.bar.txt
abc.txt

I'm going to use this inside an if-statement in a ksh script, if that makes any difference.
  #2 (permalink)  
Old 02-24-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
Code:
ls | awk -F\. 'NF==2'
Regards
  #3 (permalink)  
Old 02-24-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Franklin52 View Post
Code:
ls | awk -F\. 'NF==2'

That doesn't remove filenames containing abc..

(And there's no need for ls.)

Code:
printf "%s\n" * | awk -F\. 'NF==2 && !/abc\./'
That will not pick up filenames that begin with a dot.
  #4 (permalink)  
Old 02-24-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
Quote:
Originally Posted by cfajohnson View Post

That doesn't remove filenames containing abc..

Oops, I missed that.

Regards
  #5 (permalink)  
Old 02-24-2009
blondie53403 blondie53403 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 10
That works wonderfully, thanks! In case anyone cares, here's the final code (with private details omitted), once put inside a for-loop and if-statement. Maybe not the best way, but it works...

Code:
for file in *; do
  if [[ `echo $file | awk -F\. 'NF==2 && !/abc\./' | wc -l` -ge 1 ]]; then
    #do something
  fi
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0