Questions tagged [pattern-matching]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
27
votes
2answers
1k views

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?
7
votes
1answer
726 views

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 ...
6
votes
4answers
685 views

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 ...
5
votes
1answer
988 views

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 -...
4
votes
5answers
261 views

Count the longest stretch of consecutive patterns

I have a sequence file: $ cat file CACCGTTGCCAAACAATG TTAGAAGCCTGTCAGCCT CATTGCTCTCAGACCCAC GATGTACGTCACATTAGA ACACGGAATCTGCTTTTT CAGAATTCCCAAAGATGG I want to calculate the longest stretch of C+T. I ...
3
votes
3answers
180 views

Comparing two files linewise and if pattern of file 1 is not found (fully or partially) in file 2 then print line of file 1

I have two files: file1 (search): 1 GACGGAGGATGCAAGTGTTATCCGGAATCACTGGGCGTAAAGTTTTTTTTT 2 GACGGAGGATGCAAGTGTTATCCGGAAT 3 GACGGAGGATGCAAGTGTTATCCGGAATCACTGGGCGTAAAGCGTCC 4 ...
3
votes
0answers
27 views

Should I set the locale to `C` when matching a range of numbers?

If I wanted to search for lines in a file that contain a or b or c or d I would run LC_COLLATE=C grep -E '[a-d]' file_to_search or LC_ALL=C grep -E '[a-d]' file_to_search If I fail to set the ...
2
votes
4answers
509 views

Parse all strings of specific length?

I've exported my email archive of 10 years which is very large. I want to parse all the text for any string that is 64 characters long in search of a bitcoin private key. How can I parse strings of ...
2
votes
2answers
654 views

Use sed to print selected lines within a range

Consider the simplified file AAA BBB CCC1 DDD EEE CCC2 DDD FFF GGG CCC3 HHH I can pick out the range EEE to FFF with sed -n '/EEE/,/FFF/p' Suppose though that I want to print any line containing C ...
2
votes
1answer
49 views

How do I avoid greping matching lines with -

I'm trying to grab only matching lines from a file using grep. The problem is that, when for example I use the following expression: grep -w AFFX-KIT-000088 textfile it will return: AFFX-KIT-000088-...
2
votes
3answers
59 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 ...
2
votes
1answer
37 views

WildCard Find Replace CP

I'm trying to replace files that match a particular pattern* on CentOS, but it is not working. I initially wrote: cp example.com/(*)/orm-mysql/build/conf/(*)-conf.live.php example_dev.com/$1/orm-...
2
votes
0answers
24 views

why does recursive searching with grep print fewer lines when using \bSTRING\b compared to the -w option?

There are two ways to read all files under the current working directory: grep -rw pip | wc -l grep -r "\bpip\b" | wc -l Assuming 1 and 2 both search the working directory for the whole word "pip", ...
1
vote
1answer
37 views

Odd behavior (bug?) of sed when using $

I've got a problem and I am not sure whether this is a bug or if this is supposed to happen when using $ for addressing lines. Basically, I have the following command: sed -e '$s/test/tset/g' ...
1
vote
2answers
445 views

grep contents of one list with another

I have 2 files and I want to grep on them List1 ACY1 RPL3 RPL4 List2 1 ABHD14A-ACY1 2 ACY1 3 RPL34 4 RPL21 5 RPL3 6 RPL41 I tried something like this grep -Fwf list1.txt list2.txt ...
1
vote
1answer
70 views

Match lines between two patters using sed just once [duplicate]

