Following is the longest Linux command using pipe which I came across today while working with some API gateway,
cat access.log | grep "/gateway/allapi" | grep "\" 500" | rev | cut -d' ' -f4 | rev | sort |uniq
This is a standard access log filtering to find out client identifier involved in API failure. Here is the details about what this command will do step by step,
- The cat command will read the access log and redirect its content to standard output.
- The grep command will look for line having string as “/gateway/allapi” in each line. The next grep command further filter those lines having only string ‘” 500’. With this, we will get all failed calls from access log.
- The rev command will reverse the line (Right to left) which is filtered by the grep command
- The cut command used to take the 4th field in each reversed line which is separated with space.
- Once we have got the client identifier, the command rev will reverse it again to get the actual number.
- The results are further sorted using sort command. The Linux sort command is using Merge Sort algorithm as per this.
- The sorted list of client identifier further filtered using uniq command to get list of unique client identifier
In my opinion, the above Linux command is very easy and faster.