Questions tagged [pattern-matching]
160 questions
29
votes
2 answers
What is the difference between `a[bc]d` (brackets) and `a{b,c}d` (braces)?
What is the difference between a[bc]d and a{b,c}d? Why do people use a{b,c}d when there is already a[bc]d?
Weijun Zhou
- 3,338
- 16
- 42
11
votes
1 answer
How to grep MATCH colored input?
Say I have output from a command that is colorized for the terminal. I want to match any line that contains the color yellow. How can I do this in grep, eg: mycommand -itSomtimesPrintsLinesWithYellowColorCodes | grep -e "?????"
Note: This is NOT…
Zombies
- 645
- 1
- 7
- 11
10
votes
3 answers
Why does find not find my directory neither with -name nor with -regex
I have a directory ~/Documents/machine_learning_coursera/.
The command
find . -type d -name '^machine'
does not find anything
I also tried
find . -type d -regextype posix-extended -regex '^machine'
so as to match the the beginning of the string…
moth
- 307
- 1
- 9
9
votes
12 answers
How do I print the last sequence of lines between a start and an end pattern?
Answers to this question:
How to grep lines between start and end pattern?
are not concerned with multiple sequences of lines which fall between the match patterns. Thus, for example, sed -n '/startpattern_here/,/endpattern_here/p' will print…
einpoklum
- 8,772
- 19
- 65
- 129
9
votes
3 answers
Using an if statement for a list of words
So I have a program (say, programA), that will give me an output, for example: yes, no, maybe, probably, possibly, impossible, banana.
I want to make a script that will do something, whatever it is, based on that output. Let's say I only need to…
Lhakryma DL
- 181
- 1
- 4
8
votes
2 answers
Avoiding non-zero exit code when running `ls` using multiple patterns
Say I have two possible paths I want to list directories and files under on a Linux machine:
/some/path1/
/some/path2/
If I do the following in tcsh, I get 0 exit code, if at least one of path1 or path2 exists:
ls -d /some/{path1,path2}/*
But if I…
shikhanshu
- 245
- 1
- 6
8
votes
4 answers
Write a function that checks if a string starts with or contains something
I want to write a function that checks if a given variable, say, var, starts with any of the words in a given list of strings. This list won't change.
To instantiate, let's pretend that I want to check if var starts with aa, abc or 3@3.
Moreover, I…
No Imaginatition
- 83
- 1
- 4
7
votes
1 answer
Is there a Unix command that searches for similar strings, based mostly on how they sound when spoken?
I have a file of names, and I want to search within it, not caring too much about whether I have spelled the name ( that I am searching for ) correctly. I know that grep has quite a bit of functionality to search for a whole slew of similar strings…
gabkdlly
- 173
- 5
7
votes
1 answer
Is "warning: range of the form `[o-b]' is locale dependent" a bug in gawk 4.1.0?
While writing some awk code I was using the --lint option for gawk 4.1.0.
To my surprise I get a warning
warning: range of the form `[o-b]' is locale dependent
but my code has only a foo-bar outside of a character class.
Simplified example:
{…
U. Windl
- 1,095
- 7
- 21
7
votes
1 answer
How to refer to matched groups in jq gsub?
Using jq, I want to search for a pattern via regex, and wrap the matched string with something like
tags
$ echo "\"This is a valid json file"\" | jq '. | gsub("valid";"how_to_refer_to_matches";"i") -
How to refer to the matched results in the…
Zeta.Investigator
- 880
- 1
- 7
- 25
7
votes
3 answers
How to match a literal string containing an unclosed quote with ripgrep?
I have a file that contains the some python code. There is a line that contains the following(including the quotes)
'hello "value"'
I want to search 'hello "value" in the file. Notice the unclosed quote. I'm using ripgrep with the folowing…
Haris Muzaffar
- 249
- 2
- 6
6
votes
2 answers
Exclude patterns from glob match
My directory looks like this:
$ ls
total 0
-rw-r--r-- 1 user user 0 Jun 18 22:44 file0
-rw-r--r-- 1 user user 0 Jun 18 22:44 file1
-rw-r--r-- 1 user user 0 Jun 18 22:44 file2
-rw-r--r-- 1 user user 0 Jun 18 22:44 file3
-rw-r--r-- 1 user user 0 Jun…
rewire
- 101
- 6
6
votes
9 answers
Extract subsequence corresponding to n:th pattern from a file
I have below data blocks (multiple)
chr1.trna4 (17188416-17188486) Length: 71 bp
Type: Gly Anticodon: CCC at 33-35 (17188448-17188450) Score: 78.3
HMM Sc=56.60 Sec struct Sc=21.70
* | * | * | * | * | * …
MO12
- 369
- 1
- 5
- 17
5
votes
9 answers
How to put text inside text automatically?
I need to put one text inside another text.
1) I have a file with list of input values:
A1
B2
C3
D4
E5
I have a wrapper pattern which should contain the text:
$wgSpecialPageLockdown['INPUT_COMES_HERE'] = array('sysop');
For each input value, a…
Lahor
- 113
- 8
5
votes
1 answer
Using anchor ^ pattern when using less / search command
When navigating through Linux man pages, sometimes you need to query the meaning of flags like -p or -al in the man pages.
The default text reader for man pages is less and I'm trying to use the / search to find the first occurrence from the commons…
Silver137
- 329
- 1
- 8