Using awk to count the number of occurrences of a word in a column
Use an array awk ‘{count[$3]++} END {for (word in count) print word, count[word]}’ file If you want “block” specifically: END {print count[“BLOCK”]}
Use an array awk ‘{count[$3]++} END {for (word in count) print word, count[word]}’ file If you want “block” specifically: END {print count[“BLOCK”]}
Instead of asort, use asorti(source, destination) which sorts the indices into a new array and you won’t have to copy the array. Then you can use the destination array as pointers into the source array. For your example, you would use it like this: n=asorti(chr_count, sorted) for (i=1; i<=n; i++) { print sorted[i] ” : … Read more
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.org/software/gawk/manual/gawk.html#Changing-Fields) Update: (suggested by @EdMorton and @steve) This … Read more
Awk guesses the type. String “10” is less than string “4” because character “1” comes before “4”. Force a type conversion, using addition of zero: min=`awk ‘BEGIN{a=1000}{if ($1<0+a) a=$1} END{print a}’ mydata.dat` max=`awk ‘BEGIN{a= 0}{if ($1>0+a) a=$1} END{print a}’ mydata.dat`
You can either write a sed script file and use: sed -f sed.script file1 … Or you can use (multiple) -e ‘command’ options: sed -e ‘/SysAdmin/i\ Linux Scripting’ -e ‘1,$s/A/a/’ file1 … If you want to append something after a line, then: sed -e ‘234a\ Text to insert after line 234’ file1 …
Sure it can: pax$ echo ‘hello goodbye’ | gawk ‘{if ($0 == “hello”) {print “HELLO”}}’ HELLO You can also do inequality (ordered) testing as well: pax> printf ‘aaa\naab\naac\naad\n’ | gawk ‘{if ($1 < “aac”){print}}’ aaa aab
You forgot braces around the if block, and a semicolon between the statements in the block. awk ‘{if($3 != 0) {a = ($3/$4); print $0, a;} else if($3==0) print $0, “-” }’ file > out
Try a character class instead echo “$STRING” | egrep ‘[*]’
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
Awk is the tool that the guys who invented grep, shell, etc. invented to do general text manipulation jobs like this so not sure why you’d want to try to avoid it. In case brevity is what you’re looking for, here’s the GNU awk one-liner to do just what you asked for: awk ‘NR==FNR{a[$0];next} {for(s … Read more