Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
how to display users under a group in a line for each user in Linux bash

Currently when I run a grep command on a visitors group that I had created

grep visitors:x:1011: /etc/group

This is a sample result of the above command, as the actual result is too long.

visitors:x:1011:ellipsiscoterie,magnetcommonest,wizardmeans,beakskeletal,lemonwellmade,ralphpaperclips etc..

How do I display the above result as shown below (without visitors:x:1101 and separating the commas), as I have difficulty doing so.

ellipsiscoterie
magnetcommonest
wizardmeans
beakskeletal
lemonwellmade
ralphpaperclips
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I suggest using sed instead of grep

sed -n -e 's/^visitors\:x\:1011\://p' /etc/group | tr ',' '\n'

UPD: detailed explain of sed part is here. tr examples you can find here

over 4 years ago · Santiago Trujillo Relatório

0

Using awk, you can set the field separated to a colon using -F: and check if the string starts with visitors:x:1011:

As the comma separated values always seems to be at the end, split the last field $NF on a comma and loop the splitted values.

awk -F: '
/^visitors:x:1011:/ {
  count=split($NF, ary, ",");
  for(i=1; i<=count; i++) print ary[i];
}' /etc/group
over 4 years ago · Santiago Trujillo Relatório

0

With your shown samples please try following awk program.

awk -F':' '/^visitors:x:1011/ && NF==4{gsub(/,/,ORS,$4);print $4}' /etc/group

OR in case entry visitors:x:1011 comes only once in your file then try following:

awk -F':' '/^visitors:x:1011/ && NF==4{gsub(/,/,ORS,$4);print $4;exit}' /etc/group

Explanation: Simple explanation would be, making field separator as : for all lines. Then in main program, checking condition if line starts from visitors:x:1011 and number of fields are 4 then globally substitute comma with new line(ORS) in 4th field and print it.



OR if line with visitors:x:1011 comes once and 4th field is NULL(means no entry present which is obvious in some cases) and you want to print message for it then try following:

awk -F':' '/^visitors:x:1011/ && NF==4{if($4==""){print "NO entry found in /etc/group"} else{gsub(/,/,ORS,$4);print $4};exit}' /etc/group

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda