0

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"
kuroneko
  • 425
  • 2
  • 4
  • 10

1 Answers1

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

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

lojza
  • 1,823
  • 2
  • 13
  • 23
  • I haven't read the linked answer yet, put copy pasting your example doesn't work. I'm on macos, does that change the newline character, maybe? – kuroneko Mar 29 '22 at 10:20
  • 1
    separating commands like this works: sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/, /g' – kuroneko Mar 29 '22 at 10:22