Thursday 7 March 2013

awk


command – awk ‘{print $1}’ 
cat dog horse
              lion tiger bear

The output of the same command is   cat
                                                        lion

myFile contains data separated by a : e.g. cat:dog:horse
awk –F “:” ‘{print $1}’ myFile.
will print as normal again



ls –l | awk '{print $9}'

                                    file1
                                    file2
                                    file3    etc.
will print file names

ls –l | awk '{bytes += $5; print $9 "  " $5 " "$3 " Total " bytes " bytes"}'


implement something once at the start or end of the command. For example:
ls –l |awk '
{bytes += $5;
print $9 "  " $5 " " $3}’
END{print "Total " ttl " bytes"}'

ls –l | awk '
BEGIN{print"Directory Listing"}
{print $9 " "$5}'

ignoring lines
ls –l | awk '
BEGIN{print " Directory Listing"}
{if($1 == "total") next;
print $9}
END{print "Thank you and Goodbye"}'





No comments:

Post a Comment