$oldIFS="$IFS"#保存旧值$IFS=":"$whileread user password uid gid rest_of_line;doif["$user"=="tom"];thenecho"$user's ID is $uid"fidone< /etc/passwd
$IFS="$oldIFS"#恢复旧值
[tom@ton ~]$./script.awk
Writing my first awk executable script![tom@ton ~]$a2p script.awk #自动重写成Perl脚本#!/usr/bin/perleval'exec /usr/bin/perl -S $0${1+"$@"}'if$running_under_some_shell;# this emulates #! processing on NIH machines.# (remove #! line above if indigestible)eval'$'.$1.'$2;'while$ARGV[0]=~ /^([A-Za-z_0-9]+=)(.*)/ &&shift;# process any FOO=bar switchesprintf"%s\n", 'Writing my first awk executable script!';
new-host-808:/etc/sysconfig/network # head -1 /etc/passwd | \awk'BEGIN {FS=":";ORS=" "} {for(i=3;i<=NF;i++) {print $i}} END{print "\n"}'
499 499 User for Avahi /run/avahi-daemon /bin/false
awk 创建用户脚本
#!/usr/bin/awk -f# Script to create a file suitable for use in the 'newusers'command,#from a file consisting of user IDs and passwords in the form:#first_name last_name password#Copyright (c) KMSelf Sat Aug 25 20:47:38 PDT 2001#Distributed under GNU GPL v 2, or at your option, any later version.#This program is distributed WITHOUT ANY WARRANTY.
BEGIN{#Assign starting UID, GIDif(ARGC > 2){
startuid = ARGV[1]
delete ARGV[1]}
else{
printf("Usage: newusers startUID file\n"\
"”where:\n" \
" startUID is the starting userid to add, and\n"\
" file is an input file in form: \n"\
" first_name last_name password\n"\
)exit}
infile = ARGV[1]
printf("Starting UID: %s\n\n", startuid )}
/^#/ { next } #遇到#开头的行,就跳过{
++record
first =$1
last =$2passwd=$3
user = substr( tolower( first ),1,1) tolower( last )
uid = startuid + record - 1
gid = uid
printf("%s:%s:%d:%d:%s %s,, /home/%s:/bin/bash\n",\
user,passwd, uid, gid, first,last,user \
)}