gawk

Timestamp to Epoch in a CSV file with GAWK

本小妞迷上赌 提交于 2019-11-29 07:14:18
Looking to convert human readable timestamps to epoch/Unix time within a CSV file using GAWK in preparation for loading into a MySQL DB. Data Example: {null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647 Looking to take column 6, Tuesday, November 26 12:17 PM, and convert to epoch time for storage. All times shown will be in EST format. I realize AWK is the tool for this, but can't quite seem to structure the command. Currently have: cat FILE_IN.CSV | awk 'BEGIN {FS=OFS=";"}{$6=strftime("%s")} {print}' However this returns: {null};2013-11-26;Text &

Sort associative array with AWK

喜欢而已 提交于 2019-11-29 06:53:48
问题 Here's my array (gawk script) : myArray["peter"] = 32 myArray["bob"] = 5 myArray["john"] = 463 myArray["jack"] = 11 After sort, I need the following result : bob 5 jack 11 peter 32 john 463 When i use "asort", indices are lost. How to sort by array value without losing indices ? (I need ordered indices based on their values) (I need to obtain this result with awk/gawk only, not shell script, perl, etc) If my post isn't clear enough, here is an other post explaining the same issue : http://www

SED or AWK replace all with patterns from another file

南笙酒味 提交于 2019-11-29 02:11:06
I am trying to do pattern replacement using SED script but its not working properly sample_content.txt 288Y2RZDBPX1000000001dhana JP2F64EI1000000002d EU9V3IXI1000000003dfg1000000001dfdfds XATSSSSFOO4dhanaUXIBB7TF71000000004adf 10Q1W4ZEAV18LXNPSPGRTTIDHBN1000000005egw patterns.txt 1000000001 9000000003 1000000002 2000000001 1000000003 3000000001 1000000004 4000000001 1000000005 5000000001 Expected output 288Y2RZDBPX9000000003dhana JP2F64EI2000000001d EU9V3IXI3000000001dfg9000000003dfdfds XATSSSSFOO4dhanaUXIBB7TF74000000001adf 10Q1W4ZEAV18LXNPSPGRTTIDHBN5000000001egw I am able to do with single

GNU awk: accessing captured groups in replacement text

非 Y 不嫁゛ 提交于 2019-11-29 01:02:22
This seems like it should be dirt simple, but the awk gensub/gsub/sub behavior has always been unclear to me, and now I just can't get it to do what the documentation says it should do (and what experience with a zillion other similar tools suggests should work). Specifically, I want to access "captured groups" from a regex in the replacement string. Here's what I think the awk syntax should be: awk '{ gsub(/a(b*)c/, "Here are bees: \1"); print; }' That should turn "abbbc" into "Here are bees: bbb". It does not, at least not for me in Ubunutu 9.04. Instead, the "\1" is rendered as a ^A; that

Print all Fields with AWK separated by OFS

旧巷老猫 提交于 2019-11-28 19:15:19
Is there a way to print all records separated by the OFS without typing out each column number. #Desired style of syntax, undesired result [kbrandt@glade: ~] echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {print $0}' 1 2 3 4 #Desired result, undesired syntax [kbrandt@glade: ~] echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-) "}; {print $1,$2,$3,$4}' 1 :-) 2 :-) 3 :-) 4 This is a variation on the first style: echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {$1=$1; print $0}' Results: 1 :-( 2 :-( 3 :-( 4 Explanation: the $1=$1 is to rebuild the record, using the current OFS (you can also see http://www.gnu

print every nth line into a row using gawk

笑着哭i 提交于 2019-11-28 17:47:54
问题 I have a very huge file in which I need to obtain every nth line and print it into a row. My data: 1 937 4.320194 2 667 4.913314 3 934 1.783326 4 940 -0.299312 5 939 2.309559 6 936 3.229496 7 611 -1.41808 8 608 -1.154019 9 606 2.159683 10 549 0.767828 I want my data to look like this: 1 937 4.320194 3 934 1.783326 5 939 2.309559 7 611 -1.41808 9 606 2.159683 This is of course an example, I want every 10th line for my huge data file. I tried this so far: NF == 6 { if(NR%10) {print;} } 回答1: To

Print Field 'N' to End of Line

微笑、不失礼 提交于 2019-11-28 14:17:22
I would like to have help or direction on a problem I have in awk. I have a tab-delimited file with more than 5 fields. I want to output the fields excluding the first 5 fields. Could you please tell how to write an awk script to accomplish this task? Best, jianfeng.mao Do Note the following kind comment: There are many fields in my files. Different lines have a different number of fields. The number of fields per line is not standard. I agree with matchew's suggestion to use cut : it's the right tool for this job. But if this is just going to become a part of a larger awk script, here's how

gawk floating-point number localization

百般思念 提交于 2019-11-28 05:46:08
问题 I want gawk to parse number using comma , as the decimal point character. So I set LC_NUMERIC to fr_FR.utf-8 but it does not work: echo 123,2 | LC_NUMERIC=fr_FR.utf-8 gawk '{printf ("%.2f\n", $1 + 0) }' 123.00 The solution is to specify option --posix or export POSIXLY_CORRECT=1 but in this case the GNU awk extensions are not available, for example delete or the gensub function: echo 123,2 | LC_NUMERIC=fr_FR.utf-8 gawk --posix '{printf ("%.2f\n", $1 + 0) }' 123,20 Is it possible to have gawk

How to filter logs easily with awk?

邮差的信 提交于 2019-11-28 01:37:40
Suppose I have a log file mylog like this: [01/Oct/2015:16:12:56 +0200] error number 1 [01/Oct/2015:17:12:56 +0200] error number 2 [01/Oct/2015:18:07:56 +0200] error number 3 [01/Oct/2015:18:12:56 +0200] error number 4 [02/Oct/2015:16:12:56 +0200] error number 5 [10/Oct/2015:16:12:58 +0200] error number 6 [10/Oct/2015:16:13:00 +0200] error number 7 [01/Nov/2015:00:10:00 +0200] error number 8 [01/Nov/2015:01:02:00 +0200] error number 9 [01/Jan/2016:01:02:00 +0200] error number 10 And I want to find those lines that occur between 1 Oct at 18.00 and 1 Nov at 1.00. That is, the expected output

Timestamp to Epoch in a CSV file with GAWK

戏子无情 提交于 2019-11-28 00:48:45
问题 Looking to convert human readable timestamps to epoch/Unix time within a CSV file using GAWK in preparation for loading into a MySQL DB. Data Example: {null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17 PM;1;1385845647 Looking to take column 6, Tuesday, November 26 12:17 PM, and convert to epoch time for storage. All times shown will be in EST format. I realize AWK is the tool for this, but can't quite seem to structure the command. Currently have: cat FILE_IN