I am trying to replace this:
The numbers at the start of the line will always be 3 digits, 0 padded, there may however be empty lines or regular sentences prior, as shown in my full example that has echo.
227| Mathematics | 9 | search,seo |
038| Top Games | 2391 | search,seo,cookies |
136| Top Programming Languages | 219 | cookies |
with
227.001| Mathematics | 9 | search,seo |
038.002| Top Games | 2391 | search,seo,cookies |
136.003| Top Programming Languages | 219 | cookies |
Here is what I have so far (this works but the number is not 0 padded):
echo -e "Last Updated: Mar 15 2023
- Score: cumulative of benchmarking tools.
- KiB: total size of the page.
- Stats: metrics related to the page.
| Score | KiB | Stats |
| --- | --- | --- |
227| Mathematics | 9 | search,seo |
038| Top Games | 2391 | search,seo,cookies |
136| Top Programming Languages | 219 | cookies |" | awk '/^[0-9]{3}/{$0 = substr($0,1,3) "." ++i substr($0,4)} 1'
I think I need to add something like this ‘printf("%03dn", ++i)’ but when I tried replacing ++i with this, it did not work.
7
Answers
One solution (you have plenty of choices ^^):
Output:
With
sub
replacement (by regex pattern):Assuming 3 digits at the beginning and one line per entry
Assumptions:
One
awk
idea:This generates:
If the 1st column of the table could contain spaces …
Modifying the
awk
script and supplying the input via stdin (to mimic OP’secho | awk
):This generates:
Using any awk:
Since you are mentioning potentially processing the markdown in the table, ruby (or perl) is going to allow the table to be capture as a fully processable, sortable data element.
Here is an example that produces your desired output:
Or use a
split
with a look ahead instead of a multi-line regex:Either prints:
Here is the same but sorted by the existing first column:
Prints:
Or by the third column:
Prints:
etc…
This version it’s entirely agnostic to what’s before the first
|
, and simply tags on the padded row number after a period (.
), so it could be numbers, padded numbers, unicode text, or even emojis.The one above aligns the row numbers to the pipe (
|
) itself, so if there are spaces, gaps will be created. To align to the data values instead while preserving any leading spaces/tabs, try :