awk - multiple match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - multiple match
# 8  
Old 06-08-2014
Quote:
Originally Posted by Akshay Hegde
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

That work great! You also reminded me about escaping the parenthesis and ;. I changed the CH regex because I need to catch single and possibly double digits.
Code:
match($0, /CH: [^ ]/);

to

match($0, /CH:.[0-9][0-9]?/);

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

[0-9][0-9] matches double-digit numbers 00 to 99

[0-9][0-9]? matches 1 or 2 digit numbers

---------- Post updated at 12:39 PM ---------- Previous update was at 12:27 PM ----------

if you have gawk try this

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

# 10  
Old 06-08-2014
With today's digital channels (e.g., 1-12 and 44-1), you might be better off using just one match() for the parenthesized string. Of course, all of this assumes that there are no parentheses in either set of randcom junk:
Code:
awk '$5 == "CH:" {match($0, /[(].*[)]/); print $2, $5, $6, substr($0, RSTART+1, RLENGTH-2)}'

or if you want the parentheses as shown in your output samples:
Code:
awk '$5 == "CH:" {match($0, /[(].*[)]/); print $2, $5, $6, substr($0, RSTART, RLENGTH)}'

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