gawk

Can field separator in awk encompass multiple characters?

我的未来我决定 提交于 2019-11-30 11:33:58
Can I use a field separator consisting of multiple characters? Like I want to separate words which contain quotes and commas between them viz. "School","College","City" So here I want to set my FS to be ",". But I am getting funny results when I define my FS like that. Here's a snippet of my code. awk -F\",\" ' { for(i=1;i<=NF;i++) { if($i~"[a-z0-9],[a-z0-9]") print $i } }' OFS=\",\" $* yes, FS could be multi-characters. see the below test with your example: kent$ echo '"School","College","City"'|awk -F'","|^"|"$' '{for(i=1;i<=NF;i++){if($i)print $i}}' School College City What's being talked

Sort associative array with AWK

心不动则不痛 提交于 2019-11-30 06:46:12
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.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_26626841.html ) Thanks in advance Update

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

我怕爱的太早我们不能终老 提交于 2019-11-30 06:44:13
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) ? 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[1;34m" s "\033[0m " } { print red($1), green($2), blue($3) } An alternative to using awk functions is

Print all Fields with AWK separated by OFS

心已入冬 提交于 2019-11-30 06:30:07
问题 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 回答1: This is a variation on the first style: echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {$1=$1; print $0}' Results: 1 :-( 2 :-( 3 :-(

multidimensional arrays in awk

此生再无相见时 提交于 2019-11-30 05:47:46
问题 I tried creating a pseudo-multidimensional array in awk. # Calculate cumulative context score BEGIN { FS=OFS="\t" } { a[$2+FS+$7,$3]+=$6 } END { for (i,j) in a { print i,j,a[i,j] } } Output: awk: ccstscan.awk:9: END { for (i,j) in a awk: ccstscan.awk:9: ^ syntax error this is what is mentioned in GNU awk manual: To test whether a particular index sequence exists in a multidimensional array, use the same operator (in) that is used for single dimensional arrays. Write the whole sequence of

gawk / awk: piping date to getline *sometimes* won't work

杀马特。学长 韩版系。学妹 提交于 2019-11-30 05:12:21
问题 I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the variable supposed to hold the (converted) date remains undefined. This always happens with the exact same rows. Running `date' explicitly (from the Bash shell) on the dates of those weird rows works fine (the dates are properly converted). -- It's

print every nth line into a row using gawk

旧街凉风 提交于 2019-11-29 21:34:58
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;} } To print every second line, starting with the first: awk 'NR%2==1' file.txt To print every tenth line, starting

Can field separator in awk encompass multiple characters?

∥☆過路亽.° 提交于 2019-11-29 17:10:39
问题 Can I use a field separator consisting of multiple characters? Like I want to separate words which contain quotes and commas between them viz. "School","College","City" So here I want to set my FS to be ",". But I am getting funny results when I define my FS like that. Here's a snippet of my code. awk -F\",\" ' { for(i=1;i<=NF;i++) { if($i~"[a-z0-9],[a-z0-9]") print $i } }' OFS=\",\" $* 回答1: yes, FS could be multi-characters. see the below test with your example: kent$ echo '"School","College

How to set the field separator to an empty string?

懵懂的女人 提交于 2019-11-29 15:16:51
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 works: $ awk -F, '{print $2}' <<< "a,b,c" b $ awk -v FS=, '{print $2}' <<< "a,b,c" b But this does not:

gawk floating-point number localization

你离开我真会死。 提交于 2019-11-29 11:35:59
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 parsing number with , as decimal point without specifying POSIX option? The option your are looking for