Questions tagged [pattern-matching]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
22 views

replace both filename and filename's content relying solely on pattern found on the name-of-the-file

I'd like to know if it's possible to replace both filename.ext and a matched pattern inside its content regarding a pattern founded on the filename itself using sed (and grep also, maybe?). Let me ...
dAllARA's user avatar
  • 13
0 votes
1 answer
214 views

Searching for a pattern in a binary file using python script

I want to find repeated copies of the config section within the partition dump (binary file), using pattern and 'magic' header. The config section always starts with 202 '0xff' bytes followed by 4 ...
minto's user avatar
  • 511
0 votes
1 answer
58 views

Bash: Difference between "Pattern Matching" and "Filename Expansion" in the context of parameter expansions that do matching

Edit: Question ansewered by Gordon Davisson's comment I was reading the GNU Bash manual, and I noticed that there are basically three types of "Parameter expansion" that do pattern matching: ...
Sebastian Carlos's user avatar
3 votes
3 answers
485 views

Find files matching one pattern, but not another, using grep, awk or sed [duplicate]

I need to locate .php and .pl files that do not contain one string (e.g. aaa), but do contain another (e.g. bbb). I'm currently using this command: find /path/ \( -iname '*.php*' -or -name '*.pl*' \) -...
Krackout's user avatar
  • 2,545
4 votes
4 answers
626 views

grep multiple patterns with differing number of matching groups?

I'm trying to tally insertions and deletions from git diffs. I have the following, which when piped one or more strings of the form "4 files changed, 629607 insertions(+), 123 deletions(-)" ...
jawns317's user avatar
  • 143
1 vote
0 answers
51 views

does "find -name " matches the whole path or just the basename?

I was getting inconsistent results with pattern matching using -name, -iname, especially when looking for files -type f or directories type -d. Can someone explain under which circumstances -name ...
ricardo3889's user avatar
0 votes
3 answers
438 views

Glob pattern matching comma separated numbers

I want to write a glob pattern to match numbers separated by commas. What would the pattern be like? Here are some examples 5 5,8,13 Have started with the following, but there some problem with it, ...
Vera's user avatar
  • 1,183
0 votes
2 answers
53 views

How to combine files from incrementing directories into one directory

I have a directory structure that follows this kind of pattern: - Dir 01 (Disc 1) \ - Dir 01 (Disc 1).iso - Dir 01 (Disc 2) \ - Dir 01 (Disc 2).iso - Dir 01 (Disc 3) \ - Dir 01 (Disc 3).iso - ...
bashing_pumpkins's user avatar
-2 votes
1 answer
52 views

Determining repetitions of 8-tuples [closed]

I have 16 collections of 8 numbers (11 12 11 44 11 12 11 23) (12 21 11 44 11 12 11 23) (11 42 21 13 12 21 31 14) (11 42 21 13 12 21 11 34) and so on I want to find whether there are repetitions of ...
Vera's user avatar
  • 1,183
2 votes
2 answers
1k views

Sed based on 2 words, then replace whole line with variable

I have a txt file looking like this, 1:Ana:apple:08:00:wait 1:Joe:banana:08:30:wait 1:Oscar:orange:08:45:ready 1:Monica:apple:08:50:ready 1:Mark::orange:ready I have to find only the first line that ...
Claudio Del Prete's user avatar
0 votes
3 answers
250 views

Need to grep the word right next to or below a pattern

I have a file with the following lines in i: .......... FROM ABCD_EXT .......... FROM HEG_EXT1 .......... from xyz_EXT .......... FROM abd_EXT2 ..........where QWT_EXT.SID=POI_EXT....
TexGC's user avatar
  • 31
0 votes
1 answer
89 views

how to detect unbalanced special characters in string

I'm wondering what would be the best way (likely using grep or ack) to return lines containing unbalanced special character sets in a string? For example, if the string were: bqM#+t1U"OyBGhk]ozVG[...
irishwristwatch's user avatar
2 votes
2 answers
157 views

How does this glob expression remove colons?

Say we have a file called "Hello World: The easiest program" and its name is saved in a variable called "title". I want to match this with glob pattern matching, but remove the ...
Rudi Österreicher's user avatar
1 vote
2 answers
30 views

How to match the starting of string & then replace some value using the pattern in bash

vm0 host=10.182.35.1 controller=True vm1 host=10.182.35.2 controller=False solution=abc vm2 host=10.182.35.3 controller=True solution=abc vm3 host=10.182.37.4 controller=False solution=xyz vm4 host=10....
Madhavi Deshpande's user avatar
1 vote
2 answers
34 views

How can I delete everything before and afrer match pattern from pdb file?

I'm beginner in bash scripts, but I try to delete everything in my pdb file (test.pdb) before REMARK time 20.00 and from REMARK time 30.00 to the end of this file. I'm using to this sed command, which ...
skywalker's user avatar
0 votes
1 answer
47 views

