awk - multiple match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - multiple match
# 1  
Old 06-08-2014
awk - multiple match

I need to exttract the color fields shown below. The parenthesis can contain almost anything.
Updated:
Code:
11b -98db random junk CH: 1 random junk (a space) random junk
11g -82db random junk CH: 2 random junk (most_characters) random junk
11n -73db random junk CH: 11 random junk (sometimes multiple spaces) random junk

The following command extracts the 3rd "CH: XX" column.
Code:
awk 'match($0, /CH:.[0-9][0-9]?/) {print substr($0, RSTART, RLENGTH)}'

Can I have multiple matches and print them somehow, or is there a better way?
Moderator's Comments:
Mod Comment Please use CODE tags for sample code, sample input, and sample output.

Last edited by Kiah07; 06-08-2014 at 01:28 AM.. Reason: Add CODE tags.
# 2  
Old 06-08-2014
Quote:
The following command extracts the 3rd "CH: XX" column.

Code:

awk 'match($0, /CH:.[0-9][0-9]?/) {print substr($0, RSTART, RLENGTH)}'
Why not just awk '$5 ~ /CH/ {print $5, $6}'

Could you post the desired output? Is it...?
Quote:
-98db CH: 1 (a space)
# 3  
Old 06-08-2014
Thanks Aia, that is much cleaner than mine. The desired output is:
Code:
-98db CH: 1 (a space)

Without the parenthesis.
# 4  
Old 06-08-2014
Try:
Code:
awk 'match($0, /CH:/) {print $2, substr($0, RSTART)}'

# 5  
Old 06-08-2014
Quote:
Originally Posted by Don Cragun
Try:
Code:
awk 'match($0, /CH:/) {print $2, substr($0, RSTART)}'

Thanks, sorry but I eddied the post. I had more random things between the three things to be extracted. I really need something like:
match($0, /db/) print substring A
match($0, /CH/) print substring B
match($0, /(a space)/) print substring C

I appreciate your help.
# 6  
Old 06-08-2014
You have to write your query clearly,

Try :

Code:
$ awk '{first=$2;match($0, /CH: [^ ]/);second=substr($0, RSTART,RLENGTH);match($0,/\(.*)/);third=substr($0,RSTART+1,RLENGTH-2);print first,second,third}' file

-98db CH: 1 a space
-82db CH: 2 most_characters
-73db CH: 1 sometimes multiple spaces

# 7  
Old 06-08-2014
Code:
awk '/CH/ match($0, /-[0-9]*db/){ one=substr($0, RSTART, RLENGTH)} match($0, /CH: [0-9]*/) {two=substr($0, RSTART, RLENGTH)} match($0, /\(.*\)/) {three=substr($0, (RSTART + 1), (RLENGTH-2))}{print one, two, three}'

---------- Post updated at 11:16 PM ---------- Previous update was at 10:52 PM ----------

Got perl?

Code:
perl -nle 'print "$1 $2 $3" if $_ =~ /(-[0-9]*db).*(CH: [0-9]*).*\((.*)\)/;'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 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 multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

6. Shell Programming and Scripting

Simple awk match for multiple lines

Is there a simple way to use awk to match multiple lines?? Somehow using \n isn't working for me. Ultimately I'm trying to insert "WWW" 3 lines above "eee". input aaa bbb ccc ddd eee fff output aaa bbb WWW ccc ddd eee (1 Reply)
Discussion started by: pxalpine
1 Replies

7. Shell Programming and Scripting

awk : Filter a set of data to parse header line and last field of multiple same match.

Hi Experts, I have a data with multiple entry , I want to filter PKG= & the last column "00060110" or "00088150" in the output file: ############################################################################################### PKG= P8SDB :: VGS = vgP8SOra vgP8SDB1 vgP8S001... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

9. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

10. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies
Login or Register to Ask a Question