I want to list all the filesystems in a single comma-delimited list, in dictionary order.
I’m using this command:cat /proc/filesystems | sed 's/nodev//' | sed 's/,$//'
My output looks like this:
sysfs rootfs ramfs bdev proc cgroup cpuset tmpfs devtmpfs debugfs securityfs sockfs pipefs anon_inodefs devpts ext3 ext2 ext4 hugetlbfs vfat ecryptfs fuseblk fuse fusectl pstore mqueue binfmt_misc vboxsf
How can I change this to a single line output with commas separating each filesystem?
I figured part of it out by using
xargs
:cat /proc/filesystems | sed 's/nodev//' | xargs | sed -e 's/ /,/g'
Now I want to make the output formatted in dictionary order.
Answer
Using awk
awk '{printf "%s%s",(NR>1?",":""),$NF;} END{print""}' /proc/filesystems
Using sed
sed -r 's/^nodev//; s/^[[:blank:]]*//; H;1h;$!d;x; s/\n/,/g' /proc/filesystems
How it works
-
s/^nodev//
This eliminates
nodev
from the beginning of any line -
s/^[[:blank:]]*//
This eliminates the leading whitespace from each line.
-
H
This appends the current line to the hold space.
-
1h
If we are on the first line, overwrite the hold space with this line.
-
$!d
If we are not on the last line, delete the pattern space and start over on the next line.
-
x; s/\n/,/g
If we get here, we are one the last line. In that case, move the hold space into the pattern space and replace all newlines with commas.
To understand this, it helps to recognize H;1h;$!d;x
as an awk idiom which means read in the whole file.
Extra: sorting
To sort the names:
awk '{print $NF;}' /proc/filesystems | sort | awk '{printf "%s%s",(NR>1?",":""),$NF;} END{print""}'
Attribution
Source : Link , Question Author : TommyLan , Answer Author : John1024