Questions tagged [pattern-matching]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
2answers
23 views

Find and replace a pattern after last space

I need to replace any matching pattern after the last space. I've an ssh certificate file like: ssh-rsa-cert-v01@openssh.com AAAAHHNzaC1yc2EtY2VydC12MDFAb3BlbnNzaC5jb20AAAAgePsBdieYnkI6dvMLG5qAl+...
0
votes
2answers
40 views

regex fails when trying to match whole word

Here is my test file test.txt hello user1 ALL=(ALL) NOPASSWD: /usr/bin/* /app/tomcat/tomcat*/webapps/*, /usr/bin/rm -rf /app/tomcat/tomcat*/webapps/*,/bin/rm -rf /app/tomcat/tomcat*/webapps/**.sh , /...
1
vote
2answers
48 views

Grep two files and print lines with multiple hits

I have two files. file1.txt abc def ghi jkl mno pqr file2.txt abc ghi abc xyz xyz xyz mno jkl def stu (column separator is tab) I am trying to grep the file1.txt against file2.txt sort of like this:...
3
votes
2answers
2k views

Why this regex pattern for email is so popular when it does not even take in to consideration for lower case letters?

I have seen the below pattern is used in several places (even on sof) as an example for email id validation. \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b the above is taken from https://www.regular-...
0
votes
1answer
36 views

Different behaviour of regex dot?

From this wiki article: Regular expression For example, in the regex a., a is a literal character that matches just 'a', while . is a metacharacter that matches every character except a newline. ...
-1
votes
2answers
48 views

How to replace all characters in string that dont match a pattern in sed?

I have a string such as "something_something_something" Now the string must have the pattern: ^[a-zA-Z0-9](-*[a-zA-Z0-9])* How can I replace all characters in the string that dont match the ...
4
votes
1answer
77 views

How to ensure user input consists of exactly 6 digits

I am running HP-UX 11.23 and have a bash script that reads two inputs. Both inputs need to be exactly 6 digits long. What is the best way to read the input and if its not 6 numbers ask the users to ...
9
votes
12answers
558 views

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 '/...
0
votes
1answer
25 views

extract pdf files from an index.html already downloaded in order to wget them, even when there are multiples pdf with grep

I have an index.html file containing href link to PDF files. When I do : grep -i 'href=' index.html, I get for example : <p>Télécharger : <a href="https://ecole-euclid.cnrs.fr/wp-content/...
-1
votes
6answers
108 views

How to grep multiple strings in different lines from an entire directory and sub directories on Linux terminal?

I want to match a pattern that have "John" & "Riya" & "bug". Example_1: //This file should match. Hi John, How are you? Riya, what is your age? Fix this bug by ...
-1
votes
1answer
37 views

Delete files with specific content

Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement? For example, if I wented to delete all files containing the line Key =...
1
vote
1answer
34 views

Conditional regex pattern matching in a korn shell script

I have started learning Unix shell scripting using korn shell. Please enlighten me finding the mistake I am doing while writing a ksh code for below a problem as stated below : My script takes 2 ...
-1
votes
1answer
65 views

Why does using double brackets “[[” to check if wildcard matched files exists fail, while single brackets “[” work?

Let's say I generate a two directories with text files in each like so mkdir "Directory1" mkdir "Directory2" touch "Directory1/fileclass1_"{1..5}".txt" touch &...
-1
votes
1answer
16 views

if a line contains a pattern and is followed by another line with same pattern delete the first line

My apologizes... I have brain freeze and can't work through this. I'm using sed and pattern space, but I keep deleting all occurrences of the lines containing the pattern. I want to delete only the ...
0
votes
1answer
31 views

find a file based on date mentioned in parameter

I have a requirement to find a file based on date provided in the parameter. For example, I have these files: XYZ.ABC.M.20200615180200.batch XYZ.ABC.M.20200616180200.batch XYZ.ABC.M.20200617180200....
0
votes
1answer
23 views

Awk - multiple string matching conditions that each span two consecutive lines

I'm trying to develop a VARIATION of the following straightforward awk one-liner (which currently works for matching a couple of conditions in each line of a file): awk "{ if ($1 == $3 && ...
4
votes
2answers
73 views

ack : get the 10th (or bigger nth) matching/capturing group

I think I might have just searched wrong, but I didn't find any answer. If there's a duplicate, please just let me know, and I can take this down. Problem Background I'm using ack (link), which has ...
0
votes
3answers
416 views

How to get group results using grep?

How would I get this output: Found value: This order was placed for QT3000! OK? or Found value: This order was placed for QT300 or Found value: 0 using line.txt and pattern.txt as below: [nsaunders@...
0
votes
1answer
17 views

rysnc any file which has “_custom.conf” or just “_custom” in the filename [duplicate]

Any way to rysync only files which feature _custom.conf or even just custom or *.conf in the filename? I've had a play with rsync -am --include='custom' --include='*/' --exclude='*' /etc/asterisk ~/...
1
vote
2answers
55 views

