Shell script regex help: accept only 3 file extensions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script regex help: accept only 3 file extensions
# 1  
Old 11-12-2009
Question Shell script regex help: accept only 3 file extensions

This regex is supposed to accept files with extensions 270, 276, and "txt" only. Everything else should be discarded.

This is what I have. I'll spare you the rest of the code.
Code:
[..]
 ext =".[(27(0|6).txt)]\$"
 #ext =".[(27|0|6|.txt)]\$"
 #ext =".[27|0|6|.|txt]\$"
 #ext =".[27(0|6)|.txt]\$"

[..]
   for xfile in `ls $dir | grep "$ext" | xargs`; do
 [..]

The problem with my regex is that is accepting more than those 3 file extensions.

Any help is appreciated.
# 2  
Old 11-12-2009
This type of construct works for me, as an example:

Code:
me@www:/tmp# ls | egrep '27(0|6)\.txt$'
270.txt
276.txt

Edit: Nevermind... seems the poster wants only extensions \.270$ \.276$ and \.txt$ and not .*270\.txt etc... thanks Scrutinizer
# 3  
Old 11-12-2009
Hi,
Code:
egrep "\.(27[06]|txt)$"

should work
# 4  
Old 11-12-2009
thnx guys, I appreciate it.

now for one of the if-statements I have down the code...

what type of construct would help me get only these 4 file ext variants?
*.270
*.276
*.270.txt
*.276.txt

I'm trying this regex
Code:
ext="27[(0|6)|.txt]\$"; 
#ext = "(27[(0|6)]) | [.txt]\$";


but for some reason it doesn't accept:
270.txt
276.txt
# 5  
Old 11-12-2009
Code:
egrep "\.27[06](|\.txt)$"

-or-
Code:
egrep "\.27[06](\.txt){0,1}$"

should do the trick...

Last edited by Scrutinizer; 11-12-2009 at 05:36 PM..
# 6  
Old 11-12-2009
MySQL

Quote:
Originally Posted by Scrutinizer
[code]
Code:
egrep "\.27[06](\.txt){0,1}$"

should do the trick...
right on!! thnx, that one did it. It works like a charm.
# 7  
Old 11-12-2009
Code:
for xfile in $dir/*.27{0,6}{,.txt}; do
...

don't use ls to feed a for loop
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accept data from file or stdin in script

I have a script that looks like this:sed -f myfile.sed $1 > $1.out called myscript and would like to change it so the parameter isn't necessary: ls *.idx | myscript | xargs some_command What do I need to add so it can run either way? TIA ---------- Post updated at 09:41 AM ----------... (1 Reply)
Discussion started by: wbport
1 Replies

2. Shell Programming and Scripting

Add file extensions to files EXCEPT the script that is issuing

Greetings all, On a RedHat System - I am issuing a command from script.sh that will add a file extension to a listing of files in a directory. It works, but I need to script from having an extension added as well. Here is what I have tried to no luck: for file in `ls * | awk ' /\./{print... (6 Replies)
Discussion started by: jeffs42885
6 Replies

3. Shell Programming and Scripting

Shell script to accept user input on the fly

I want a shell script that accepts user input simultaneously when performing other tasks. Example: A shell script should echo some messages on the console and when the user presses some keys it should respond to that action. say, when user presses the key A - more information should be printed... (2 Replies)
Discussion started by: Arun_Linux
2 Replies

4. Shell Programming and Scripting

Need regex shell script to remove text from file

Hello I am trying to remove a line like <?php /*versio:2.05*/if (!defined('determinator')){ content goes here}?> Now i want to scan all... (6 Replies)
Discussion started by: devp
6 Replies

5. Shell Programming and Scripting

How to write a shell script to automatically accept return key with out user intervention?

Hi Friends, i am creating a shell script which is accepting file name as input parameter from Java and invoking finacle service. The service will accpet text file,B2k_session id,etc and upload the text file data in finacle database. My shell script looks like this:- #! /bin/ksh... (2 Replies)
Discussion started by: vadlamudy
2 Replies

6. Shell Programming and Scripting

How to accept arguments in shell script when calling in perl

I have a shell script like this: #!/bin/sh $PYTHON MetarDecoder.py < ../data/mtrs/arg1/arg2 And I'm calling it with this in perl: my $output = `./metar_parse.sh --options`; It's successful when I put in actual values for arg1 and arg2 in the shell script, but I'd like to pass arguments... (1 Reply)
Discussion started by: civilsurfer
1 Replies

7. Shell Programming and Scripting

SQL PLUS Command 'ACCEPT' is not waiting for user input with sh shell script

Dear All, The sqlplus 'Accept' command is not waiting for user input when I include the command within a shell script. Note: The 'Accept' command is working fine if I execute it in a SQLPLUS Prompt. Please fins the below sample script which i tried. SCRIPT: -------- #!... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies
Login or Register to Ask a Question