Using AWK
//print the first column
awk -F":" ' { print $1 } ' /etc/passwd
// print 1st column and last column
awk -F":" ' { print $1, $NF} ' /etc/passwd
//print 1st column of 1st ten records
awk -F":" ' NR==1,NR==10 { print $1 } ' /etc/passwd
//print 1st column length of 1st ten records
awk -F":" ' NR==1,NR==10 { print length($1) } ' /etc/passwd
// using printf to format output
awk -F":" ' NR==1,NR==10 { printf "%-8s %3d\n" , $1,$3 } ' /etc/passwd
// add header and footer for output
awk -F":" '
BEGIN { printf "%-8s %-3s\n" , "User", "UID" }
NR==1,NR==10 { printf "%-8s %3d\n" , $1,$3 }
END { print "============== END ============="} ' /etc/passwd
// add output filed separator between columns
awk -F":" ' { OFS="|";print $1, $NF } ' /etc/passwd
// condition
awk -F":" ' /^root/ { print $1, $NF } ' /etc/passwd
awk -F":" ' { if($1 ~ /root/) { print $1, $NF } }' /etc/passwd
Written on March 27, 2017