Print lines between patterns, excluding between patterns that doesn't match

So I have an input such as this: start abcd sfd afsdaf afsaf end 1 start sdfo efsf end 0 start sdf efsf end 2 so I want to only print lines between start and end (including start and end) but ...
8
votes
3answers
781 views

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, ...
2
votes
0answers
13 views

How to extract similar blocks of code from multiple files into a single file?

Problem: I have multiple large files that contain the following code block(s) split across all of them: type Query { ... } extend type Query { ... } extend type Query { ... } I wish to parse ...
1
vote
1answer
65 views

My grep for a process returns multiple strings with similar name

I have a script that checks processes on our Linux servers. An example would be we have a component named ebond-dbreader-bbg on server 1 and also a component named ebond-dbreader-bbg-cbrt on server 1....
0
votes
2answers
29 views

I need to fetch the relay=hostname.com from the sendmail logs

I need to fetch the relay=sender.hostname.com from the Sendmail log example below. sendmail[0000]: 03O7cddddddd: from=<user@user.hostname.com>, size=782, class=0, nrcpts=1, msgid=<...
0
votes
1answer
48 views

if condition between lines inside different lists, parse lists for matches, then use cp

Please, could anybody kindly suggest me how to do? I have two lists (both with sha1sums and their relative filenames) but formatted differently, here's an example: list01.txt artist'ssomesong.mp3,...
0
votes
4answers
137 views

AWK: how to extract pattern between 2 custom field separators regardless of pattern's position on the line?

Here is a fragment of a line in file: LN=FINE FOODS & PHARMACEUTICALS NTM, MIC=XAIM, RIC=FF.MI, SG=MA1 I am interested in extracting the value of tag "MIC", i.e. my desired output is: ...
1
vote
2answers
41 views

Gnu Sed (on Gnu/Linux) to find match + “anything” and replacing it

I'm trying to use sed to find matching string on a line in a file on nth match. I want to use expression to mach num and "anything" after on that line and replace it with num x. example file: START ...
0
votes
1answer
22 views

How do I delete the lines in a range that contain a given string from a script?

I believe the title is self-explanatory. I have some files given as parameters and the given string is the last parameter of my script. I have tried the two scripts below and do not know how I could ...
0
votes
2answers
52 views

grep patterns and keep matches on the same line

From the sample input: cat file.txt ID1 sample1 sample2 match1 sample3 ID2 sample4 match2 matchABC I would like to obtain sample output, keeping only the matched words on ...
0
votes
2answers
137 views

Bash brace expansion with variables for pattern matching

I want to loop through all files matching a pattern of the form for file in {prefix1, prefix2, ...}*{postfix1, postfix2, ...}; do How can I programmatically replace {prefix1, prefix2, ...} with a ...
0
votes
2answers
61 views

Matching columns of File1 to columns of file 2 and printing them in a file using awk

I am new to awk and trying to compare ":" separated columns 1(eg., chr10) and 2 (eg 10000003) between file1 and file 2 ; and write the matching lines into a new file using awk. File 1: chr10:...
-2
votes
3answers
359 views

What is command to redirect the lines from one file to another file

I want to redirect the lines which contain "hello" from file1 to file 2 in Unix. What is the command used here?
0
votes
1answer
44 views

Grep different kinds of patterns

I have a file that looks like this: 1>>>PROKKA_00001 Transcriptional regulator PadR-like family protein - 137 aa >>tr|E5G076|E5G076_ARAHY Ara h 1 allergen OS=Arachis hyp (619 aa) 37.3%...
2
votes
4answers
207 views

How to select first occurrence between two patterns including just the first?

