1

I want to print the output of usernames from /etc/passwd into multiple columns. The output from my server is

[root@master vdabas]# cat -n /etc/passwd | cut -f1 -d":"

 1  root
 2  bin
 3  daemon
 4  adm
 5  lp
 6  sync
 7  shutdown
 8  halt
 9  mail
10  operator
11  games
12  ftp
13  nobody
14  systemd-network
15  dbus
16  polkitd
17  sshd
18  postfix
19  vdabas
20  vani
21  delhi

I want the output to be as below:

 1  root            8  halt             15  dbus
 2  bin             9  mail             16  polkitd
 3  daemon          10 operator         17  sshd
 4  adm             11 games            18  postfix
 5  lp              12 ftp              19  vdabas
 6  sync            13 nobody           20  vani
 7  shutdown        14 systemd-network  21  delhi

2 Answers 2

6

You can use the pr command to format in 3 columns (-3), omitting pagination (-T):

cat -n /etc/passwd | cut -f1 -d":" | pr -T -3

or perhaps

awk -F: '{print NR,$1}' /etc/passwd | pr -T -3
0
0

Under macOS (and maybe other BSD flavors) use

 pr -t -3

(lower case "t")

In your peculiar case, if your input were in a file list.txt

pr -3 -l7 list.txt

would do the job. As said, for my BSDish macOS "pr" command. (note that the option -l automatically supresses the headers if the sum of the -l argument is not greater than the sum of both, the header and trailer depths)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .