Jeff
Jeff

Reputation: 410

How to append space before match pattern in bash

How to append number of space before match pattern or after line by line in bash with sed command?

file.txt

str_len: equ $ - str    ; calcs length of string (bytes) by
          ; subtracting this address ($ symbol)

output.txt

str_len: equ $ - str    ; calcs length of string (bytes) by
                        ; subtracting this address ($ symbol)

Upvotes: 2

Views: 530

Answers (2)

Rubens
Rubens

Reputation: 14778

There is just a tool from unix-magic-set-of-wondrous-things that does exactly what you want:

$ column -t -s ';' -o ';' <input>
str_len: equ $ - str    ; calcs length of string (bytes) by
                        ; subtracting this address ($ symbol)

Other than that, sed is Turing complete, and so is Turing's machine. But that does not mean one has time to implement non-trivial solutions on such architectures :D

Edit: The above command was run using column from util-linux 2.25.2, with flags:

-o, --output-separator string
   Specify the columns delimiter for table output (default is two spaces).

-s, --separator separators
   Specify the possible input item delimiters (default is whitespace).

-t, --table
   Determine  the  number  of  columns  the input contains and create a table.
   Columns are delimited with whitespace, by default, or with  the  characters
   supplied  using  the --output-separator option.  Table output is useful for
   pretty-printing.

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74695

Here's one way you could do it using awk:

$ awk -F' *;' -vOFS=\; '{print $1 substr("                        ",1,24-length($1)),$2}' file.txt
str_len: equ $ - str    ; calcs length of string (bytes) by
                        ; subtracting this address ($ symbol)

Set the input field separator to any number of spaces followed by a semicolon. Set the output field separator to a semicolon. Print the first column, followed by as many spaces as are needed to pad to 24 characters, followed by the second column.

Upvotes: 1

Related Questions