Pages

Thursday, April 4, 2019

AWK match and substr function

MATCH Function

match(string, regexp [, array])

Search string for the longest, leftmost substring matched by the regular expression and return the character position (index) at which that substring begins (one, if it starts at the beginning of string). If no match is found, return zero.

The regexp argument may be either a regexp constant (/…/) or a string constant ("…"). 

Note: The order of the first two arguments is the opposite of most other string functions that work with regular expressions, such as sub() and gsub(). It might help to remember that for match(), the order is the same as for the ‘~’ operator: ‘string ~ regexp’.

The match() function sets the predefined variable RSTART to the index. It also sets the predefined variable RLENGTH to the length in characters of the matched substring. If no match is found, RSTART is set to zero, and RLENGTH to -1.

SUBSTR Function

substr(string, start [, length ])

Return a length-character-long substring of string, starting at character number start. The first character of a string is character number one.49 For example, substr("washington", 5, 3) returns "ing".

If length is not present, substr() returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". The whole suffix is also returned if length is greater than the number of characters remaining in the string, counting from character start.

example:

AB: 20190131  13 J-1|19:30:00.000000000 18:06:00.000000000 123466  50 @TEST . "" 1234 - I . ".." "" "" "TEST TEXT 1" "TEXT 2: SAMPLE TEXT I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find f.==Required file.csv.gz FIELD*SERVER-TIME*05:29:51.981378000" "" NoTime

       
Command : awk 'match($0,/==.*?.csv.gz/){print $3","substr($0, RSTART+2, RLENGTH-2)}'  Sample file


Output  : 13,Required file.csv.gz       
 

No comments:

Post a Comment