How to alter the string format of the text between the line number and the history entry in the output of history in bash?
history in bash prints, for example:
how to change that to this, for instance:
Also, alignment is done by
history already, by adjusting the number of spaces. How to include this functionality as well? That is, if . is used as the string, how to change the number of dots according to the number of digits in the line number? Also, I'd like to remove the leading spaces.
history | sed 's/^\( *[0-9]*\) */\1../'
When I do
history, there is always two spaces after the line number. The alignment is done by varying the number of spaces before the number.
This is an example of how to replace the leading spaces (which vary in number):
history | sed 's/^ */&\n/; :a; s/ \(.*\n\)/.\1/; ta; s/\n//'
This version works similarly to the second one above, but it moves the dots after the numbers so there are no leading spaces and the variable number of dots cause the history entries to be left aligned.
sed 's/^ */&\n/; :a; s/ \(.*\n\)/.\1/; ta; s/\n//;s/\(\.*\)\([^ ]*\) */\2\1/' 1....aaa bbb ccc 22...ddd eee fff 333..ghi jkl mno
This is how it works:
- add a newline after the initial sequence of spaces (divide and conquer)
:a- label for branching
- replace one space before the newline with a dot
ta- branch to label
:aif a replacement is made, once all the leading spaces are replaced, continue past the branch instruction
- remove the newline
- capture the dots and non-spaces (digits) in two capture groups and exclude the spaces after the numbers
- and swap the two groups (so the dots now come after the numbers)