Commands tagged awk conditional (3)

  • The sample output shows each record/row with the last field zero-padded to 26 digits. For testing, I used (L)ine and field/column numbers.... Line 4, field2 = L42, etc up to the last field where I just used line numbers X 4. I had some whitespace-delimited files with variable-length records/rows (having 4 - 5 fields/columns) which required reformatting by zero-padding the last field to 26 digits. This requires setting NF (Not $NF) as an awk variable, with a simple conditional that assumes that any line where (N)umber of (F)ields does NOT equal 4 has a NF of 5. If needed, more conditional checks can be added, and the "NF" changed to any field ($1, $5, etc). Show Sample Output

    awk '{var = sprintf(NF); if (var == 4) printf "%s %s %s %026d\n" , $1,$2,$3,$4; else printf "%s %s %s %s %026d\n" , $1,$2,$3,$4,$5}' >>
    genatomics · 2014-12-20 02:53:35 0
  • Removes directories which are less than 1028KB total. This works for systems where blank directories are 4KB. If a directory contains 1 MB (1024KB) or less, it will remove the directory using a path relative to the directory where the command was initially executed (safer than some other options I found). Adjust the 1028 value for your needs. It would be helpful to test the results before proceeding with the removal. Simply run all but the last two commands to see a list of what will be removed: du | awk '{if($1<1028)print;}' | cut -d $'\t' -f 2- If you're unsure what size a blank folder is, test it like this: mkdir test; du test; rmdir test

    du | awk '{if($1<1028)print;}' | cut -d $'\t' -f 2- | tr "\n" "\0" | xargs -0 rm -rf
    i814u2 · 2015-06-25 16:00:48 0

  • 0
    awk '{if ($3 =="LAN" && $5 == "0.00" ) print $1,  $2, "LAN",  "288",  "100.00"; else print $1 ,$2, $3, $4, $5 }' sla-avail-2013-Feb > sla-avail-2013-Feb_final
    shantanuo · 2020-02-15 10:37:44 3

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

Search for a string inside all files in the current directory
ack seeks for , list all found files and display everything in nice colors. Look for it in

Convert text to picture
generates a picture file with the text. Some other samples in:

delay execution of a command that needs lots of memory and CPU time until the resources are available
[ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] returns true if less than 2000 MB of RAM are available, so adjust this number to your needs. [ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ] returns true if the current machine load is at least equal to the number of CPUs. If either of the tests returns true we wait 10 seconds and check again. If both tests return false, i.e. 2GB are available and machine load falls below number of CPUs, we start our command and save it's output in a text file. The ( ( ... ) & ) construct lets the command run in background even if we log out. See .

Execute a command on multiple hosts in parallel
Ssh to host1, host2, and host3, executing on each host and saving the output in {host}.log. I don't have the 'parallel' command installed, otherwise it sounds interesting and less cryptic.

Apply, in parallel, a bc expression to CSV
Define a function that applies bc, the *nix calculator, with the specified expression to all rows of the input CSV. The first column is mapped to {1}, second one to {2}, and so forth. See sample output for an example. This function uses all available cores thanks to GNU Parallel. Requires GNU Parallel

Mac OS X: Change Color of the ls Command
I use terminal with black background on the Mac. Unfortunately, the default ls color for the directory is blue, which is very hard to see. By including the line above in my ~/.bash_profile file, I changed the directory's color to cyan, which is easer to see. For more information on the syntax of the LSCOLORS shell variable: $ man ls I tested this command on Mac OS X Leopard

Watch movies in your terminal
requires mplayer

BASH one-liner to get the current week number
Not as cool as the python example, but it still works.

Install pip with Proxy
Installs pip packages defining a proxy

reverse order of file
awk reverse order of lines (emulates "tac")

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: