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
質問
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.
解決 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
他のヒント
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
,
as field separator (defined by -F,
). -a
option splits our entire line on ,
in an array defined by @F
. if(/cards:/)
. @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.