1

I have output in bash that I would like to format. Right now my output looks like this:

1scom.net 1
1stservicemortgage.com 1
263.net 1
263.sina.com 1
2sahm.org 1
abac.com 1
abbotsleigh.nsw.edu.au 1
abc.mre.gov.br 1
ableland.freeserve.co.uk 1
academicplanet.com 1
access-k12.org 1
acconnect.com 1
acconnect.com 1
accountingbureau.co.uk 1
acm.org 1
acsalaska.net 1
adam.com.au 1
ada.state.oh.us 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
adelphia.net 1
aecom.yu.edu 1
aecon.com 1
aetna.com 1
agedwards.com 1
ahml.info 1

The problem with this is none of the numbers on the right line up. I would like them to look like this:

1scom.net                 1
1stservicemortgage.com    1
263.net                   1
263.sina.com              1
2sahm.org                 1

Would there be anyway to make them look like this without knowing exactly how long the longest domain is? Any help would be greatly appreciated!

The code that outputted this is:

grep -E -o -r "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" $ARCHIVE | sed 's/.*@//' | uniq -ci | sort | sed 's/^ *//g' | awk ' { t = $1; $1 = $2; $2 = t; print; } ' > temp2
Albert
  • 113
  • 2
  • 12
  • 2
    Pipe to `column -t`. – Etan Reisner Apr 21 '15 at 04:00
  • 1
    Yup. Also there's no reason for the `sed` in there to remove leading spaces. The `awk` command does that for you by default (when you play with fields). – Etan Reisner Apr 21 '15 at 04:04
  • You can also reduce the noise in the awk component. `... | uniq -ci | awk '{print $2,$1}' | column -t` In fact, if you want to provide an example of your source data, I'm sure we can improve the whole command line. What's inside `$ARCHIVE`? Oh and also, there are [many TLDs](http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains) which will not fit inside `\.[A-Za-z]{2,6}`. – ghoti Apr 21 '15 at 04:09
  • There are many email text files inside of ARCHIVE. How would I capture all domains in a regex @ghoti? – Albert Apr 21 '15 at 04:14
  • 1
    Waitasec. Is this the same question as [this other one](http://stackoverflow.com/q/29762023/1072112), and [this other one as well](http://stackoverflow.com/q/29762398)? Please don't abandon your old questions. Either clarify them to improve them so they can be answered, or delete them. If you have better ways of expressing your question, put them IN YOUR QUESTION, don't just ask a new one. – ghoti Apr 21 '15 at 04:15
  • No, this is not. I fixed that issue but am trying to solve a formatting issue so I thought it would be best fit under a new question. – Albert Apr 21 '15 at 04:17
  • Alternately, you can also use `printf`: `printf "%-30s %d\n" $(< test.list)` or `your_command | xargs printf "%-30s %d\n"`. However, `column -t` is better approach. – anishsane Apr 21 '15 at 04:42

1 Answers1

1

ALIGNMENT:

Just use cat with column command and thats it:

cat /path/to/your/file | column -t

For more details on column command refer http://manpages.ubuntu.com/manpages/natty/man1/column.1.html

EDITED:

View file in terminal:

column -t < /path/to/your/file

(as noted by anishsane)

Export to a file:

column -t < /path/to/your/file > /output/file

Rajesh N
  • 2,554
  • 1
  • 13
  • 17