Hi, i need to extract some information from the /etc/passwd file to be used as a command input in a mail software. My /etc/passwd looks like: k.thomas:x:1918:100:Kimaura Thomas:/home/users/k.thomas:/bin/usersh My main issue here is that the fifth field contains spaces and spanish chars with accent. I currently do not posess the skill to understand how to use cut to extract all the field. So far, cut returns the name up to the space, so in this case it will return Kimaura and not Kimaura Thomas. Can you please point me to internet examples of tools (sed, awk, grep or cut) that will help me accomplish this, or maybe provide the code?
And here is the code (not the best...im still learninng..) #!/bin/bash # Passwd to Zimbra import # This script modifies the displayName field in the Zimbra mailserver # This script will parse /etc/passwd and extracts field 1 and field 5 # field 1 is the username. # field 5 is the name in long format of the username # # domain="oj.gob.pa" # file="zimbranames.file" # x=0 # echo ''>$file # # for linia in `cat /etc/passwd` # do # user=`echo $linia|cut -f1 -d":"` # nombre=`echo $linia|cut -f5 -d":"` # echo "zmprov ma $user@$domain displayName $nombre">>$file # x=$[x+1] # done # echo "$x accounts exported to "$PWD/$file"" # sleep 5
Thanks in advance.
Erick Perez wrote:
Hi, i need to extract some information from the /etc/passwd file to be used as a command input in a mail software. My /etc/passwd looks like: k.thomas:x:1918:100:Kimaura Thomas:/home/users/k.thomas:/bin/usersh My main issue here is that the fifth field contains spaces and spanish chars with accent. I currently do not posess the skill to understand how to use cut to extract all the field. So far, cut returns the name up to the space, so in this case it will return Kimaura and not Kimaura Thomas. Can you please point me to internet examples of tools (sed, awk, grep or cut) that will help me accomplish this, or maybe provide the code?
And here is the code (not the best...im still learninng..) #!/bin/bash # Passwd to Zimbra import # This script modifies the displayName field in the Zimbra mailserver # This script will parse /etc/passwd and extracts field 1 and field 5 # field 1 is the username. # field 5 is the name in long format of the username # # domain="oj.gob.pa" # file="zimbranames.file" # x=0 # echo ''>$file # # for linia in `cat /etc/passwd` # do # user=`echo $linia|cut -f1 -d":"` # nombre=`echo $linia|cut -f5 -d":"` # echo "zmprov ma $user@$domain displayName $nombre">>$file # x=$[x+1] # done # echo "$x accounts exported to "$PWD/$file"" # sleep 5
Thanks in advance.
You should be able to do this easily with awk. Look at the -F option for defining fields. The follow command line will pluck field 1 and field 5 from your example. $ echo "k.thomas:x:1918:100:Kimaura Thomas:/home/users/k.thomas:/bin/usersh" | awk -F : '{ print $1", "$5 }' Or, in your script . . . user=echo $linea | awk -F : '{ print $1 }' nombre=echo $linea | awk -F : '{ print $5 }' . . . That's about as inelegant as it comes but it should be easy to understand. There is a fantastic (g)awk manual at http://www.gnu.org/software/gawk/manual/ presented in a number of formats.
You should be able to do this easily with awk. Look at the -F option for defining fields. The follow command line will pluck field 1 and field 5 from your example. $ echo "k.thomas:x:1918:100:Kimaura Thomas:/home/users/k.thomas:/bin/usersh" | awk -F : '{ print $1", "$5 }' Or, in your script . . . user=echo $linea | awk -F : '{ print $1 }' nombre=echo $linea | awk -F : '{ print $5 }' . . . That's about as inelegant as it comes but it should be easy to understand. There is a fantastic (g)awk manual at http://www.gnu.org/software/gawk/manual/ presented in a number of formats. _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
thanks Robert,
If i do what you said for linia in `cat /etc/passwd` do user=echo $linea | awk -F : '{ print $1 }' nombre=echo $linea | awk -F : '{ print $5 }' echo "the name is $nombre" echo "zmprov ma $user@$domain displayName $nombre">>$file
x=$[x+1] done
then I have: the name is echo
So it seems i have problems with quotes... either " or ` goes somewhere.... now..on my way to reaad about escape sequences in echo.
Am 27.11.2008 um 21:35 schrieb Erick Perez:
thanks Robert,
If i do what you said for linia in `cat /etc/passwd` do user=echo $linea | awk -F : '{ print $1 }' nombre=echo $linea | awk -F : '{ print $5 }' echo "the name is $nombre" echo "zmprov ma $user@$domain displayName $nombre">>$file
x=$[x+1] done
then I have: the name is echo
So it seems i have problems with quotes... either " or ` goes somewhere.... now..on my way to reaad about escape sequences in echo.
Backquotes Around everything right of the "=".
Rainer
On Thu, Nov 27, 2008 at 01:56:11PM -0500, Erick Perez wrote:
So far, cut returns the name up to the space, so in this case it will return Kimaura and not Kimaura Thomas.
No, it doesn't.
# for linia in `cat /etc/passwd`
This is your mistake. Think about it for linia in `cat /etc/passwd` do echo Line just read: $linia done
That shows what is going wrong; the "in" is splitting at the white space.
What you should be doing cat /etc/passwd | while read linia instead.
Even better would be while read linia do .... done < /etc/passwd
Or, to rewrite the whole program in a one line awk script:
awk -F: '{printf("zmprov ma %s@'$domain' displayName %s\n",$1,$5)}' /etc/passwd > $file
On Thu, Nov 27, 2008 at 2:37 PM, Stephen Harris lists@spuddy.org wrote:
On Thu, Nov 27, 2008 at 01:56:11PM -0500, Erick Perez wrote:
So far, cut returns the name up to the space, so in this case it will return Kimaura and not Kimaura Thomas.
No, it doesn't.
# for linia in `cat /etc/passwd`
This is your mistake. Think about it for linia in `cat /etc/passwd` do echo Line just read: $linia done
That shows what is going wrong; the "in" is splitting at the white space.
What you should be doing cat /etc/passwd | while read linia instead.
Even better would be while read linia do .... done < /etc/passwd
Or, to rewrite the whole program in a one line awk script:
awk -F: '{printf("zmprov ma %s@'$domain' displayName %s\n",$1,$5)}' /etc/passwd > $file
--
rgds Stephen _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Stephen, you are right. i learned a bit more about "for" cycles and the blank space was being stripped. Rainer, some backquotes as you said were missing.
So the code ends like this, and it works :))
#while read linia #do # user=`echo $linia | awk -F : '{ print $1 }'` # nombre=`echo $linia | awk -F : '{ print $5 }'` # echo "el nombre es $nombre" # echo "zmprov ma $user@$domain displayName $nombre">>$file # # x=$[x+1] #done < /etc/passwd
Stephen, your single line awk command is wonderful. the while cycle takes about a minute (it is a very very long passwd file) the awk took less than 10 seconds
.........I have so much to learn....