-1

My problem is to extract text from lines in /etc/passwd matching a specific pattern

Only text before the ":*" needs to be selected each line

I want the output seperated by commas.

Here's an example:

dog:*dsfgfdh

cat:*bsdfsdf

To:

dog, cat

I have tried something like this which obviously doesnt work

cat /etc/passwd | grep 'a patern'

Anyone with more understanding of grep wanting to help?

1 Answers1

0

Quick & Dirty:

awk -F':' -v ORS=", " '$0=$1' /etc/passwd

It is "dirty" because the result leaves a comma , at the end. Also there is no newline at the end.

A better version:

awk -F':' '{printf "%s%s", NR==1?"":", ", $1}END{print ""}' /etc/passwd
Kent
  • 189,393
  • 32
  • 233
  • 301
  • That is almost exactly what I want, it only still has the comments in the output: ##, # User Database, – Gale Heusen Feb 05 '19 at 12:28
  • @GaleHeusen then the answer gave you a good start, just filter out those comment lines you will get what you want. It is an easy step for you I think. – Kent Feb 05 '19 at 12:40