Using regex in find command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using regex in find command
# 22  
Old 04-21-2011
Hey, it doesn't work in Bash, either. =P I think csh is simpler. It even allows arguments, which I abuse, for aliases without needing to use a script.
# 23  
Old 04-21-2011
Quote:
Originally Posted by Zucriy Amsuna
Hey, it doesn't work in Bash, either. =P
What, exactly, didn't work in bash? No reason it shouldn't be possible in it -- like any proper bourne shell, it's fully recursive, hence doesn't have mysterious corners where normal expressions don't work.
Quote:
I think csh is simpler.
It's not, really -- it just looks like it. When you start doing anything more complicated than "echo line", you find places it acts weird -- as you've already found. It's also unflexible, unportable, and widely loathed for a variety of technical faults to the point its own inventor admits he "wasn't too good at programming" when he wrote it. We even have our own FAQ about it.

More to the point, you need to know how to use a proper Bourne shell. Many important things in the system use bourne and only bourne shells.
Quote:
It even allows arguments, which I abuse, for aliases without needing to use a script.
Bash functions are way more powerful than aliases.

Code:
function asdf
{
    echo $1 $2 $3
}

Put that in your ~/.bashrc and 'asdf' will be a command you can use.

But it also has aliases too. Show exactly what you tried in BASH and we'll find a way to make it work.
# 24  
Old 04-22-2011
Read this entire thread, from the beginning, and that is all I tried with both csh and bash. This is what it comes down to:

Code:
alias a="`find | egrep '.*assign[0-9]{1,2}$'`"

Its purpose is to run a C++ program without trying to run the .cpp, .o, and other files. The C++ program will either be assign# or assign##, with the # being any arbitrary number. The alias works fine when I type it in (which is a pain), so I try the .bashrc file, but then the `` end up getting executed (like it did with the .cshrc file). This means it tries to find everything everywhere that qualifies for the regular expressions.

Maybe this can be solved with a function? Things in `` are still executed in the .bashrc file, whether they're in an alias or a function...

Was that clear enough? What I posted in this entire thread has everything and could be easier to understand.

(Also, in csh, I had a problems before with a $ being in the regular expressions. The details are on the previous thread page. I tried it with bash, and it worked fine. I suppose this is one of those csh problems.)
# 25  
Old 04-22-2011
Here is what I have, if I understood it all Smilie
Code:
~/unix.com/a$ ls
assign1       assign11      assign12.cpp  assign13.o   assign2.cpp  assign3.o
assign10      assign11.cpp  assign12.o    assign1.cpp  assign2.o
assign10.cpp  assign11.o    assign13      assign1.o    assign3
assign10.o    assign12      assign13.cpp  assign2      assign3.cpp

~/unix.com/a$ a
./assign1
./assign11
./assign12
./assign2
./assign10
./assign3
./assign13

Bash function:
Code:
a() {
    find | grep -E 'assign[[:digit:]]{1,2}$'
}

Bash Alias:
Code:
alias b='find | grep -E "assign[[:digit:]]{1,2}$"'
alias c='find | grep -E '\''assign[[:digit:]]{1,2}$'\'''


Last edited by tukuyomi; 04-22-2011 at 09:55 AM.. Reason: Added alias (and another)
# 26  
Old 04-22-2011
Quote:
Originally Posted by Zucriy Amsuna
Read this entire thread, from the beginning, and that is all I tried with both csh and bash. Its purpose is to run a C++ program without trying to run the .cpp, .o, and other files.
You told us bits and pieces but never your entire purpose. Being clear would have helped you a lot.

find is going to give you a relative path. Just typing your script into the shell would've shown you that. Give find $HOME to fix that.

You also need to escape your $ so the shell doesn't think it's a variable. Trivial in BASH:
Code:
alias a="`find $HOME | egrep '.*assign[0-9]{1,2}\$'`"

But you just can't make a literal $ appear inside double-quotes in CSH. It's another one of those annoying holes. You have to do something silly like
Code:
VAR1='$'
alias a="`find $HOME | egrep '.*assign[0-9]{1,2}$VAR1'`"