Remove/move files in a directory with filename timestamp pattern

So I have files that need to remove/move/filter. All files have this pattern like this in a directory, let's say directory frames contain this pattern filename timestamp_in_nanosecond.jpg. And this is ...
Muhammad Ikhwan Perwira's user avatar
0 votes
2 answers
263 views

Extract all links (including multiple links per line) in markdown file

Sample markdown file: cat index.md # Abstract - To achieve [Work Life Balance](./WorkLifeBalance/WorkLifeBalance.md), first understand what it means and what are the requirements. - Develop right **[...
Porcupine's user avatar
  • 1,700
0 votes
1 answer
186 views

Remove certain words and set word at end of string in files

I have a bunch of files that contain strings similar to: Get<1>(abc) Get<2>(xyz) How can I recursively search for all files with the above pattern and remove everything before the first '(...
aWLW's user avatar
  • 101
6 votes
2 answers
2k views

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 ...
rewire's user avatar
  • 101
0 votes
1 answer
81 views

Sum sizes of certain folders/files with inclusion and exclusion patterns

I'm looking for a tool that gives me the total size on disk of certain folders and files. It should be able to unambiguously include or exclude items and all of their children (folders and files in it)...
ostrich's user avatar
  • 11
1 vote
2 answers
133 views

how to check for existence of multiple patterns in bash

i have several patterns that include ** wildcard, let them be: foo/**/bar.xml foo/**/baz.xml and i have an example dirtree: $ tree foo/ foo/ └── deep ├── baz.xml └── even └── deeper ...
murison's user avatar
  • 163
-1 votes
2 answers
754 views

awk no print if value from file1 not match value in file2

file1: G A 4 H B 3 I C 2 J D 1 file2: M H 6 N H 5 O K 4 P J 3 Q I 2 R I 1 S G 0 T L 1 Desired: M H 6 N H 5 P J 3 Q I 2 R I 1 S G 0 print only rows where $2 from file2 matches $1 from file1 I'm ...
Sollosa's user avatar
  • 1,889
0 votes
2 answers
208 views

match number range with non significative zeros in zsh

I have in a directory files like this : booklet-001.pdf booklet-002.pdf booklet-003.pdf … booklet-997.pdf booklet-998.pdf booklet-999.pdf Then, I whant to match the files numbered from 079 to 256. ...
fauve's user avatar
  • 1,130
1 vote
0 answers
53 views

How to find the PHP file which prints an HTML element with a certain selector?

I store my MediaWiki 1.36.1 website with Skin:Timeless on NameCheap with a shared hosting environment with Cpanel file manager and SSH shell access. In my website, there is a certain footer element ...
searcher's user avatar
2 votes
1 answer
117 views

ZSH expanded variables in [[ ... ]] do not perform globbing

