gawk

gawk FS to split record into individual characters

落花浮王杯 提交于 2019-12-08 17:36:07
问题 If the field separator is the empty string, each character becomes a separate field $ echo hello | awk -F '' -v OFS=, '{$1 = NF OFS $1} 1' 5,h,e,l,l,o However, if FS is a regex that can possibly match zero times, the same behaviour does not occur: $ echo hello | awk -F ' *' -v OFS=, '{$1 = NF OFS $1} 1' 1,hello Anyone know why that is? I could not find anything in the gawk manual. Is FS="" just a special case? I'm most interested in understanding why the 2nd case does not split the record

Are fields defined in the END block in AWK?

人走茶凉 提交于 2019-12-08 17:28:15
问题 What would happen when using $1 , $2 ... in the END block, like: awk '{print $3}END{print $1 $2}' I found that $1 and $2 retain the values from the last record. Is this behaviour guaranteed by the standard or is it implementation-specific? 回答1: Checking the docs we see that it is implementation-specific : Traditionally, due largely to implementation issues, $0 and NF were undefined inside an END rule. The POSIX standard specifies that NF is available in an END rule. It contains the number of

Is it possible to append an item to an array in awk without specifying an index?

那年仲夏 提交于 2019-12-08 14:35:29
问题 I realize that awk has associative arrays, but I wonder if there is an awk equivalent to this: http://php.net/manual/en/function.array-push.php The obvious workaround is to just say: array[$new_element] = $new_element However, this seems less readable and more hackish than it needs to be. 回答1: I don't think an array length is immediately available in awk (at least not in the versions I fiddle around with). But you could simply maintain the length and then do something like this: array

backslash in gawk fields

﹥>﹥吖頭↗ 提交于 2019-12-08 11:56:11
问题 I've just been made into checking all my output files with gawk which I avoid as much as I can. How does gawk 'NF \!= 6' file differ from gawk 'NF != 6' file that is, how does the backslash change the meaning of this expression? Should it output lines with number of fields different than 6 and ending with backslash? I'm getting the following error on my files: gawk: ^ backslash not last character on line Anybody? 回答1: If you use double quotes instead of single quotes then ! is a special

Combine split lines with awk / gawk

a 夏天 提交于 2019-12-08 05:34:27
问题 A system wraps lines in a log file if they exceed X characters. I am trying to extract various data from the log, but first I need to combine all the split lines so gawk can parse the fields as a single record. For example: 2012/11/01 field1 field2 field3 field4 fi eld5 field6 field7 2012/11/03 field1 field2 field3 2012/12/31 field1 field2 field3 field4 fi eld5 field6 field7 field8 field9 field10 field11 field12 field13 2013/01/10 field1 field2 field3 2013/01/11 field1 field2 field3 field4 I

How to convert a date string to timestamp in gawk?

主宰稳场 提交于 2019-12-08 05:17:15
问题 I am scanning through a log file formatted like this: 76.69.120.244 - - [09/Jun/2015:17:13:18 -0700] "GET /file.jpg HTTP/1.1" 200 22977 "http://example.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36" "16543" "ewr1" "0.002" "CA" "Bell Canada" "2" 76.69.120.244 - - [09/Jun/2015:17:13:19 -0700] "GET /differentfile.bin HTTP/1.1" 206 453684 "http://example.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,

Change a string in a file with sed?

我怕爱的太早我们不能终老 提交于 2019-12-07 18:37:28
问题 I have a inputfile with template as shown below. I want to change the Version: using sed. Package: somename Priority: extra Section: checkinstall Maintainer: joe@example.com Architecture: i386 Version: 3.1.0.2-1 Depends: Provides: somename Description: some description Currently I am getting the current version using grep -m 1 Version inputfile | sed 's/[:_#a-zA-Z\s"]*//g' and I am trying to replace the current version with sed 's/3.1.0.2-1/3.1.0.2/' inputfile However this does not seem to

matching a specific substring with regular expressions using awk

妖精的绣舞 提交于 2019-12-07 12:41:39
问题 I'm dealing with a specific filenames, and need to extract information from them. The structure of the filename is similar to: "20100613_M4_28007834.005_F_RANDOMSTR.raw.gz" with RANDOMSTR a string of max 22 chars, and which may contain a substring (or not) with the format "-W[0-9].[0-9]{2}.[0-9]{3}". This substring also has the unique feature of starting with "-W". The information I need to extract is the substring of RANDOMSTR without this optional substring. I want to implement this in a

Combine split lines with awk / gawk

假装没事ソ 提交于 2019-12-06 16:24:57
A system wraps lines in a log file if they exceed X characters. I am trying to extract various data from the log, but first I need to combine all the split lines so gawk can parse the fields as a single record. For example: 2012/11/01 field1 field2 field3 field4 fi eld5 field6 field7 2012/11/03 field1 field2 field3 2012/12/31 field1 field2 field3 field4 fi eld5 field6 field7 field8 field9 field10 field11 field12 field13 2013/01/10 field1 field2 field3 2013/01/11 field1 field2 field3 field4 I want to return 2012/11/01 field1 field2 field3 field4 field5 field6 field7 2012/11/03 field1 field2

AWK: redirecting script output from script to another file with dynamic name

浪尽此生 提交于 2019-12-06 07:36:42
问题 I know I can redirect awk's print output to another file from within a script, like this: awk '{print $0 >> "anotherfile" }' 2procfile (I know that's dummy example, but it's just an example...) But what I need is to redirect output to another file, which has a dynamic name like this awk -v MYVAR"somedinamicdata" '{print $0 >> "MYWAR-SomeStaticText" }' 2procfile And the outpus should be redirected to somedinamicdata-SomeStaticText . I know I can do it via: awk '{print $0 }' 2procfile >> "