You should really narrow down find a lot more, too, or it's going to trawl your entire home directory, recursively, to find the one file that matches your grep regex, every time you run a shell.
This User Gave Thanks to Corona688 For This Post:
# 27  
Old 04-22-2011
tukuyomi: That finds them, yes, but I need to run them. Using `` (the backquotes) would accomplish this. However, if I use them in the .bashrc file (or in .cshrc), they are automatically run upon logging in.

Corona688: Using this in bash worked:

Code:
function a {
`find $1 | egrep '.*assign[0-9]{1,2}$'`
}

And that sparked an idea for me to make it work in csh (which is the default shell my university uses, sadly):

Code:
set var1='$'
set var0='`'
alias   a       "$var0 find \!* | egrep '.*assign[0-9]{1,2}$var1'$var0"

The \!* is not needed, but it could help with running something in a different directory.

And both ways work! Thanks for all your help, everyone. ^_^ Problem solved.

However, it makes me wonder... How might I set such variables in bash?
# 28  
Old 04-22-2011
Quote:
Originally Posted by Zucriy Amsuna
And that sparked an idea for me to make it work in csh (which is the default shell my university uses, sadly)
At last, a clue! I've been wondering for a long time just what's continuing to spread its use.
 
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. UNIX for Dummies Questions & Answers

Find command and regex

Hi All, We have to copy some files from a source directory to a destination directory. We only have to copy the file if the filename is in a list of values. We can use find command: find . -type f -name '*_111.txt' -o -name '*_115.txt' ... -exec cp {} /tmp \; But the list contains... (3 Replies)
Discussion started by: bartleby
3 Replies

3. Shell Programming and Scripting

what's wrong with my regex using find

#ls json-* json-lexer.c json-lexer.h json-parser.c json-parser.h json-streamer.c json-streamer.h #find . -regex '^(json-)+.' return nothing (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

regex help with 'find'

How to do alternation using regular expressions in the 'find' command? Like say you want to find all files that do not match the names specifically "this" or "that" within a directory using regular expressions? (10 Replies)
Discussion started by: stevensw
10 Replies

5. Shell Programming and Scripting

Find directories by regex

Hello, I want to check if directories exist with a regex expression dir1=/temp/local/*/home (exists on file system) dir2=/temp/server/*/logs (does not exist on file system) I want to check if there are any directories with the above regex Code: if ];then echo "Directory... (4 Replies)
Discussion started by: gogineni
4 Replies

6. Shell Programming and Scripting

find regex and remove #

hi , how do i remove # from a line where i found regex.. don't need to remove all the line.. only remove comment.. (3 Replies)
Discussion started by: Poki
3 Replies

7. Shell Programming and Scripting

Find regex

Hi There, Can anybody help me out for searching this regular expression? xxxxx.yyy.zzzz.From-ABCD.To-XYZ.xxxxxx I would like the ID1 and ID2 (knowing which one is Id1 and id2) .From-<ID1>. and .To-<ID2>. Thanks in advance!! Regards, Bhaskar (4 Replies)
Discussion started by: bhaskar_m
4 Replies

8. UNIX for Dummies Questions & Answers

Regex in find command

Hi, I need to find out the files whcih contains date in YYYYMMDD in their name. I don't know if I can use regex in side find. Now I am using commad for the same purpose which is not full proof. find . -name "**" -print But I want then It should contain at lease 8 digit in their... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

9. Shell Programming and Scripting

Find with RegEx

I have some files in unix ls -1 TMH.backend.tar.421E-03.Z TMH.backend.tar.421E-04.Z TMH.backend.tar.421E-05.Z TMH.backend.tar.421E-06.Z TMH.backend.tar.421E-07.Z TMH.backend.tar.421E-08.Z TMH.backend.tar.421E-08.Z.bak20081223164844 TMH.backend.tar.421E-09.Z... (1 Reply)
Discussion started by: on9west
1 Replies

10. Shell Programming and Scripting

dont't find right regex

I have a string which contains following information: <SZ.T><P ALIGN="CENTER"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="+3">Bundesregierung nimmt sich dicke Deutsche vor</FONT></P></SZ.T> <SZ.UT><P ALIGN="CENTER"><FONT SIZE="+1"><I> Seehofer und Schmidt planen Kampagne gegen... (3 Replies)
Discussion started by: trek
3 Replies
Login or Register to Ask a Question