I have extended_glob set in .zshrc. this works as expected: [[ "value" = [a-z]* ]] && echo "globbed" Prints "globbed". But this does not: foo=[a-z]* [[ "...
toni rmc's user avatar
  • 123
5 votes
9 answers
1k views

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['...
Lahor's user avatar
  • 113
8 votes
2 answers
2k views

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 ...
shikhanshu's user avatar
0 votes
2 answers
35 views

How to extend a wild card option to include js and ts

I have an alias set as follows: alias ppp='prettier --write "**/*.js"' I want it also to do all my .ts files. I'm quite inept at wild card expressions sadly.
Peter Kellner's user avatar
7 votes
1 answer
313 views

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 ...
U. Windl's user avatar
  • 1,104
4 votes
3 answers
1k views

Is there a way to make find use shell's extglob shell option?

In short, I instinctively wrote a command like this to find the two files prefix.ext and prefix_suffix.ext down a hierarchy find /some/path -type f -name 'prefix?(_suffix).zip' but it doesn't work. ...
Enlico's user avatar
  • 1,471
5 votes
1 answer
609 views

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 / ...
Silver137's user avatar
  • 329
0 votes
1 answer
391 views

select lines in file with same string pattern

Let´s suppose I have the following file.txt: asiub sj abq b aia ainp oo test = 123d sub ,. aiba 87ab test = 129szs bq test = aqua ayqvq 133s I want to print only the lines of file.txt that contain ...
aaaaa's user avatar
  • 141
0 votes
1 answer
147 views

Rsync: Clone structure for specific pattern [duplicate]

How can one recursively copy all files of a specific pattern (*.tpl) using rsync? Example: /etc/test.tpl /etc/dir/test.tpl /etc/dir/**/test.tpl
Veles's user avatar
  • 115
1 vote
6 answers
882 views

print lines between 1st occurence of 1st pattern to last occurence of 2nd pattern with AWK

I have a datafile, with dates in 2nd column # cat datafile -;20210106;-;-;-; -;20210112;-;-;-; -;20210112;-;-;-; -;20210112;-;-;-; ... -;20210217;-;-;-; -;20210217;-;-;-; -;20210217;-;-;-; -;20210217;-...
Sollosa's user avatar
  • 1,889
0 votes
0 answers
49 views

Capturing comment character from matching pattern

I would like to pick out lines from a file matching # Mode: org # can be any comment character for the programming language of the file. Rather than #, in could also be any of ; or !, with any ...
Vera's user avatar
  • 1,183
0 votes
2 answers
97 views

how to perform multi substitution with sed on a block of text?

I have text file, and in it has a block that has pattern like below: # cat file Jit .... enabled=false Jit ..shoes.. From=2021-01-01 Jit ..gloves.. From=2021-01-01 so I want to change all of these ...
Sollosa's user avatar
  • 1,889
-1 votes
4 answers
1k views

Linux - Find all occurrences of a certain pattern in a line of a file

I have a file in Linux which contains a line like : (memberOf=CN=Group1)(memberOf=CN=Group2)(memberOf=CN=Group3)(memberOf=CN=Group4) My goal is to populate the list of groups that is present in the ...
Deep's user avatar
  • 1
0 votes
1 answer
373 views

How to not print the output if the second pattern does not match

file1: pattern1 a b c end cmd=> cat file1 | sed -n '/pattern1/,/pattern2/p' output=> pattern1 a b c end How to not print the output if the second pattern does not match? Desired output: ...
Priyatharshini Ganesan's user avatar
1 vote
6 answers
2k views

match some or all patterns with awk

I have a small problem with awk multiple pattern matching which I cannot figure out. I have the following awk line: awk '/pat1/{v1=$4; next} /pat2/{v2=$5; next} /pat3/{v3=$6;next} /pat4/{v4=$5; print ...
lucian's user avatar
  • 299
2 votes
1 answer
40 views

Loop through file with unique identifer and compute cumulative sum

I have a large text file, as shown below, where column1-colum3 define a region and fourth column is unique identifier. The column 6 is assigned value of the region. chr6 26204834 26204839 ...
CN_229133's user avatar
  • 105
1 vote
2 answers
735 views

Why does "grep -w" match words followed by hyphen/minus ("-")?

I need to ask you about using grep command in a Bash script in Debian. I have got for example file with these lines: /fruit-/apple.txt /fruit-/banana.txt /fruit-/samples /vegetables-/carrot.txt /...
SED85's user avatar
  • 45
2 votes
1 answer
1k views

How can grep return a different number of matches?

I need to grep a file (24 lines) into two files (which are kind of big). The original files contains 24 lines that resemble this string: AATGGACGCTTAAC[A|C|T][A|C|G]CGGC[A|T]TCGGAT I do the following:...
gabt's user avatar
  • 381
2 votes
1 answer
220 views

Search for long string (>50 characters) and record match

I have a piece of code that looks for a string and then records if that matches. The code works fine when the string is not longer than 50 characters. However above this, say for a string of 100 or ...
dcp1234's user avatar
  • 27
1 vote
2 answers
258 views

grep partial ip number from the file

I have to sort IP addresses into classes, so I can block entire class in myfirewall. It works fine when I try to do for /24 class, but not so well when do for /16 class. I have a list of IPs in the ...
DenisZ's user avatar
  • 76
3 votes
2 answers
559 views

Awk regex for operators

I want to count and sum number of matches in a file with my awk regex. The file file contains: Gra pes gra ndma straw berry blue Berry banana peanut school I need to make a regex for pattern matching,...
Hewitt's user avatar
  • 45
0 votes
1 answer
222 views

case-substring function and quoting

i use a simple function in a script in order to ckeck if a string contains a particular substring: #!/bin/bash # subs() { case $2 in *$1*) return 0 ;; *) ...
noemata's user avatar
-1 votes
2 answers
358 views

Finding files and matching patterns with printed filename in bash

I have the following code that lists matches far a pattern ptrn, with the filename being printed before the listing (which uses the context option -C NUM) find "$fdir" "${isufx[*]}"...
Pietru's user avatar
  • 371
1 vote
3 answers
2k views

Matching a valid version number inside case statement

I want to match a version number inside a case statement. The version number could look like this 1.12.0.32 1.12.0.32.1 2.10.0.30.1.2 and is stored inside a shell variable: version. As I searched on ...
ashish_k's user avatar
  • 375
0 votes
1 answer
163 views

Instruct shell script to do a task if the output of a program contains a specific word

I need to create a program in Bash that launches a command, check if the result contain a word, and do something if it does. Example: I need to check if a printer is online with lpstat -p [printer ...
Othin's user avatar
  • 1
7 votes
1 answer
4k views

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 <div> tags $ echo "\"This is a valid json file"\" | jq '. | gsub("...
Zeta.Investigator's user avatar