AWK break string into fields + pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK break string into fields + pattern match
# 1  
Old 10-13-2010
AWK break string into fields + pattern match

I am trying to break a string into separate fields and print the field that matches a pattern. I am using awk at the moment and have gotten this far:

Code:
awk '{for(i=1;i<=NF;++i)print "\t" $i}' longstring

This breaks the string into fields and prints each field on a separate line.
I want to add a pattern match to this using the posix version of awk, or replace the whole thing with sed or perl.
# 2  
Old 10-13-2010
Ok!

On each field, you can use AWK's "match" function, if it returns different from 0, it found the regex, so you can apply "sub" or "gsub" function on that field.

From AWK's man pages:
Code:
       match(s, r [, a])       Returns the position in s where the regular expression r occurs, or 0 if r is not present, and sets the values of RSTART and RLENGTH.  Note that
                               the argument order is the same as for the ~ operator: str ~ re.  If array a is provided, a is cleared and then elements 1 through n  are  filled
                               with  the  portions of s that match the corresponding parenthesized subexpression in r.  The 0āth element of a contains the portion of s matched
                               by the entire regular expression r.  Subscripts a[n, "start"], and a[n, "length"] provide the starting index in the string  and  length  respec-
                               tively, of each matching substring.

       gsub(r, s [, t])        For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions.  If t  is
                               not supplied, use $0.  An & in the replacement text is replaced with the text that was actually matched.  Use \& to get a literal &.  (This must
                               be typed as "\\&"; see GAWK: Effective AWK Programming for a fuller discussion of the rules for &ās and backslashes in the replacement  text  of
                               sub(), gsub(), and gensub().)

I hope it helps!

Regards.
# 3  
Old 10-14-2010
Thanks Felipe, the match function looks good.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 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. UNIX for Beginners Questions & Answers

awk script for pattern match and line break

Hi, I have input which reads like 9089.00 ----- kl jkjjljk lkkk; (909099) 9097.00 ----- HGJJHHJ jcxkjlkjvhvlk jhdkjksdfkhfskd 898.00 ----- HHHH I am trying to do something like this - As soon as I found pattern match "XYZ.00-----" it will insert a line break to the input and will go to... (3 Replies)
Discussion started by: Indra2011
3 Replies

4. Shell Programming and Scripting

awk partial string match and add specific fields

Trying to combine strings that are a partial match to another in $1 (usually below it). If a match is found than the $2 value is added to the $2 value of the match and the $3 value is added to the $3 value of the match. I am not sure how to do this and need some expert help. Thank you :). file ... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Pattern Match and Rearrange the Fields in UNIX

For an Output like below Input : <Subject A="I" B="1039502" C="2015-06-30" D="010101010101"> Output : <Subject D="010101010101" B="1039502" C="2015-06-30" A="I"> I have been using something like below but not getting the desired output : awk -F ' ' '/Subject/ BEGIN{OFS=" ";}... (19 Replies)
Discussion started by: arunkesi
19 Replies

6. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

7. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

8. Shell Programming and Scripting

pattern match in a string

Hello, Please see below line code: #!/bin/ksh set -x /usr/bin/cat /home/temp |while read line do if ] then echo "matched" else echo "nope" fi done content of filr temp is as below (4 Replies)
Discussion started by: skhichi
4 Replies

9. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

10. UNIX for Advanced & Expert Users

how can awk match multi pattern in a string

Hi all, I need to category the processes in my system with awk. And for now, there are several command with similar name, so i have to match more than one pattern to pick it out. for instance: binrundb the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"... (5 Replies)
Discussion started by: sleepy_11
5 Replies
Login or Register to Ask a Question