gawk

Check if given strftime format matches a date

此生再无相见时 提交于 2019-12-13 02:34:32
问题 I have strftime format of time, let's say (%Y-%m-%d %H:%M:%S) and a file which should contain this kind of data e.g. (2012-02-11 17:15:00) . I need to check if given pattern actually matches the data. How to approach this? awk, date? EDIT: More info: The user enters the strftime format, let's say on input. Then he enters a file which should contain those dates. I need to make sure, that those data are valid (he didn't make a mistake). So I need to check the rows in the input file and see, if

How to use awk/shell scripting to do SQL Where clause and SQL join like filtering and merging of rows and columns?

六眼飞鱼酱① 提交于 2019-12-12 18:23:40
问题 I have a huge data set with say 15 - 20 GB and it is a tab delimited file. While I can either do it in Python or in SQL, It would be easier and simple to have it done in Shell script to avoid moving the csv files Say, For example, taking a pipe delimited file input: ---------------------------------------- Col1 | Col2 | Col3 | Col4 | Col5 | Col6 ---------------------------------------- A | H1 | 123 | abcd | a1 | b1 ---------------------------------------- B | H1 | 124 | abcd | a2 | b1 -------

awk variable assignment statement explanation needed

試著忘記壹切 提交于 2019-12-12 10:58:34
问题 ok, straight to the point, here is the codes, I formatted the codes a little to make it easy to read: awk '{ t=$0 ; $0=t ; $0=// ; print "$0=// ; value of $0 is ",$0 $0=t ; $0=/./ ; print "$0=/./ ; value of $0 is ",$0 $0=t ; $0=/*/ ; print "$0=/*/ ; value of $0 is ",$0 $0=t ; $0=/**/ ; print "$0=/**/ ; value of $0 is ",$0 $0=t ; $0=/[0-9]/ ; print "$0=/[0-9]/ ; value of $0 is ",$0 $0=t ; $0=/[a-z]/ ; print "$0=/[a-z]/ ; value of $0 is ",$0 $0=t ; $0=/[0-9][a-z]/ ; print "$0=/[0-9][a-z]/ ;

awk change once per file

一世执手 提交于 2019-12-12 06:44:54
问题 awk -i inplace ' BEGIN {FS=" "} BEGINFILE {changed=0} { print;if ($1 == "namespace" && !changed) {print "foo";changed=1} } ' * Is there a more elegant way to do this? Some built-in construct I missed? I am running GNU Awk 4.1.0 (and I am superb glad for -i inplace). 回答1: awk -i inplace '$1=="namespace" && !seen[ARGIND]++ {$0=$0 ORS "foo"} 1' * FS=" " is the default, no need to specify it explicitly. 回答2: You could say: ... -F' ' '$1 == "namespace" && !a {$0=$0 RS "foo";a=1}1' file or even: ..

bash: insert a line after a pattern using gawk

旧街凉风 提交于 2019-12-12 04:23:49
问题 I am trying to insert a line after the Pattern using gawk . Let's say, file aa contains 11 22 33 11 22 33 I'm using gawk to insert 222 only after first 22, i.e. after insertion, my aa file would contain: 11 22 222 33 11 22 33 But, if I use: gawk -v nm=222 '/22/ {if (done++ == 0) print;print nm;next}1' aa The file aa contains: 11 22 222 33 11 222 33 (I don't want second replacement of 22 by 222 and like to retain 22 as-is, and no more insertion, i,e,. insert 222 only once after first 22.

Awk or Gawk to do data matching and merging

邮差的信 提交于 2019-12-12 01:36:46
问题 Related question: https://stackoverflow.com/posts/18164848 The input file input.txt is a tab delimited unicode txt with a A e f m b B g h c C i j b B k l I want to match by the first and second column and merge. So I want to get output.txt with a A e f m b B g h k l c C i j The code has to detect the maximum number of columns in the input. Since it is 5 in this example, "k l" were put from 6th column. Actually I almost managed to do this using Matlab when they are all numbers. But oh, when

Using sed piped with w command show user with the largest idle time

不问归期 提交于 2019-12-12 01:29:20
问题 the w command produces something like this: 01:19:02 up 53 days, 10:44, 15 users, load average: 0.00, 0.02, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT higrheht pts/5 c-13-76-207-161. 23:21 2:05 0.07s 0.07s -bash sgergrgr pts/6 c-97-164-31-14.h 00:54 2.00s 0.04s 0.04s -bash jwegrgrng pts/14 c-23-71-12-251.h 22:48 8:03 0.07s 0.06s vim s2 hiqrefan pts/18 c-13-31-206-169. 23:19 0.00s 0.01s 0.01s -bash hqeffran pts/19 c-19-71-206-169. 23:19 1:58m 0.02s 0.02s -bash aqrgri pts/20 c-84-6-212-27.hs

Shell: delete every second match against a regex in a file

夙愿已清 提交于 2019-12-11 19:34:13
问题 Say I have come up with a regex matching a piece of data; the regex contains 2 sed groups (sub-expressions enclosed in ( and ) ). Also say that this regex is duplicated 9 times to match a whole line. The problem I am facing is how to delete (in an elegant way) every second match against the regex. 回答1: Let's say you have the following string and want to remove the occurrences of bar : foo bar foo bar foo bar You can use the following sed command, note the option g which makes the substitution

AWK - does an if condition calculate an array input

泄露秘密 提交于 2019-12-11 13:57:51
问题 Question so I have the following line of code: if (sum[msg,h]/summsg[msg,h] != 0) printf ("%9.2f\n",sum[msg,h]/summsg[msg,h]) msg is a message array holds 10 distinct values hr holds all hours present in a log file. the sum[] array is addind together values of a field ( sum[$5,$3] += $11 ) and the summsg[] array is counting the number of lines ( summsg[$5,$3]++ ) This is retuning a fatal: division by zero attempted error, but I thorght that awk would evaluate the sum[msg,h]/summsg[msg,h] != 0

Notepad++ find all lines with open parentheses but no close parentheses and add closer parentheses for AWK script error

蹲街弑〆低调 提交于 2019-12-11 13:23:12
问题 I have several large log files that include lines in the following format: /resource/text_(moretext Now these need to be closed with a ")", but the file is way too large to do this manually. Unfortunately the text within the lines can be anything. So I think I need some expression that is able to find all lines that have "(" and no ")". Then these lines have to be replaced with the exact same content but with ")" added to the end. So it should look like this: Before: /resource/text_(moretext