How to align list to specific character

command linetext processing

Is there a command or set of commands that I can use to horizontally align lines of text to an arbitrary character? For example, with a list of email addresses the output would produce a text file with all the '@' characters lined up vertically.

To be successful I believe that a variable number of empty spaces must be added to the beginning of most lines. I do not want separate columns as they take more effort to read (for example, column -t -s "@" < file.txt).

Before:

123@example.com
456789@example.net
01234@something-else.com

After:

   123@example.com
456789@example.net
 01234@something-else.com

Put differently: can I specify a character to be an anchor point, around which the surrounding text is horizontally centered? My use-case for this is email addresses, to make them easier to scan visually.

Best Answer

NO Awk. Only sed and column:

column -ts@ file.txt | sed -E 's/([^ ]+)([ ]+) (.+)/\2\1@\3/'

Output:

   123@example.com
456789@example.net
 01234@something-else.com

Now, that I think about, this is almost the same as Sundeep' solution, it just looks shorter / has less calls to sed, and it also assumes that @ happens only once in each line.

Related Question