Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
2  
Probably not applicable for this case, but a general tool for this type of formatting is column. – Wildcard Jan 6 at 7:41
2  
I'm perhaps missing something, but you want to sort on file names as in sort -t, -k3 filename ? – Runium Jan 6 at 7:44
    
I've had a look at sort and it does indeed what is needed, but coming from the z/OS world, it's feels very, very unnatural to sort on fields rather than positions. – Robert Prins Jan 6 at 11:24
1  
This is an XY problem. You don't need to reformat your CSV in order to sort your data at all. – Sobrique Jan 6 at 19:24

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
share|improve this answer
    
Nice one. A slightly more readable version would arguably contain {printf "%*s\n", l, $0}. It also works with gawk --posix, so I guess it is not gawk's invention. – joepd Jan 6 at 18:20
sed -e:, -e's/^[^,]\{0,6\},/ &/;t,'

It just does a little loop until there are at least 7 not-commas at the head of a line. It only does anything at all, though, if there are fewer than 7 not-commas already there which are immediately followed by a comma. And so it doesn't affect blank lines or lines which do not match a comma, or those that do but already start with 7 or more not-commas.

The loop is really cheap, too - the automaton only needs to consider two kinds of characters - those that are or are not commas - and only 7 of those at maximum at any given time.


   7241,11111111111111111111111111111111,\01-data\file1
1237241,22222222222222222222222222222222,\01-data\file2
     41,33333333333333333333333333333333,\01-data\file3
share|improve this answer

Not using sed. I copied your input and pasted into Libreofffice Calc, selected the "separate by comma" option in "Paste Special" and that put each data type into separate columns as shown below.

Very simple solution I think.

7241    11111111111111100000000000000000    \01-data\file1 

1237241 22222222222222200000000000000000    \01-data\file2

41      33333333333333300000000000000000    \01-data\file3
share|improve this answer
    
This is an understandable error, but somewhat horrifying. Manipulating data and text on the command line never ever requires opening up a GUI. I like LibreOffice quite well, but the tool for the job is awk, which is a super-advanced "spreadsheet" for the command line. – Wildcard Jan 6 at 18:22

I don't quite understand the question, but I think you just want to sort by filename? In that case, just use sort:

sort -t , -k 3 my_file.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.