I have a textfile that has the following format and I want to add a vertical line after those lines, followed by increasing numbers:
c4-1 d e c
c d e c
e-2 f g2
e4 f g2
g8-4\( a-5 g f\) e4 c
g'8\( a g f\) e4 c
c-1 r c2
c4 r c2
I achieve the line and the numbering with the following while-loop
:
#!/bin/bash
while read -r line; do
if [ -z "$line" ]; then
echo
continue
fi
n=$((++n)) \
&& grep -vE "^$|^%" <<< "$line" \
| sed 's/$/\ \|\ \%'$(("$n"))'/'
done < file
and get an output like:
c4-1 d e c | %1
c d e c | %2
e-2 f g2 | %3
e4 f g2 | %4
g8-4\( a-5 g f\) e4 c | %5
g'8\( a g f\) e4 c | %6
c-1 r c2 | %7
c4 r c2 | %8
now I want the addition to be vertically aligned and get an output like this:
c4-1 d e c | %1
c d e c | %2
e-2 f g2 | %3
e4 f g2 | %4
g8-4\( a-5 g f\) e4 c | %5
g'8\( a g f\) e4 c | %6
c-1 r c2 | %7
c4 r c2 | %8
this would mean I need to somehow get the line length of the longest line (here: 21 characters) and the line length of each line and add the difference with spaces, how could I achieve this?
wc -L
to get length of longest line in a file.. and then use printf formatting – Sundeep Nov 20 '19 at 04:01wc -L
. Though this attempt is pretty slow. Freddies answer usingcolumn
is pretty awesome, check it out :-)) – nath Nov 20 '19 at 05:03