gawk

any way to access the matched groups in action? [duplicate]

扶醉桌前 提交于 2019-12-22 04:57:18
问题 This question already has answers here : AWK: Access captured group from line pattern (6 answers) Closed 5 years ago . I often find myself doing the same match in the action as the pattern, to access some part of the input record, e.g. /^Compiled from \"(.*)\"$/ { file_name = gensub("^Compiled from \"(.*)\"$", "\\1", "g"); print file_name; } So the regexp matching is done twice. Is there any way I can access \\1 in the action without matching again? I am trying to both reduce on pattert

single space as field separator with awk

自古美人都是妖i 提交于 2019-12-21 04:06:56
问题 I am dealing with a file where fields are separated by a single space. awk interprets the FS " " as "one or more whitespace", which misreads my file when one of the fields is empty. I tried using "a space not followed by a space"( " (?! )" ) as FS but awk does not support negative lookahead. Simple google queries like "single space field separator awk" only sent me to the manual page explaining the special treatment of FS=" " . I must have missed the relevant manual page... How can I use a

how to use sed, awk, or gawk to print only what is matched?

我怕爱的太早我们不能终老 提交于 2019-12-20 08:35:17
问题 I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract a specific value. I don't want to do search-and-replace. This is being called from bash. Let's use an example: Example regular expression: .*abc([0-9]+)xyz.* Example input file: a b c abc12345xyz a b c As simple as this sounds, I cannot figure out how to call sed/awk/gawk correctly. What I was

AWK - Is it possible to Breakdown a log file by a distinct field && by hour

巧了我就是萌 提交于 2019-12-20 06:40:44
问题 Question I am trying to find out if it is possible with awk alone to pass in a log file and then have awk output a distinct message with a breakdown of the hour (00-23) as well as a count, for that particular hour vs distinct message. Example Output requested Message1 00 13 01 30 ... 23 6 Message2 00 50 01 10 ... 23 120 etc, etc The input file would look a little something like the following: blah,blah 2016-06-24 00:30:54 blah Message1 7 rand rand2 2016-06-24 00:40:12 blah Message2 35 rand

Quantifiers in a regular expression used with awk behave unexpected

◇◆丶佛笑我妖孽 提交于 2019-12-20 03:17:22
问题 I want to process this list: (Of course this is just an excerpt.) 1 S3 -> PC-8-Set 2 S3 -> PC-850-Set 3 S3 -> ANSI-Set 4 S3 -> 7-Bit-NRC 5 PC-8-Set -> S3 6 PC-850-Set -> S3 7 ANSI-Set -> S3 This is what I did: awk -F '[[:blank:]]+' '{printf ("%s ", $2)}' list This is what I got: 1 2 3 4 5 6 7 Now I thought the quantifier + is equivalent to {1,} , but when I changed the line to awk -F '[[:blank:]]{1,}' '{printf ("%s ", $2)}' list I got just blanks and the whole line was read to $1. Can someone

Simple trouble with awk and regex

送分小仙女□ 提交于 2019-12-19 09:55:24
问题 echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}' Why I cannot get any output? Thank you. 回答1: You need to enable "interval expressions" in regular expression matching by specifying either the --posix or --re-interval option. e.g. echo xx y11y rrr | awk --re-interval '{ if ($2 ~ /y[1-5]{2}y/) print $3} From the man page: --re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). Interval expressions were not traditionally

Peek at next line, but don't consume it

二次信任 提交于 2019-12-18 14:52:35
问题 getline reads in the next line and increments the NR counter by 1. After using getline , awk resumes work with the next line. This is the desired behavior in most cases. In my special situation I need only to peek the next line and depending on its content I read the next line or I need to backtrack one line. How can I backtrack one line in awk ? I tried setting the NR counter manually to NR=NR-1 but this doesn't work. Or is there a method that only looks at the next line without changing NR

How to print awk's results with different colors for different fields?

对着背影说爱祢 提交于 2019-12-18 12:16:46
问题 This file has 3 fields. I wanted e.g. the first 2 fields in green, and the third in white (NB : black background), so I tried : awk '{print "\033[0;32m"$1"\033[0m", "\033[0;32m"$2"\033[0m", "\033[0;37m"$3"\033[0m"} }' chrono.txt and everything was green… How must I proceed (if it is possible) ? 回答1: To get color output from awk, you can use this approach. function red(s) { printf "\033[1;31m" s "\033[0m " } function green(s) { printf "\033[1;32m" s "\033[0m " } function blue(s) { printf "\033

How to set the field separator to an empty string?

自作多情 提交于 2019-12-18 08:56:46
问题 The awk manual indicates that both -v FS and -F are equivalent ways to set the field separator. The GNU Awk User’s Guide -> 4.5.4 Setting FS from the Command Line: FS can be set on the command line. You use the `-F' argument to do so. (...) The value used for the argument to `-F' is processed in exactly the same way as assignments to the built-in variable FS. However, I noticed that there is a difference if we set it to an empty string, it is not the same. Tested on my GNU Awk 4.1.1 . This

How to set the field separator to an empty string?

梦想的初衷 提交于 2019-12-18 08:56:04
问题 The awk manual indicates that both -v FS and -F are equivalent ways to set the field separator. The GNU Awk User’s Guide -> 4.5.4 Setting FS from the Command Line: FS can be set on the command line. You use the `-F' argument to do so. (...) The value used for the argument to `-F' is processed in exactly the same way as assignments to the built-in variable FS. However, I noticed that there is a difference if we set it to an empty string, it is not the same. Tested on my GNU Awk 4.1.1 . This