Home > Enterprise >  shell: build a comma separated list ended with a period
shell: build a comma separated list ended with a period

Time:03-30

In a shell exercice I'm asked to build from a newline separated list a ", " separated list with a final period like that:

abc
def
hij

would becomes:

abc, def, hij.

They ask me to "write a command line". I tried using tr and paste but I can't get the whitespace and the final period is problematic.

I'm a bit lost, I can't think of any command that could help me for that. Could someone give me some clue?

NB: what exactly I'm asked for is the following:

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 ".".

I currently have that line (doesn't do the comma/period thing):

cat /etc/passwd | sed '/^#/d' | awk -F ':' 'NR % 2 == 0 {print $1}' | rev | sort -r | sed -n "$FT_LINE1,$FT_LINE2p"

CodePudding user response:

echo -e "abc\ndef\nhij" | sed ':a;N;$!ba;s/\n/, /g'

explained here https://stackoverflow.com/a/1252191/2235381

  • Related