Combining multiple greps


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Combining multiple greps
# 8  
Old 02-23-2019
Is there a "gray area" between grepping and globbing?
A moment ago I just read that "The moral of the story is that grep never uses globbing." (see link)
I also thought I had it straight in my mind that globbing is for filenames, and grepping is for searching text within a file.
Yet my first post above asks for help grepping filenames!

Globbing and Regex: So Similar, So Different | Linux Journal
# 9  
Old 02-23-2019
In your post, you're not "grepping filenames", but grepping text that is the result of an ls command, containing file names.


As you pointed out, you need to differentiate between "grepping and globbing", which are not the same even though it sometimes may seem so. Either is a malapropism; the exact terms would be "regex matching" and "pattern matching".

"Globbing" deals with patterns and is done by the shell, mostly when dealing with directory contents. And, in one exceptional case, some recent shells can deal with regexes: in "conditional expressions". man bash:
Quote:
A ... binary operator, =~, is available ... the string to the right of the operator is considered an extended regular expression and matched accordingly
"Grepping" deals with regexes, basic and extended, abbr. BREs and EREs. They have many subtleties, and it pays off to spend some time reading the man page.

Patterns and regexes in principle have different syntaxes. There are some overlaps, e.g. the [...] bracket expression meaning "Match any one of the enclosed characters", but also "faux amis" (false friends) like the * . It's always annoying to keep those differences in mind when dealing with either, and I have to test and experiment every single time when I switch from one to the other.

Last edited by RudiC; 02-23-2019 at 11:45 AM..
These 3 Users Gave Thanks to RudiC For This Post:
# 10  
Old 02-24-2019
With regards to pattern matching, perhaps a finer point to be made here is that (standard) globbing, or glob pattern matching, is a special form of pattern matching, where patterns are used for filename expansion.

In pattern matching, the wildcards are *, ? or a bracket expression ( [ ... ] )

In the case of globbing there are the following extra rules:
  • Wildcards do not match files that start with a . (dot) (those can be matched by specifying a dot as the first character in the pattern)
  • Wildcards do not match / (forward slash).
  • A forward slash cannot be used in a bracket expression (doing so turns the bracket expression into a literal string).

Globbing results in a list of files if there is a match, or the pattern itself if there is no match. The order in which the list of files is presented is governed by LC_COLLATE.
See Patterns Used for Filename Expansion


Examples:
Code:
$ mkdir -p somedir/foo
$ touch a.b .a.b somedir/bar somedir/.baz somedir/"foo bar"
$ ls -d *
a.b	somedir
$ ls -d .*
.	..	.a.b
$ ls -d * .*
.	..	.a.b	a.b	somedir
$ ls -d * .[!.]*
.a.b	a.b	somedir
$ ls -d */*
somedir/bar	somedir/foo	somedir/foo bar

Compare this to regular pattern matching, where a slash is actually matched
Code:
$ ls -d somedir/foo | while read line; do case $line in (*) echo "$line"; esac; done
somedir/foo

--
The bash shell and other more modern shells, like ksh93 and zsh also support extended globbing with additional rules.

Last edited by Scrutinizer; 02-24-2019 at 07:32 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining multiple files into one

Hello Everyone, I have 4 different files (one column in each) that I'm trying to combine into 1 file with four columns. Having issues trying to get the columns to format properly. I have tried the following: paste file1 file2 file3 file4 | column -s $'\t' -t > results.txt paste file1 file2... (1 Reply)
Discussion started by: malk71
1 Replies

2. Shell Programming and Scripting

Combining multiple seds into one awk

i'm trying to optimize my script. i have a lot of instances where i'm doing something like this: echo $blah | sed 's~ ~|~g' | sed 's-_space_- -g' | sed 's-_LP_-(-g' | sed 's-_RP_-)-g' obviously, this is very inefficient. i know i can combine into one sed command with something this, using... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Combining multiple files

I have 2 files. each having 3 coloums 1st field date as 20130322 2nd field time as 05:55 3rd field numberic value File 2 has entries missing for some date time. FILE1 20130322 05:35 2219 20130322 05:40 1809 20130322 05:45 1617 20130322 05:50 ... (2 Replies)
Discussion started by: sandeepkmehra
2 Replies

4. Shell Programming and Scripting

Bash Scipting (New); Run multiple greps > multiple files

Hi everyone, I'm new to the forums, as you can probably tell... I'm also pretty new to scripting and writing any type of code. I needed to know exactly how I can grep for multiple strings, in files located in one directory, but I need each string to output to a separate file. So I'd... (19 Replies)
Discussion started by: LDHB2012
19 Replies

5. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

6. Shell Programming and Scripting

Combining multiple variables into new variable

Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting. I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
Discussion started by: dring
10 Replies

7. Shell Programming and Scripting

Combining multiple files into one with the same name/different extension

I've been trying to find information in regard to creating a script that will generate HTML files. I currently have a series of files that contain code I need to surround with a <textarea> tag for easy viewing. I have about a thousand files that contain code, one file that contains the HTML code up... (10 Replies)
Discussion started by: 12o
10 Replies

8. Shell Programming and Scripting

Combining multiple commands

Hi Guys, I am looking to optimze these 5 SSH lines to a single SSH to get my machine to not hang! lol! cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} top -b > util/{}.top &' >> r_query_info cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} uname -r... (5 Replies)
Discussion started by: wick3dsunny
5 Replies

9. Shell Programming and Scripting

Sed: Combining Multiple Lines into one

Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows? What I'm trying to do: I have a file that looks like Color,Type,Count,Day Yellow,Full 5 Tuesday Green,Half 6 Wednesday Purple,Half 8 Tuesday ...... (3 Replies)
Discussion started by: goldfish
3 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question