awk match function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk match function
# 1  
Old 02-15-2012
Question awk match function

Hello,

I tried an example with 'match' and print out RSTART and RLENGTH.
Sadly it doesn't work.

Code:
test.txt >
yesterday was tuesday
today is wednesday
tomorrow is thursday

Code:
awk -F\\n '{ match( '/day/', $1 ); printf RSTART "," RLENGTH "\n" }' test.txt

output:
Code:
0,-1

# 2  
Old 02-15-2012
give a try to :
Code:
awk -F\\n '{ match($1,"day");printf RSTART "," RLENGTH "\n" }' text.txt

and (for education purpose) also give a try to:

Code:
awk -F\\n '{ match($1,"day");print substr($1,RSTART,RLENGTH),$2,$3}' text.txt

You can find the awk function's syntax here

Last edited by ctsgnb; 02-15-2012 at 08:46 AM..
# 3  
Old 02-15-2012
oh, thanx and sorry for this unnecessary question Smilie
# 4  
Old 02-15-2012
While we are at it. If we make FS equal to \n, while maintaining RS as is, we are effectively making $0 equal to $1. So the above statements should be equivalent to:
Code:
awk '{ match( $0, "day" ); print RSTART "," RLENGTH }' infile

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 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

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

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

5. Shell Programming and Scripting

awk match help

Trying to match $1 of file2.txt with $1 of file 1.txt and output the entire line of the match. Thank you :) awk 'NR==FNR{A=$2; next} A {$2=$2 " " A}1' file1.txt file2.txt > output.txt file1.txt LMNA 285.195652 MZT1P1 166.852113 HFM1 129.847940 file2.txt LMNA PTPN11... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

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

8. Shell Programming and Scripting

array and awk match function in SunOS 5.10

Hi Experts, Need help in writing a shell script in SunOS 5.10. I want to use array but it is not running in SunOs where as it is running in unix. pls help Want to print the alue store in array as below but it is giving error. p=1 p=6 p=15 p=20 for i in 1 2 3 4 do echo ${p} done ... (2 Replies)
Discussion started by: forroughuse
2 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but... My input data: (from an input file) i only show the first record in my file.. all other records are similar. mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql... (2 Replies)
Discussion started by: danmauer
2 Replies
Login or Register to Ask a Question