[ALUG] Manipulating files from bash

Mark Rogers mark at more-solutions.co.uk
Mon Sep 25 11:19:28 BST 2017

On 25 September 2017 at 10:27, Mark Rogers <mark at more-solutions.co.uk> wrote:
> Having read the docs I'll go for something much closer to the output
> generated by "sfdisk -d", massively simplifying the process for sed.

Here's what I end up with:

sudo sfdisk -d /dev/sdd | \
  sed -E '/^(label(-id)?|device):/d     # Remove unwanted headers
      ; /^$/d                           # Remove blank lines
      ; s#^/dev/[a-z0-9]+ *: *##        # Remove device prefixes
      ; s# *= *#=#g                     # Remove whitespace around = signs
      ; s#start=[0-9]+,##               # Remove start values
      ; $ s#size=[0-9]+,##              # Remove last size value
(Apologies for any wrapping!)

The result is:
  unit: sectors
  size=85622, type=c
  size=5521408, type=83

The "$" trick to apply an expression to the last line of the input
only was a new one on me, as was EOL commenting within the script.

Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450
Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER

More information about the main mailing list