Home > Bash, Scripting > Format(print) the content of a file using awk

Format(print) the content of a file using awk

awk is a programming(scripting) language mainly used for text manipulations such as formatting, selective printing etc. Here, in the following example I format ‘/etc/passwd’ file output to more readable way.

[suku@eldorado~]#awk 'BEGIN{FS=":";
format="%-3s%-15s%-15s%-15s%-s\n";
printf(format,"SI","USER","UID","GID","LOGIN_SHELL")}
{printf(format,NR,$1,$3,$4,$NF)}''END{print "Total"" " NR}' /etc/passwd

Categories: Bash, Scripting
  1. No comments yet.
You must be logged in to post a comment.