NewFiveFour | Blog | Portfolio


Unix: Basics of Gawk

The basic syntax of this command is usually:

    gawk '<gawk command>' filename

Within the gawk command, the format is:

    BEGIN { <operations> } /<pattern match>/ { <operations> } /<another pattern>/ { <another op> } END { <operations> }

The BEGIN and END parts are optional. As is the pattern match.

Within the main operations block, you can use the NF variable to find many fields are on this line.

This gawk program will only print non-blank lines, since blank lines have a NF of 0:

    gawk '{ if(NF!=0) print $0 }' filename

The ‘print $0’ will print the whole line. The $1 will print the first field and so on. Fields are separated by spaces and/or tabs.

Pattern matching will allow you to only print lines with, in this case, the word ‘hello’ in them:

    gawk '/hello/ { print $0 }' filename

If you place a ‘~’ before the pattern match, it will be inverted.

There are many other things gawk can do, including variables, addition of fields and many more.

unix awk

NodeJS: Write javascript to JSON file

If you want to write your javascript object to a json file, it’s easy enough:

fs.writeFileSync('thefilename.json', JSON.stringify(your_object))

This will synchronously write your file, too, unlike the other variant which calls a callback when it’s finished.

nodejs json

Simple SQLite3 and NodeJS interactions

Firstly install sqlite3, npm -g install sqlite3 then:

const sqlite3 = require('sqlite3');
var yourdb = new sqlite3.Database('yourdb.sqlite3');
yourdb.all("select * from yourtable", [], function(e, row) { console.log(row) })
nodejs sqlite

NodeJS: Increase allocated memory

You may find NodeJS crashes due to lack of memory. You can increase it as follows (to 6gb):

node --max-old-space-size=6144

nodejs

Unix: Change the keyboard layout using a key switch

If you want to type using another keyboard quickly you can use setxkbmap to change your X windows keyboard using a keymap.

Let’s say you want to switch from a us keyboard to a french keyboard. Use setxkbmap us,fr -option grp:ralt_shift_toggle

Then press right alt and shift together to switch between the two. I hold shift then tap alt gr

unix

Page 2 of 80
Previous Next