Questions tagged [patterns]

63 questions
157
votes
2 answers

How do we specify multiple ignore patterns for `tree` command?

I need to print the directory structure of our production system and I would like to remove some specific directories from the tree? How do we specify multiple ignore patterns for tree command?
Bharat Sinha
  • 1,713
  • 2
  • 12
  • 8
36
votes
1 answer

What is the .gitignore pattern equivalent of the regex (Big|Small)(State|City)-[0-9]*\.csv

I have a regex I stuck in my .gitignore similar to: (Big|Small)(State|City)-[0-9]*\.csv It didn't work so I tested it against RegexLab.NET. I then found the gitignore man page which led me to learn that gitignore doesn't use regexes, but rather…
Justin Dearing
  • 1,391
  • 3
  • 13
  • 26
21
votes
1 answer

echo [9876543210] displays 1 4 5 6 ... why?

Please explain why 1 4 5 6 is displayed for the last four echo statements? I hit this by accident once, but I am now curious as to why this behavior occurs. These statements work as expected (for me). $ echo [ 9876543210 ] [ 9876543210 ] $ echo…
MikeD
  • 800
  • 5
  • 16
19
votes
2 answers

How to use logical OR in find?

In bash shell ls can use a logical OR functionality through (of course I could also do ls name1 name2 but my true examples are more complicated): ls @(name1|name2) Is there a way to do this using find? My naive implementation: find . -maxdepth 1…
JeffDror
  • 325
  • 1
  • 2
  • 7
15
votes
3 answers

How to ignore multiple files with `ag` The Silver Searcher

There is an option --ignore which allows specifying files to ignore. At the moment I only managed to ignore multiple files by doing --ignore file1 --ignore file2....... Trying to use --ignore "*assets*|*scripts*" does nothing. So is there a catch…
T.Chmelevskij
  • 253
  • 1
  • 2
  • 7
15
votes
1 answer

How to find files that contain newline in filename?

I am trying to find files that have a newline in the filename. But I can't figure out what pattern to use. The following works, but is not very useful if I want to use it in indented code. find . -name '* *' I tried these two and they only…
toxalot
  • 1,025
  • 1
  • 9
  • 21
13
votes
5 answers

List files that match a pattern but ignore files that match another pattern?

Let's say I have a directory with files a1, a2, a3, b1, b2, b3. I only want to match files that start with a but don't contain 3. I tried ls -I "*3" *a* but it returns a1 a2 a3, even though I don't want it to match a3. Is this possible with ls?
Steven
  • 705
  • 1
  • 7
  • 10
12
votes
2 answers

find only the first occurence using only grep

Suppose i have a file with many words, i want to find only the first word with the pattern "xyz". How do I do it if there are multiple words with this pattern on the same line? -m returns all the words in the first line in which it matches. I need…
Atchyut Sreekar
  • 131
  • 1
  • 1
  • 4
12
votes
5 answers

How to create an array of unique elements from a string/array in bash?

If I have a string "1 2 3 2 1" - or an array [1,2,3,2,1] - how can I select the unique values, i.e. "1 2 3 2 1" produces "1 2 3" or [1,2,3,2,1] produces [1,2,3] Similar to uniq but uniq seems to work on whole lines, not patterns within a line...
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
11
votes
3 answers

Find files that end with number

Im trying to make some backup script as the log files get bigger and bigger. What i have is coping the current file, (for example secure file in /var/log/) and remove the content from that file. But there are some files with the name like: secure.1,…
user3523605
  • 269
  • 2
  • 6
  • 16
10
votes
5 answers

Pattern matching on path names in bash

I want to act on a list of subdirectories in a directory. Consider: for x in x86-headers/*/C/populate.sh; do echo $x; done This…
Faheem Mitha
  • 34,649
  • 32
  • 119
  • 183
10
votes
3 answers

Is it possible to match multiple specific line numbers (not range) with sed?

Consider: echo -e "a\nb\nc\nd\ne\nf\ng\nh" | sed '3,5a test' This will match lines 3, 4 and 5. But I am trying to match only lines 3 and 5 (not 4). And append 'test' after them.
Aquarius Power
  • 4,099
  • 5
  • 38
  • 56
9
votes
7 answers

How to match exact string using `sed`? But not the part of it.?

I have a input file FILE1.TXT as below. 11 id1 12 13 AGE = 20 14 NAME = NAME1 15 16 id2 17 18 AGE = 30 19 NAME = NAME2 . . . 110 idXYZ 111 112 AGE = AGEXYZ 113 NAME = NAMEXYZ 114 115 idZZZ 116 I want to search…
Vinay
  • 193
  • 1
  • 1
  • 8
8
votes
1 answer

Bash Globbing not as expected

This is a homework question: Match all filenames with 2 or more characters that start with a lower case letter, but do not end with an upper case letter. I do not understand why my solution is not working. So I executed the below: touch aa touch…
user211221
8
votes
2 answers

BASH: count # of words in each line of a document

I need to identify patterns in a text file for further analysis. So the input files may contain semi-structured text as follows; file1 905:john: abc123: [email protected]: US 920:eric: ericaA: [email protected]: US 1000: rio: ri0ri0: [email protected]:…
user3148655
  • 397
  • 1
  • 5
  • 10
1
2 3 4 5