Given: sed -e '/pattern1/,/pattern2/!d' file.org How can I match the first occurrence of the lines between pattern1 and pattern2, but not the rest? For example: pattern1 aaaa pattern2 pattern1 bbb ...
1
vote
2answers
23 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/\[/...
1
vote
1answer
42 views

Compare two text files, extract matching rows of file2 plus additional rows

have been mocking around with this too long and tried grep, join, awk but I cant get the parameters right. I need to get the command right. I have got two text files. cat file1 @ABC:11:ABC:1:...
0
votes
3answers
3k views

Grep for word stem and print only word (and not line)

I'm trying to print only a matching word that are found through a regular expression. Below, I want all the OPENSSL_NO_* options that are present in the source code: $ grep -IR OPENSSL_NO fuzz/asn1.c:...
0
votes
2answers
40 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 ...
0
votes
2answers
32 views

How to grep a string with any two numbers that match or that have the first number one less than the second number?

I have a tab delimited file with four columns. I would like to grep for the lines that have a specific pattern in column 1, where it says apple M of N.  I only want to extract the lines that have the ...
0
votes
2answers
60 views

AWK Split Variable

I have the following sequence in a file: AA MUi MUi-j MUi-N ABk ABk-l ABk-N MBk(i) MBk-l(i) MBk-l(i-j) MBk-l(i-N) MBk-N(i-j) MBk-N(i-N) I want to split it in small pieces: The first two chars will ...
0
votes
3answers
43 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"; ...
0
votes
2answers
47 views

Extracting lines to new files

Say I have a large CSV file with a header and several columns. For the purpose of this question I will consider a small file with just two columns. We can call it use_rep. user_id,rep 885,500K+ 22565,...
0
votes
1answer
163 views

Log the files found with the linux find and sed command

I have a folder with txt, html, jpg and png files. Now I want to go over all textbased files (txt and html) and start a search and replace. I did the following and it is working fine: find ./ -...
0
votes
1answer
398 views

How to negate Bash's Regular Expression Matching Operator

Bash has the =~ regular expression matching operator. Here is an example of using it: #!/bin/bash input=$1 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] # ^ ...
0
votes
2answers
443 views

grep multiple patterns with different context

I have a file roughly like header_one param1 param2 ... data_one data1 data2 data3 data4 ... header_two param1 param2 ... data_two data1 data2 data3 data4 I'd like to extract all header blocks ...
0
votes
2answers
102 views

Pattern recognition betweel two sentences in a file which has spaces and special characters?

I have a file in which I want to print all the lines between two patterns. Pattern1 = # Begin TRACE A Data and Pattern 2 =# Done Data $capture, I want to print every line between pattern1 and pattern2....
0
votes
1answer
21 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
27 views

Extract pattern in perl

Is it possible to have just one regex which can extract the pattern "Mango-1.whatever.." in any of these 3 conditions (i.e. $dummy). I keep printing everything after this pattern. If I use comma in ...
0
votes
1answer
21 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 ...
0
votes
0answers
20 views

grep -f not working with file

I have a file with having patterns separated by new line characters. which matches the patterns i want to find from a file. but when i run command grep -f patterns_file words_file It gives no ...
0
votes
0answers
16 views

Find longest matching portion (not in the same offset) within two files

I am wondering if I can compare two files and find longest matching portion not in the same offset Example: File1: 0022222100022222 File2: 0333310003333333 I need to find the matching value (1000) ...
0
votes
4answers
52 views

How to extract word which has only numbers separated by 'dot'

From below lines abcd efgh ijhk lmn opqrs 9.0.8.2c tuv wxyz abcd efgh ijhk lmn opqrs 8.1.3.9b How to extract only 9.0.8.2c 8.1.3.9b
0
votes
1answer
233 views

Sed search and delete matching two patterns one of which is a variable

I have a file that contains multiple instances of code that I need to delete. Here is an example: !/bin/bash mkdir /rootdir/pipeline_runs/oncology/analysis/sample_1_NA172_1 cd /rootdir/pipeline_runs/...
0
votes
1answer
15 views

how to match a column of a file 1 to text of another file 2 then if it matches print file 2

File 1 Name,TaxID,Lineage,Count,Proportion_All(%),Proportion_Classified(%) Acaryochloris marina,155978,Bacteria;Cyanobacteria;;Synechococcales;Acaryochloridaceae;Acaryochloris,4,0.000218669,0....
0
votes
1answer
43 views

For a given string(2 or 3 word) in a .txt file and print the whole line. [duplicate]

My .txt file contains these kind of content : The Law and Lawyers of Pickwick, by Frank Lockwood 21214 Frankenstein, by Mary Wollstonecraft 20 The first ...
0
votes
1answer
236 views

print out lines if first three columns match the first three columns in another file

I have file1 which has 3 columns and file2 which has 8 columns. What I wanted to do was if the first 3 columns in file 2 were found in file 1, then print out the entire 8 columns in file 2. I have ...
0
votes
1answer
141 views

Pattern recognition and summing columns between two csv/excel files

I have two files, in which one has data on each person's start date, increment, promotion and other details, and the other file has their research output over the years. Can a script be written in ...
0
votes
0answers
57 views

grep a sequnce of 'AB<anything>CDE' [duplicate]

I want to grep for 'AB...CDE', where ... stands for anythings until CDE first occurs. I have tried grep 'AB*CDE' filename but this matches til the last occurrence of CDE. How can fix this? ...
-2
votes
2answers
81 views

Find longest stretch of pattern from centre of sequence

How to calculate the longest consecutive pattern of C/T anywhere in sequence and longest consecutive pattern of C/T from centre CCCCTGTTGCCAAACAATGC TTTTCCCGCCTTTGGCCTAC ...
-3
votes
1answer
117 views

Extract data between two patterns from a huge (forced) text file

I have filename.json. If I parse it in terminal with file filename.json output is: filename.json: UTF-8 Unicode text, with very long lines wc -l filename.json 1 filename.json If I parse it ...