get the user input in awk

You can collect user input using the getline function. Make sure to set this in the BEGIN block. Here’s the contents of script.awk: BEGIN { printf “Enter the student’s name: ” getline name < “-” } $2 == name { print } Here’s an example file with ID’s, names, and results: 1 jonathan good 2 … Read more

Using awk to pull specific lines from a file

awk ‘NR == FNR {nums[$1]; next} FNR in nums’ numberfile datafile simply referring to an array subscript creates the entry. Looping over the first file, while NR (record number) is equal to FNR (file record number) using the next statement stores all the line numbers in the array. After that when FNR of the second … Read more

ignorecase in AWK

Add IGNORECASE = 1; to the beginning of your awk command like so: bash-3.2$ echo “Create” | awk ‘/^create/;’ bash-3.2$ echo “Create” | awk ‘IGNORECASE = 1;/^create/;’ Create

print unique lines based on field

You put your test for “seen” in the action part of the script instead of the condition part. Change it to: awk -F, ‘!seen[$1]++’ Input.csv Yes, that’s the whole script: $ cat Input.csv 10,15-10-2014,abc 20,12-10-2014,bcd 10,09-10-2014,def 40,06-10-2014,ghi 10,15-10-2014,abc $ $ awk -F, ‘!seen[$1]++’ Input.csv 10,15-10-2014,abc 20,12-10-2014,bcd 40,06-10-2014,ghi

Random numbers generation with awk in BASH shell

If you don’t provide a seed to srand, it will either use the current date and time or a fixed starting seed (this may vary with the implementation). That means, for the former, if your processes run fast enough, they’ll all use the same seed and generate the same sequence. And, for the latter, it … Read more

How to escape a single quote inside awk

This maybe what you’re looking for: awk ‘BEGIN {FS=” “;} {printf “‘\”%s’\” “, $1}’ That is, with ‘\” you close the opening ‘, then print a literal ‘ by escaping it and finally open the ‘ again.