But in any event, assigning the awk variable rand from the bash magical $RANDOM does not make rand magical. Of course, that's more pragmatic than didactic it's not as educational as writing your own shuffler.Ģ) That applies equally to the awk solution (i.e., why use awk when you can use shuf?). You could improve the computation, or you could generate a random number with more random bits by using /dev/urandom, but personally I'd just use the shuf utility, which does what you want (randomly shuffles an input). After that, limit will be the wrong value. However, it will only avoid bias the first time through the loop, when i 1 = size, since limit was computed based on size. Is attempting to avoid bias in the random number generated by $RANDOM, since that number only has 16 bits and thus the bias might be noticeable. Normally, the seed is set from something which changes frequently, like the time - although that's not ideal - or a value extracted from /dev/random.ġ) I understand that your loop while (((rand=$RANDOM) >= limit)) do : done I don't think srand does what you think it does srand sets the "seed" for awk's random number generator, which avoids generating the same random number sequence every time you invoke awk. To get a random integer in the range [0, i 1), use int(rand()*(i 1)). See gawk manual: Arrays for complete details and gotchas.Awk has the rand function, which generates a random number between 0.0 and 1.0 (actually, strictly less than 1.0). There will be plenty of array examples in later chapters in relevant context. The keys can be numbers or strings, but numbers get converted to strings internally. awk '/regexp/' is a shortcut for awk '$0 ~ /regexp/'Īrrays in awk are associative, meaning they are key-value pairs.By default, when the condition evaluates to true, the contents of $0 is printed. The correct term would be input record, but that's a discussion for a later chapter.Īlso, in the above examples, only the filtering condition was given and nothing about what should be done. When the string isn't specified, the test is performed against a special variable $0, which has the contents of the input line. The full syntax is string ~ /regexp/ to check if the given string matches the regexp and string !~ /regexp/ to check if doesn't match. Regular expressions (regexp) will be covered in detail in the next chapter, only simple string value is used here without any special characters. In the above examples, a regular expression (defined by the pattern between a pair of forward slashes) has been used to filter the input. As awk is primarily used from the command line, many shortcuts are available to reduce the amount of typing needed. You can then use awk's programming instructions to process those lines. Similar to grep and sed, by default awk automatically loops over input content line by line. $ printf 'gate\napple\nwhat\nkite\n' | awk '!/e/' $ # same as: grep -v 'e' and sed -n '/e/!p' $ printf 'gate\napple\nwhat\nkite\n' | awk '/at/' $ # same as: grep 'at' and sed -n '/at/p' And similar to many command line utilities, awk can accept input from both stdin and files. FilteringĪwk provides filtering capabilities like those supported by grep and sed plus some nifty features of its own. These features will be covered in depth in later chapters, but don't go skipping this chapter. This chapter will give an overview of awk syntax and some examples to show what kind of problems you could solve using awk.
0 Comments
Leave a Reply. |