Similar to, but not exactly like How to select first occurrence between two patterns including them... given this input file: something P1 something content1 content2 something P1 something content3 ...
0
votes
1answer
86 views

Vim search and replace positional arguments

I am trying to replace positional arguments with variables in my bash script using Vim's substitute :scommand. Example: I would like to replace the following text in a bash script: python3 mymodule....
7
votes
9answers
718 views

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 * ...
0
votes
0answers
15 views

Finding matches between two files based on two columns not perfectly matching

I am trying to merge two files like these: File1 chr19:clu_14_NA 2.75415627299357e-07 0.000108249659979084 NM_014931.3 chr17:clu_159255_NA 2.81974666270266e-07 0.000109329961143601 ...
5
votes
9answers
2k views

How can I make sure a string contains at least one uppercase letter, one lowercase letter, one number and one punctuation character?

This is what I'm using now to get the job done: #!/bin/sh -- string='Aa1!z' if ! printf '%s\n' "$string" | LC_ALL=C grep -q '[[:upper:]]' || \ ! printf '%s\n' "$string" | LC_ALL=C grep -q '[[:...
0
votes
0answers
8 views

Extracting two patterns from a file on multiple lines with output side by side

I have text report data formatted with an a identifier code on one line and then the account code associated with the identifier code on another line. Sometimes there are multiple account codes per ...
1
vote
1answer
118 views

ausearch: NOT operator in string match

When examining network activity logs from audit, I want to exclude a few programs I know, e.g. firefox. ausearch -x firefox -i brings up all firefox-related connections. But common NOT-operators ...
0
votes
1answer
1k views

what is GREEDYDATA in elasticsearch

reading the conf files of logstash i found in filter conf grok { match => { "message" => "Put\s*command\s*:\s+%{GREEDYDATA:command}" } } How does this filter work , i tried to ...
1
vote
1answer
24 views

Test if values in file1 are contained in column5 of another file

i have 2 text files: file1 contains only one column with a series of id values; file2 contains many columns. I need to check whether the values of file1 are contained in column2 of file2; the values ...
0
votes
0answers
43 views

Comparing two columns of one file with two columns of another file. Python or Bash?

So guys have two files, both of them are tab-separated text files, trying to merge both the files based on two columns. The two files are unsorted and doesn't contain a header. Another thing is that ...
0
votes
1answer
284 views

Zsh string pattern match to delete spaces

Trying to solve a code-golf problem using zsh, but my regex isn't working correctly. Requirement Given input string $1, delete all spaces immediately to the left of any ! character. Examples: ...
0
votes
1answer
25 views

Segregate results from a file using awk or sed after matching multiple patterns

From one of our application logs that contains a bunch of INFO messages, and system out lines, I want to be able to extract lines that are contained within 2 patterns and then specifically get values ...
0
votes
1answer
256 views

'expect' not matching server response string

I'm using a bash script to log into a telnet server and execute a number of commands. It looks like: login_and_run.sh #!/bin/bash unset TELNET_USER_NAME_STRING unset TELNET_PASSWORD_STRING unset ...
2
votes
3answers
96 views

How to find the last occurrence of a string in column 1 and replace the corresponding value in column 3?

I have three columns in a file as such: apple1 10109283 20012983 apple1 10983102 10293809 apple1 10293893 2349823049 apple10 109283019 109238901 apple10 ...
0
votes
2answers
44 views

Extract match and everything before next match, do it for every match

I have a file with this structure: >Cluster 0 0 51aa, >MG00HS05:520:C8M1TACXX:3:1101:1428:2080/1... * 1 51aa, >MG00HS05:520:C8M1TACXX:3:1101:1658:2480/1... at 3:51:1:49/96.08% 2 ...
1
vote
3answers
1k views

Checking whether a word (value) is in a comma-separated list

In below code, 2 should not match with 21, but it gives a result "matched". How can I check whether the SOURCE value is in the LIST? LIST="1,21,3,4" SOURCE="2" if echo "$LIST" | grep -q "$SOURCE"; ...
1
vote
2answers
233 views

sed or awk to print between 2 patterns[excluding the patterns] [duplicate]

I have the following text.. [start] this is my line [end] My output should be: this is my line Need help.. Tried a combination of : sed -n '/start/,/end/p' $File & sed -e "s/]/']/" -e "s/\[/...