LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 04-30-2013, 02:34 AM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,819

Rep: Reputation: 505Reputation: 505Reputation: 505Reputation: 505Reputation: 505Reputation: 505

Try this:

Code:
grep -i -e "perg" -- *
 
Old 04-30-2013, 07:38 AM   #17
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 393

Rep: Reputation: 157Reputation: 157
Quote:
Originally Posted by NevemTeve View Post
Yet another possibility: perhaps your excel automagically converts '--' to 'en-dash' or 'em-dash'
It is than possible that it saves the value as UTF-16.

For a quick and dirty approach, perhaps convert XSL to ODS and than have a small tool which unzips it to temporary directory and greps through the XML files.
 
Old 05-01-2013, 07:17 PM   #18
Ystack
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Original Poster
Rep: Reputation: 0
grep -i -e "perg" -- *

My tests show this configuration will indeed look into a local file named "--" without issue, and will similarly treat "*" as a local filename (rather than a glob), looking into that too if it is present. But directories are skipped over, not processed at all, so the potential choke on directory "--Z" is merely avoided through misdirection in this instance. And to try it on a tree-walk, grep would expect a full list of filenames to be specified at each stage, and then it reaches that empty subdirectory ......

Try this: #> grep -i -e "perg" --

....(ouch!... we are waiting ...)
 
Old 05-01-2013, 10:35 PM   #19
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,495

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
grep has -r to recurse into directories, or you can use find+xargs:

Code:
grep -rie 'perg' -- /mytest
find /mytest -type f -print0 | xargs -0 grep -ie 'perg' --
 
1 members found this post helpful.
Old 05-01-2013, 10:36 PM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,819

Rep: Reputation: 505Reputation: 505Reputation: 505Reputation: 505Reputation: 505Reputation: 505
You mean you want grep working recursive? Use option -r.
 
Old 05-02-2013, 04:49 PM   #21
Ystack
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Yes, indeed. And even the -e switch now seems superfluous:

#> grep -ir "perg" -- *

seems to work equally well -- (ie not only does not choke on, but actually looks into subdirectory "--Z" & finds "perg"). Without the "*" it would still launch into an outer space excursion. Without the "--" it would still choke.

Anyone game to offer a rationale? Understanding what is happening here may very well help with the original problem I was working on.
 
Old 05-02-2013, 06:11 PM   #22
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,495

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
You only need the -e switch if your pattern starts with a "-" to avoid the pattern being interpreted as an option. You only need the -- switch if your file names start with a "-" to avoid the file name being interpreted as an option. If you pass no file names grep reads from standard input, so it's just waiting for you to type something (end of data with Ctrl+D).

Note that grep doesn't see the * or the quotes, those are handled by the shell:
Code:
# Assuming you have files name --Z and foo in the current dir:

# You type this
grep -ir "perg" -- *
# grep sees this
grep -ir perg -- --Z foo
 
2 members found this post helpful.
Old 05-03-2013, 02:46 PM   #23
Ystack
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Got it -- ta. Nice explanation.
 
Old 05-05-2013, 01:57 PM   #24
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Darn, ntubski got to the explanation before I did. Indeed, with almost any command, arguments that begin with leading hyphens will confuse them. Most of the common ones will recognize the special '--' option that signals the end of command options. See "info coreutils > Common Options".

grep and sed also have '-e', which can be used to explicitly specify that the following token is a pattern expression. '-e' also allows you to give multiple expressions at once.

As for grep's recursion option, since it operates on directories you shouldn't use a generic globbing pattern like '*'. Instead just give it one or more starting directories to search from. In practice this usually means using '.' as the argument, unless you want it to start at a different place.

Code:
grep -ir "perg" .
You can also specify --include= and --exclude= globbing options to filter what it processes. Note that these globs are handled by grep, not the shell, and so need to be quoted so that they get passed to it unexpanded.
 
1 members found this post helpful.
Old 05-07-2013, 03:40 PM   #25
Ystack
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Original Poster
Rep: Reputation: 0
And some valuable extra depth here, very clearly presented. Thanks indeed.
 
Old 05-19-2013, 05:22 PM   #26
Ystack
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Original Poster
Rep: Reputation: 0
"...since it operates on directories you shouldn't use a generic globbing pattern like '*'....." (David the H.). Perhaps a little clarification.

Mostly I do not want recursion, which excludes the combination "-r .", so back to some kinda glob for expansion to all files in a current directory.

#> grep -i "perg" -- *

does avoid the particular problem explicated previously (with "*" + link "--Z"), but I guess there may be other dangers too?

Or maybe it is just the combination of "-r *" that should be avoided. But if not, then perhaps I am safer to feed grep a pre-expanded & pre-filtered list from Perl:

my $finds = '';
my @safeglob = grep {-f && (-T || -B)} glob "*";
if (@safeglob) {$finds = `fgrep -i 'perg' @safeglob`;} # backticks

What do you think?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
grep multiple strings GEEXTER Linux - General 7 12-06-2013 09:56 PM
grep files based on strings verse123 Linux - Newbie 5 08-16-2012 04:36 PM
[SOLVED] grep multiple strings krist_m Linux - Newbie 4 01-11-2011 11:43 AM
grep searching for strings with '(apostrophe) macsdev Programming 5 11-11-2010 11:46 PM
grep two strings Hondro Linux - General 3 09-08-2008 09:55 PM


All times are GMT -5. The time now is 04:34 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration