shell: 构建一个以逗号分隔的列表,以句点结尾
shell: build a comma separated list ended with a period
在 shell 练习中,我被要求从换行符分隔列表构建一个“,”分隔列表,最后一个句点如下:
abc
def
hij
会变成:
abc, def, hij.
他们让我“写一个命令行”。我尝试使用 tr
和 paste
但我无法获得空格,最后一个句点有问题。
我有点迷茫,我想不出任何命令可以帮助我。有人可以给我一些线索吗?
注意:我的确切要求如下:
Write a command line that displays the output of a cat /etc/passwd command,
removing comments, every other line starting from the second line, reversing each lo-
gin, sorted in reverse alphabetical order, and keeping only logins between FT_LINE1
and FT_LINE2 included, and they must separated by ", " (without quotation
marks), and the output must end with a ".".
我目前有那条线(不做 comma/period 事情):
cat /etc/passwd | sed '/^#/d' | awk -F ':' 'NR % 2 == 0 {print }' | rev | sort -r | sed -n "$FT_LINE1,$FT_LINE2p"
echo -e "abc\ndef\nhij" | sed ':a;N;$!ba;s/\n/, /g'
此处解释
在 shell 练习中,我被要求从换行符分隔列表构建一个“,”分隔列表,最后一个句点如下:
abc
def
hij
会变成:
abc, def, hij.
他们让我“写一个命令行”。我尝试使用 tr
和 paste
但我无法获得空格,最后一个句点有问题。
我有点迷茫,我想不出任何命令可以帮助我。有人可以给我一些线索吗?
注意:我的确切要求如下:
Write a command line that displays the output of a cat /etc/passwd command, removing comments, every other line starting from the second line, reversing each lo- gin, sorted in reverse alphabetical order, and keeping only logins between FT_LINE1 and FT_LINE2 included, and they must separated by ", " (without quotation marks), and the output must end with a ".".
我目前有那条线(不做 comma/period 事情):
cat /etc/passwd | sed '/^#/d' | awk -F ':' 'NR % 2 == 0 {print }' | rev | sort -r | sed -n "$FT_LINE1,$FT_LINE2p"
echo -e "abc\ndef\nhij" | sed ':a;N;$!ba;s/\n/, /g'
此处解释