Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Thursday, 22 October 2026

Using grep, awk, sed, sort, and uniq in combination

So I was trying to retrieve some information out of a log, and just for funsies, I thought I'd write it down here, so I remember what I did.

In short:

So I was trying to get a list of UUIDs of messages that failed to be parsed.

The serverlog I had contained for example the following:

Error during processing of message 8748 (225abd00-a806-455e-b3ef-a991c163fd41).
grep "Error during processing of" server.log | awk '{print $7}' | sed 's/(// ; s/)// ; s/\.//' | sort | uniq

Short explanation:

  • grep - only pick the lines containing that string
  • awk - print the 7th column with a standard delimiter of white space (" ")
  • sed - replace opening brackets ("("), closing brackets (")") and periods (".") from the output
  • sort - sort all alphabetically
  • uniq - make sure there are no duplicates

The result should be a list of UUIDs.

Footnote 1: the sort is essential, as uniq only works on adjacent lines

Footnote 2: yes, I know I can grep inside of an awk commando, but I am more familiar with grep and this is more fun.

Footnote 3: I've tested this on my MacBook. Linux might in some cases have different syntax.

Thursday, 6 July 2017

Lambdas, New IO, and parsing textfiles in a hurry.

Okay, so I needed to do some parsing of a file containing URLs (which I "wget"-ted) and moving the retrieved files to proper locations.

I decided to write a quick Java program to do this instead of messing around with scripting languages or a Linux bash shell.

It worked very well, and I am rather pleased with the result and Java 8.

It contains the following "new/newer/not-very-old" stuff:
  • a lambda
  • a stream (of Strings)
  • a method reference (used as a lambda)
  • the java.nio.file package (New IO)
One small note: lambdas implement an interface. In this case the forEach requires a lambda that implements the Consumer interface. The Consumer interface does not specify an IOException. Therefore, I am required to catch it here and rethrow it unchecked.

References

[1] Java SE 8 - Official Javadoc
https://docs.oracle.com/javase/8/docs/api/