Domanda

For example, AAA000 and BBB111 are the users under CARDS group....I'm trying to get an output in the following format.

AAA000 AAA000@pattern.com \
BBB111 BBB111@pattern.com \

I tried grep -i CARDS: /etc/group | sed 's/$/,/' | sed 's/,/@capitalone.com\n/g' which gives me

cards:x:36082:CARDS@pattern.com
AAA000@pattern.com
BBB111@pattern.com

Any idea what else should I include in the command to get in to the format I need...?

Thanks, Sam.

È stato utile?

Soluzione 2

You can do this in awk:

awk -F, '/cards:/{for(i=2;i<=NF;i++){printf"%s %s@pattern.com\n", $i, $i}}' file

Input:

cat file
cards:x:36082:CARDS,AAA000,BBB111

Output:

AAA000 AAA000@pattern.com
BBB111 BBB111@pattern.com

Altri suggerimenti

Assuming your input is like this:

$ cat file
cards:x:36082:CARDS,AAA000,BBB111

You can do:

$ perl -F, -lane 'if(/cards:/){print "$_ $_\@pattern.com" for (@F[1..$#F])}' file
AAA000 AAA000@pattern.com
BBB111 BBB111@pattern.com
  • We use , as field separator (defined by -F,).
  • Using -a option splits our entire line on , in an array defined by @F.
  • We test if the current line is the line you want to parse by doing if(/cards:/).
  • If it is we print every element of the array starting from second index (@F[1..$#F]) till the very last. This is because our first index would have the value cards:x:36082:CARDS which you don't need.

Found command using grep and awk...........

grep -i CARDS: /etc/group|awk -F, '/cards:/{for(i=2;i<=NF;i++) print $i,$i"@pattern.com \\"}'

Thanks for the help guys.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top