Questions tagged [pattern-matching]
The pattern-matching tag has no usage guidance.
141
questions
-1
votes
2
answers
57
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 ...
0
votes
2
answers
20
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. ...
1
vote
0
answers
27
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 ...
3
votes
1
answer
63
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]*
[[ "...
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['...
8
votes
2
answers
764
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 ...
0
votes
0
answers
10
views
Expression to pick the correct previous version for a current version
Consider the below file containing the different versions in oldest to newest order.
<version>2.4.2-release</version>
<version>2.4.2-mybranch</version>
<version>2.4.2-...
0
votes
2
answers
11
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.
7
votes
1
answer
304
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 ...
4
votes
3
answers
276
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.
...
4
votes
1
answer
154
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 / ...
0
votes
1
answer
10
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 ...
0
votes
1
answer
24
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
1
vote
6
answers
344
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;-...
0
votes
0
answers
36
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 ...
0
votes
2
answers
41
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 ...
-1
votes
4
answers
105
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 ...
0
votes
1
answer
62
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:
...
1
vote
5
answers
249
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 ...
2
votes
1
answer
24
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 ...
1
vote
2
answers
162
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
/...
2
votes
1
answer
482
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:...
2
votes
1
answer
96
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 ...
1
vote
2
answers
86
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 ...
3
votes
2
answers
364
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,...
0
votes
1
answer
34
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 ;;
*)
...
-1
votes
2
answers
74
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[*]}"...
1
vote
3
answers
825
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 ...
0
votes
1
answer
47
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 ...
3
votes
1
answer
931
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("...
0
votes
2
answers
38
views
Check if the second integer of a line in a file matches
I have a file with the below format of entries.
$ cat mw.hosts
host23v0242 USER=wsadmin MOUNTPOINT=/web
server21v0243 USER=wsadmin ...
0
votes
1
answer
504
views
to match the case insensitive pattern in if condition
There could be below 2 cases in a file: a.txt:
case 1:
V1=last #case insensitive
V2=Last #case insensitive
case 2:
V1=last #case insensitive
V2=LastNum #case insensitive
I need to check if ...
2
votes
8
answers
573
views
Print a portion of a lines in a text file between predefined tags
I have a file like the following
<g> Good wheatear </g> other parts of line
<g> The farm land is to be sold </g> other parts of line
<g> knock knock </g> other ...
3
votes
1
answer
448
views
Grep pattern that has double quotes and variable substitution surrounded by single quotes
So, Let's say I have the following pattern:
Thisisatest="1"
in a file called file.
And I want to match the exact string above but for whatever reason, I choose to loop over some numbers ...
1
vote
1
answer
57
views
Making Linux unzip command just extract files from directories whose names contain a given string
I have a zip archive that contains a directory hierarchy of files. I want to use the Linux unzip command to extract just the subset of these files that have a particular string somewhere in the ...
5
votes
3
answers
901
views
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. ...
-1
votes
1
answer
42
views
How to search for a pattern and print two lines before it for all matches found
I have a file with information about different protein entries. Each entry includes: details of the sequences (top line), number of times any alphabet is found in the sequence (second line) and the ...
0
votes
3
answers
55
views
Printing all numeric data one by one
I am provided with text file which contains data (alphanumeric, special characters in any order and layout). Now I have to print all numeric data one by one.
Example :
this is txt *24354 filer3243gdt ...
0
votes
1
answer
20
views
Delete specific pattern Unix
I have a string that look like this:
Root;rootrank;Bacteria;domain;"Actinobacteria";phylum;Actinobacteria;class;Acidimicrobidae;subclass;Acidimicrobiales;order;"Acidimicrobineae";...
0
votes
1
answer
240
views
How to match a string on a file using pure bash?
So i want to match a string/or word on a file, but without using any external tools (grep, sed etc), with only pure bash...
Essentially i want the equivalent of:
grep "string" file
or
grep -...
0
votes
1
answer
65
views
How to replace a hash pattern inside file and directory names
I've just backed up the notes I have in Notion, and they use hashcodes in the notes names to make them unique apparently.
For example, this is might be a note-folder with some notes:
Archive ...
0
votes
2
answers
38
views
Grep the RHS of the equal too sign [duplicate]
I am grepping into /etc/os-release and then I want to store the output in a variable. I had made a conditional script where I was not needing a variable but now I need one.
[[ ${$(grep ID_LIKE /etc/os-...
0
votes
2
answers
35
views
Batch rename folders appending fixed text and moving part of the name
I have several directories named in the pattern [year] name, like [1900] aaaa, [1901] bbbb, [1902] cccc, and so on. I'd like to append a fixed text - to the beginning of the name and move the [year] ...
-1
votes
2
answers
424
views
How to merge two files based on the matching of one column?
I have two files, B.csv:
1,AD
2,AB
3,AC
5,AF
7,AE
and C.csv:
1,x
3,z
5,y
How do I get this output:
1,AD,x
2,AB,
3,AC,z
5,AF,y
7,AE,
by matching the common column 1 in both of the files?
0
votes
2
answers
168
views
Regex that grep numbers after specific string in multiple files and return average
I would like to return the average of all numbers after a specific string in multiple files i.e.
We are given 10 files (file1.txt,...,file10.txt). Each file contains among others
Test1: Avg. length 24....
0
votes
2
answers
110
views
Parsing a file to blocks and iterating over each block
Edited the question to try and make things clearer
I have a file that looks like this:
BEGIN
Block name : Block1
Var names : var1 var2 var3
var1 32.7
var2 12.2
var3 65.4
END
BEGIN
Block name : ...
10
votes
3
answers
933
views
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'
...
3
votes
3
answers
267
views
Boosting the grep search using GNU parallel
I am using the following grep script to output all the unmatched patterns:
grep -oFf patterns.txt large_strings.txt | grep -vFf - patterns.txt > unmatched_patterns.txt
patterns file contains the ...
0
votes
2
answers
63
views
Find and replace expresion with quotes with sed [closed]
I want to replace a line of python code, which contain quotes
The line in the original file looks like this:
ScalarLogger("results.dat" ,('t',) + exp_data)
And I want to replace it to
...
0
votes
3
answers
85
views
Extract lines between two match strings while excluding the matched lines [duplicate]
Below is my file
$ cat README
login user1
run .profile
cd /u/opt
unzip -l zip file
copy files in zip file to .orig
unzip -o the zip file
cd /u/opt/install
./reqe21.sh
sqlplus admin/<password> @...