print lines which match multiple patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print lines which match multiple patterns
# 1  
Old 07-12-2010
print lines which match multiple patterns

Hi,

I have a text file as follows:
Code:
11:38:11.054 run1_rdseq    avg_2-5 999988.0000  1024.0000   
11:50:52.053 run3_rdrand    999988.0000   1135.0 128.0417   
11:53:18.050 run4_wrrand    avg_2-5 999988.0000  8180.5833
11:55:42.051 run4_wrrand    avg_2-5 999988.0000   213.8333
11:55:06.053 run4_wrrand    999988.0000   9807 510.2083

I have a variable $run, that contains all the unique values of column 2. I need to print lines that match $run and the pattern 'avg'. So from the above text file I would want to print :
Code:
11:38:11.054 run1_rdseq    avg_2-5 999988.0000  1024.0000   
11:53:18.050 run4_wrrand    avg_2-5 999988.0000  8180.5833
11:55:42.051 run4_wrrand    avg_2-5 999988.0000   213.8333

If I do something like :
Code:
egrep -e '(run4_wrrand.*avg)' textfile.txt

, I get the right output. But if I do:
Code:
egrep -e '($run.*avg)' flatfile.html

I get no output. I know shell variables can not be used in this way, but how else can I achieve the above output?


Thanks
Anna

Last edited by Scott; 07-12-2010 at 05:02 PM.. Reason: Please use code tags for input, output and log files as well as code
# 2  
Old 07-12-2010
Hi,

try:

Code:
run=run
egrep -e "$run.*avg" file

HTH Chris
This User Gave Thanks to Christoph Spohr For This Post:
# 3  
Old 07-12-2010
Wow!!! I had been scrating my head for a couple of hrs on this and all that was needed to fix this was the double quotes!!!!. Thanks a ton Chris!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

4. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

5. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

6. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

7. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

9. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

10. Shell Programming and Scripting

Removing file lines that each match to a different patterns

I have a very large file (10,000,000 lines), that contains a sample id and a property of that sample. I have another file that contains around 1,000,000 lines with sample ids that I want to remove from the original file (create a new file without these lines). I know how to do this in Perl, but it... (9 Replies)
Discussion started by: Jo_puzzled
9 Replies
Login or Register to Ask a Question