CSV columnar reformat with SED (or anything other coreutil)

sedtext processing

Given the following made-up segment from an output file from hashdeep:

7241,11111111111111111111111111111111,\01-data\file1
1237241,22222222222222222222222222222222,\01-data\file2
41,33333333333333333333333333333333,\01-data\file3

How would I get about it to format it like:

   7241,11111111111111111111111111111111,\01-data\file1
1237241,22222222222222222222222222222222,\01-data\file2
     41,33333333333333333333333333333333,\01-data\file3

I'd like to use sed (as that's what I'm beginning to get to grips with), but is there a way to tell sed to only change characters if they occur in a specific column or specific columns?

Of course if there is another way to do it, I'd be just as happy to hear about that.

The reason for this is that I want to sort the output on the filenames, so that I can compare two output files, without having to use the -j0 (single-thread) option on hashdeep.

Best Answer

With awk:

awk -v l="$(wc -L <file)" '{printf "%"l"s\n", $0}' file
  • -v assigns an external value to an awk variable l.
    • wc -L <file find the length of the longest line in the file.
  • printf "%"l"s\n", $0 prints each line space padded with by l spaces. For 10 spaces, it would for example look like: printf "%10s\n", $0.

The output:

   7241,11111111111111111111111111111111,\01-data\file1
1237241,22222222222222222222222222222222,\01-data\file2
     41,33333333333333333333333333333333,\01-data\file3
Related Question