Parse Log File – Unix


You have a log file on UNIX operating system. Each line in the log file contains an IP address in the 10th column where columns (or fields) are separated by a space. How can you print the unique IP addresses in the log file using UNIX shell


This is the same problem as in the previous post but we need to use UNIX shell instead of Perl scripting. Use the cat command to print the content of the file then pipe the output of the cat command to the cut command specifying space as the delimiter (using -d option) and then extract the 10th field (using -f command option) then pipe the output to the sort command and finally pipe the output to the unique command. It should look similar to the following

cat mylog.txt | cut -d ' ' -f10 | sort | unique

Leave a Reply