gawk

awk not rounding with OFMT and $0

北城以北 提交于 2019-11-27 22:41:35
问题 I'm printing an array with 100 columns and I would like all columns to have 2 decimals. I would like to use print $0 and not have to individually specify the format for all columns. OFMT does seen to work with $0: echo '0.77767686 0.76555555 0.6667667 0.77878878' |awk '{CONVFMT="%.2g";OFMT="%.2g";print ($0+0);print ($0+0)"";print $0}' Results: 0.78 0.78 0.77767686 0.76555555 0.6667667 0.77878878 回答1: Note that all input is treated as strings until implicitly converted by how it is used. OFMT

GNU awk: accessing captured groups in replacement text

霸气de小男生 提交于 2019-11-27 15:46:37
问题 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

Print second last column/field in awk

主宰稳场 提交于 2019-11-27 10:32:34
I want to print the second last column or field in awk. The number of fields is variable. I know that I should be able to use $NF but not sure how it can be used. And this does not seem to work: awk ' { print ( $NF-- ) } ' Chris Kannon awk '{print $(NF-1)}' Should work Small addition to Chris Kannon' accepted answer: only print if there actually is a second last column. ( echo | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 2 | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 2 3 | awk 'NF && NF-1 { print ( $(NF-1) ) }' ) Steve It's simplest: awk '

How to use “:” as awk field separator?

夙愿已清 提交于 2019-11-27 09:02:12
问题 Given following command: echo "1: " | awk '/1/ -F ":" {print $1}' why does awk output: 1: 回答1: "-F" is a command line argument not awk syntax, try: echo "1: " | awk -F ":" '/1/ {print $1}' 回答2: If you want to do it programatically, you can use the FS variable: echo "1: " | awk 'BEGIN { FS=":" } /1/ { print $1 }' Note that if you change it in the main loop rather than the BEGIN loop, it takes affect for the next line read in, since the current line has already been split. 回答3: You have

Print Field 'N' to End of Line

半腔热情 提交于 2019-11-27 08:18:42
问题 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. 回答1: I agree with matchew's suggestion to use cut : it's

Printing long integers in awk

橙三吉。 提交于 2019-11-27 05:27:44
I have a pipe delimited feed file which has several fields. Since I only need a few, I thought of using awk to capture them for my testing purposes. However, I noticed that printf changes the value if I use "%d" . It works fine if I use "%s" . Feed File Sample: [jaypal:~/Temp] cat temp 302610004125074|19769904399993903|30|15|2012-01-13 17:20:02.346000|2012-01-13 17:20:03.307000|E072AE4B|587244|316|13|GSM|1|SUCC|0|1|255|2|2|0|213|2|0|6|0|0|0|0|0|10|16473840051|30|302610|235|250|0|7|0|0|0|0|0|10|54320058002|906|722310|2|0||0|BELL MOBILITY CELLULAR, INC|BELL MOBILITY CELLULAR, INC|Bell Mobility

Parsing a CSV file using gawk

大城市里の小女人 提交于 2019-11-27 02:09:10
How do you parse a CSV file using gawk? Simply setting FS="," is not enough, as a quoted field with a comma inside will be treated as multiple fields. Example using FS="," which does not work: file contents: one,two,"three, four",five "six, seven",eight,"nine" gawk script: BEGIN { FS="," } { for (i=1; i<=NF; i++) printf "field #%d: %s\n", i, $(i) printf "---------------------------\n" } bad output: field #1: one field #2: two field #3: "three field #4: four" field #5: five --------------------------- field #1: "six field #2: seven" field #3: eight field #4: "nine" ---------------------------

Printing long integers in awk

怎甘沉沦 提交于 2019-11-26 09:12:52
问题 I have a pipe delimited feed file which has several fields. Since I only need a few, I thought of using awk to capture them for my testing purposes. However, I noticed that printf changes the value if I use \"%d\" . It works fine if I use \"%s\" . Feed File Sample: [jaypal:~/Temp] cat temp 302610004125074|19769904399993903|30|15|2012-01-13 17:20:02.346000|2012-01-13 17:20:03.307000|E072AE4B|587244|316|13|GSM|1|SUCC|0|1|255|2|2|0|213|2|0|6|0|0|0|0|0|10|16473840051|30|302610|235|250|0|7|0|0|0|0

How to use multiple arguments for awk with a shebang (i.e. #!)?

家住魔仙堡 提交于 2019-11-25 23:54:00
问题 I\'d like to execute an gawk script with --re-interval using a shebang. The \"naive\" approach of #!/usr/bin/gawk --re-interval -f ... awk script goes here does not work, since gawk is called with the first argument \"--re-interval -f\" (not splitted around the whitespace), which it does not understand. Is there a workaround for that? Of course you can either not call gawk directly but wrap it into a shell script that splits the first argument, or make a shell script that then calls gawk and