Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

bash grep

Name: Anonymous 2012-02-27 13:29

How do I grep a given path and not the contents of the file in bash? ls won't work because the path is actually an input file, $1. Thanks.

Name: Anonymous 2012-02-27 13:55

wat

Name: Anonymous 2012-02-27 14:03

So I want to search the filename, $1, with something like this.

[code]{
if grep -q "lol" $1 ; then
echo farts
fi
}[code]

But instead it searches the contents of the file (like cat $1), not the filename.

Name: Anonymous 2012-02-27 14:18

I figured it out.

{
if echo $1 | grep "lol" ; then
echo "fucking echo the filename then pipe it to your mom."
fi
}

Name: Anonymous 2012-02-27 23:03

If you are looking for a file name in a directory tree, you can use find and grep in combination:


find . | grep wabbles


might output something like:


x/y/z/wabbles/wek.tt
wabbels.tt
r/wabbels/y.tt

Name: Anonymous 2012-02-27 23:07

find . -name \*RTFM\*

Name: Anonymous 2012-02-27 23:35

>>5
Here's a Perl version of what he's doing.
if ($path =~ /lol/) {
say "This pathname is HILARIOUS";
}


I think there's a better way to do this in Bash.

Name: Anonymous 2012-02-27 23:54

>>7
oh I see. Yeah, ops solution in >>4 looks good.


$ WABS=hello
$ if echo $WABS | grep hell > /dev/null; then echo "haha it contained hell!"; else echo "it did not contain hell"; fi
haha it contained hell!
$ WABS=welcome 
$ if echo $WABS | grep hell > /dev/null; then echo "haha it contained hell!"; else echo "it did not contain hell"; fi
it did not contain hell

Name: Anonymous 2012-02-28 0:41

>>8
Some greps have the -q or --quiet option for this purpose. Besides, if you have to redirect stdout, you should redirect stderr, too.

OP should be careful with those regexes. Use ^ and $ to mark the beginning and end of the line, and use the dirname and basename commands to grab the parent directory and the filename, respectively (echo `basename $1` | grep -q lol).

Name: >>8 2012-02-28 1:16

>>9
thanks for the tips

Name: Anonymous 2012-02-28 1:47

Some people, when confronted with a problem, think "I know, I'll ask /prog/ to check my dubs." Now they have two problems.

Name: Anonymous 2012-02-28 8:27

>>11
checked

Name: Anonymous 2012-02-28 9:59

>>1
use perl; faggot

Name: Anonymous 2012-02-28 10:00

perl

Don't change these.
Name: Email:
Entire Thread Thread List