10. Í£Ö¹¶Ô cat ʹÓùܵÀ
grep µÄÒ»¸ö³£¼ûµÄ»ù±¾Ó÷¨´íÎóÊÇͨ¹ý¹ÜµÀ½« cat µÄÊä³ö·¢Ë͵½ grep ÒÔËÑË÷µ¥¸öÎļþµÄÄÚÈÝ¡£Õâ¾ø¶ÔÊDz»±ØÒªµÄ£¬´¿´âÊÇÀË·Ñʱ¼ä£¬ÒòΪÖîÈç grep ÕâÑùµÄ¹¤¾ß½ÓÊÜÎļþÃû×÷Ϊ²ÎÊý¡£Äú¸ù±¾²»ÐèÒªÔÚÕâÖÖÇé¿öÏÂʹÓà cat£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 21. ºÃÏ°¹ßºÍ»µÏ°¹ß 10 µÄʾÀý£ºÊ¹ÓôøºÍ²»´ø cat µÄ grep
~ $ time cat tmp/a/longfile.txt | grep and
2811
real 0m0.015s
user 0m0.003s
sys 0m0.013s
~ $ time grep and tmp/a/longfile.txt
2811
real 0m0.010s
user 0m0.006s
sys 0m0.004s
~ $
´Ë´íÎó´æÔÚÓÚÐí¶à¹¤¾ßÖС£ÓÉÓÚ´ó¶àÊý¹¤¾ß¶¼½ÓÊÜʹÓÃÁ¬×Ö·û (-) µÄ±ê×¼ÊäÈë×÷Ϊһ¸ö²ÎÊý£¬Òò´Ë¼´Ê¹Ê¹Óà cat À´·ÖÉ¢ stdin ÖеĶà¸öÎļþ£¬²ÎÊýҲͨ³£ÊÇÎÞЧµÄ¡£½öµ±ÄúʹÓôø¶à¸öɸѡѡÏîÖ®Ò»µÄ cat ʱ£¬²ÅÕæÕýÓбØÒªÔڹܵÀÇ°Ê×ÏÈÖ´ÐÐÁ¬½Ó¡£
11. ʹÓÃÎļþÃûÍê³É
Èç¹û²»ÐèÒªÔÚÃüÁîÌáʾ·û´¦¼üÈ볤µÄ¡¢ÁîÈ˷ѽâµÄÎļþÃû£¬ÕâÊDz»ÊǺܰôÄØ£¿µÄÈ·£¬Äú²»ÐèÒªÕâÑù×ö¡£Ïà·´£¬Äú¿ÉÒÔÅäÖÃ×îÁ÷ÐÐµÄ UNIX Shell ÒÔʹÓÃÎļþÃûÍê³É¡£¸Ã¹¦ÄÜÔÚ¸÷¸ö Shell ÖеŤ×÷·½Ê½ÂÔÓв»Í¬£¬Òò´ËÎÒ½«ÏòÄúչʾÈçºÎÔÚ×îÁ÷ÐÐµÄ Shell ÖÐʹÓÃÎļþÃûÍê³É¡£ÎļþÃûÍê³ÉʹÄú¿ÉÒÔ¸ü¿ìµØÊäÈë²¢±ÜÃâ´íÎó¡£ÀÁ¶è£¿Ò²Ðí°É¡£Ð§Âʸü¸ß£¿µ±È»£¡
³£ÓÃÊ××ÖĸËõд´Ê
1) MB£ºÕ××Ö½Ú
2) HTTP£º³¬Îı¾´«ÊäÐÒé
3) HTTPS£ºHTTP over Secure Sockets Layer
4) FTP£ºÎļþ´«ÊäÐÒé
5) FTPS£ºFTP over Secure Sockets Layer
6) LDAP£ºÇáÐÍĿ¼·ÃÎÊÐÒé
ÎÒÕýÔÚÔËÐÐÄÄÖÖ Shell£¿
Èç¹ûÄú²»ÖªµÀĿǰʹÓõÄÊÇÄÄÒ»ÖÖ Shell£¬»áÔõôÑù£¿ËäÈ»Õâ¸ö¾÷Çϲ»ÊÇÁíÍâ 10 ¸öºÃÏ°¹ßµÄÕýʽ×é³É²¿·Ö£¬µ«ËüÈÔÈ»ºÜÓÐÓ᣿ÉÒÔʹÓà echo $0 »ò ps -p $$ ÃüÁîÏÔʾÄúÕýÔÚʹÓÃµÄ Shell¡£¶ÔÓÚÎÒÀ´Ëµ£¬ÔËÐеÄÊÇ Bash Shell¡£
Çåµ¥ 1. È·¶¨ÄúµÄ Shell
$ echo $0
-bash
$ ps ¨Cp $$
PID TTY TIME CMD
6344 ttys000 0:00.02 ¨Cbash
C Shell
C Shell Ö§³Ö×îÖ±½ÓÎļþÃûÍê³É¹¦ÄÜ¡£ÉèÖà filec ±äÁ¿¿ÉÆôÓøù¦ÄÜ¡££¨Äú¿ÉÒÔʹÓÃÃüÁî set filec¡££©ÔÚÄú¿ªÊ¼¼üÈëÎļþÃûºó£¬¿ÉÒÔ°´ Esc ¼ü£¬Shell ½«Íê³ÉÎļþÃû£¬»òÍê³É¾¡¿ÉÄܶàµÄ²¿·Ö¡£ÀýÈ磬¼ÙÉèÄúÓµÓÐÃûΪ file1¡¢file2 ºÍ file3 µÄÎļþ¡£Èç¹ûÄú¼üÈë f£¬È»ºó°´ Esc ¼ü£¬½«Ìî³ä file£¬¶øÄú±ØÐë¼üÈë 1¡¢2 »ò 3 À´Íê³ÉÏàÓ¦µÄÎļþÃû¡£
Bash
Bash Shell Ò²ÌṩÁËÎļþÃûÍê³É£¬µ«Ê¹Óà Tab ¼ü´úÌæ Esc ¼ü¡£ÄúÔÚ Bash Shell Öв»ÐèÒªÉèÖÃÈκÎÑ¡Ïî¼´¿ÉÆôÓÃÎļþÃûÍê³É£¬¸ÃÑ¡ÏîÊÇȱʡÉèÖõġ£Bash »¹ÊµÏÖÁËÆäËû¹¦ÄÜ¡£¼üÈëÎļþÃûµÄÒ»²¿·Öºó£¬°´ Tab ¼ü£¬Èç¹ûÓжà¸öÎļþÂú×ãÄúµÄÇëÇ󣬲¢ÇÒÄúÐèÒªÌí¼ÓÎı¾ÒÔÑ¡ÔñÆäÖÐÒ»¸öÎļþ£¬ÄÇôÄú¿ÉÒԶఴ Tab ¼üÁ½´Î£¬ÒÔÏÔʾÓëÄúÄ¿Ç°¼üÈëµÄÄÚÈÝÏàÆ¥ÅäµÄÎļþµÄÁÐ±í¡£Ê¹ÓÃ֮ǰÃûΪ file1¡¢file2 ºÍ file3 µÄÎļþʾÀý£¬Ê×ÏȼüÈë f¡£µ±Äú°´Ò»´Î Tab ¼üʱ£¬Bash Íê³É file£»ÔÙ°´Ò»´Î Tab ¼üʱ£¬½«Õ¹¿ªÁбí file1 file2 file3¡£
Korn Shell
¶ÔÓÚ Korn Shell Óû§£¬ÎļþÃûÍê³ÉÈ¡¾öÓÚ EDITOR ±äÁ¿µÄÖµ¡£Èç¹û EDITOR ÉèÖÃΪ vi£¬ÄÇôÄú¼üÈ벿·ÖÃû³Æ£¬È»ºó°´ Esc ¼ü£¬ºó¸ú·´Ð±¸Ü (\) ×Ö·û¡£Èç¹û EDITOR ÉèÖÃΪ emacs£¬ÄÇôÄú¼üÈ벿·ÖÃû³Æ£¬È»ºó°´Á½´Î Esc ¼üÒÔÍê³ÉÎļþÃû¡£
12. ʹÓÃÀúÊ·À©Õ¹
Èç¹ûÄúΪһϵÁÐÃüÁîʹÓÃÏàͬµÄÎļþÃû£¬»á·¢ÉúʲôÇé¿ö£¿µ±È»£¬ÓÐÒ»ÖÖ¿ì½Ý·½Ê½¿ÉÒÔ¿ìËÙ»ñµÃÄúÉÏ´ÎʹÓõÄÎļþÃû¡£ÈçÇåµ¥ 2 Ëùʾ£¬!$ ÃüÁî·µ»ØÇ°Ò»¸öÃüÁîʹÓõÄÎļþÃû¡£´ÓÎļþ this-is-a-long-lunch-menu-file.txt ÖÐËÑË÷µ¥´Ê pickles µÄ³öÏÖλÖá£ËÑË÷½áÊøºó£¬Ê¹Óà vi ÃüÁîÀ´±à¼ this-is-a-long-lunch-menu-file.txt Îļþ£¬¶ø²»ÐèÒªÖØмüÈëÎļþÃû¡£ÄúʹÓøÐ̾ºÅ (!) À´·ÃÎÊÀúÊ·£¬È»ºóʹÓÃÃÀÔª·ûºÅ ($) ·µ»ØÇ°Ò»ÃüÁîµÄ×îºó×ֶΡ£Èç¹ûÄú·´¸´Óõ½³¤ÎļþÃû£¬ÄÇôÕâÊÇÒ»¸ö·Ç³£ºÃµÄ¹¤¾ß¡£
Çåµ¥ 2. ʹÓà !$ »ñµÃÇ°Ò»¸öÃüÁîʹÓõÄÎļþÃû
$ grep pickles this-is-a-long-lunch-menu-file.txt
pastrami on rye with pickles and onions
$ vi !$
13. ÖØÓÃÒÔÇ°µÄ²ÎÊý
!$ ÃüÁî·µ»Øij¸öÃüÁîʹÓõÄÉÏÒ»¸öÎļþÃû²ÎÊý¡£µ«Èç¹ûij¸öÃüÁîʹÓöà¸öÎļþÃû£¬¶øÄúֻϣÍûÖØÓÃÆäÖÐÒ»¸öÎļþÃû£¬¸ÃÈçºÎ×ö£¿!:1 ²Ù×÷·û·µ»Øij¸öÃüÁîʹÓõĵÚÒ»¸öÎļþÃû¡£Çåµ¥ 3 ÖеÄʾÀýÏÔʾ¿ÉÒÔÈçºÎ½«´Ë²Ù×÷·ûÓë !$ ÔËËã·û×éºÏʹÓá£ÔÚµÚÒ»¸öÃüÁîÖУ¬½«Ò»¸öÎļþÖØÐÂÃüÃûΪ¸üÓÐÒâÒåµÄÃû³Æ£¬µ«ÎªÁ˱£³ÖÔʼÎļþÃû¿ÉÓ㬴´½¨ÁËÒ»¸ö·ûºÅÁ´½Ó¡£ÖØÐÂÃüÃûÎļþ kxp12.c ÒÔÌá¸ß¿É¶ÁÐÔ£¬È»ºóʹÓà link ÃüÁîÀ´´´½¨µ½ÔʼÎļþÃûµÄ·ûºÅÁ´½Ó£¬ÒÔ·ÀÔÚÆäËûλÖÃʹÓøÃÎļþÃû¡£!$ ²Ù×÷·û·µ»Ø file_system_access.c ÎļþÃû£¬¶ø !:1 ²Ù×÷·û·µ»Ø kxp12.c ÎļþÃû£¬¸ÃÎļþÃûÊÇÉϸöÃüÁîµÄµÚÒ»¸öÎļþÃû¡£
Çåµ¥ 3. ×éºÏʹÓà !$ ºÍ !:1
$ mv kxp12.c file_system_access.c
$ ln ¨Cs !$ !:1
14. ʹÓà pushd ºÍ popd ¹ÜÀíĿ¼µ¼º½
UNIX Ö§³Ö¸÷ÖÖĿ¼µ¼º½¹¤¾ß¡£×îϲ»¶µÄÁ½¿îÌá¸ß¹¤×÷ЧÂʵŤ¾ßÊÇ pushd ºÍ popd¡£Äúµ±È»Á˽â cd ÃüÁîÓÃÓÚ¸ü¸ÄÄúµÄµ±Ç°Ä¿Â¼¡£Èç¹ûÄúÒªÔÚ¶à¸öĿ¼Öе¼º½£¬µ«Ï£ÍûÄܹ»¿ìËÙ·µ»Øij¸öλÖ㬸ÃÈçºÎ×ö£¿pushd ºÍ popd ÃüÁî´´½¨Ò»¸öÐéÄâĿ¼¶ÑÕ»£¬pushd ÃüÁîÓÃÀ´¸ü¸ÄÄúµÄµ±Ç°Ä¿Â¼²¢½«Æä´æ´¢ÔÚ¶ÑÕ»ÖУ¬¶ø popd ÃüÁîÓÃÀ´´Ó¶ÑÕ»µÄ¶¥²¿ÒƳýĿ¼²¢Ê¹Äú·µ»Ø¸ÃλÖá£Äú¿ÉÒÔʹÓà dirs ÃüÁîÀ´ÏÔʾµ±Ç°Ä¿Â¼¶ÑÕ»£¬¶ø²»»áѹÈë»òµ¯³öÐÂĿ¼¡£Çåµ¥ 4 ÏÔʾÈçºÎʹÓà pushd ºÍ popd ÃüÁîÔÚĿ¼Ê÷ÖпìËÙµ¼º½¡£
Çåµ¥ 4. ʹÓà pushd ºÍ popd ÔÚĿ¼Ê÷Öе¼º½
$ pushd .
~ ~
$ pushd /etc
/etc ~ ~
$ pushd /var
/var /etc ~ ~
$ pushd /usr/local/bin
/usr/local/bin /var /etc ~ ~
$ dirs
/usr/local/bin /var /etc ~ ~
$ popd
/var /etc ~ ~
$ popd
/etc ~ ~
$ popd
~ ~
$ popd
pushd ºÍ popd ÃüÁ֧³ÖʹÓòÎÊý´¦ÀíĿ¼¶ÑÕ»¡£Ê¹Óà +n »ò -n ²ÎÊý£¬ÆäÖÐ n ÊÇÒ»¸öÊý×Ö£¬Äú¿ÉÒÔÏò×ó»òÏòÓÒÒƶ¯¶ÑÕ»£¬ÈçÇåµ¥ 5 Ëùʾ¡£
Çåµ¥ 5. ÐýתĿ¼¶ÑÕ»
$ dirs
/usr/local/bin /var /etc ~ ~
$ pushd +1
/var /etc ~ ~ /usr/local/bin
$ pushd -1
~ /usr/local/bin /var /etc ~
15. ²éÕÒ´óÐÍÎļþ
ÊÇ·ñÐèÒªÕÒ³öÄúµÄËùÓпÕÏдÅÅ̿ռ䱻ʲôռÓÃÁË£¿Äú¿ÉÒÔʹÓÃÒÔϼ¸¸ö¹¤¾ßÀ´¹ÜÀíÄúµÄ´æ´¢É豸¡£ÈçÇåµ¥ 6 Ëùʾ£¬df ÃüÁîΪÄúÏÔʾÿ¸ö¿ÉÓþíÉÏÒÑʹÓõĿéµÄ×ÜÊý£¬ÒÔ¼°¿ÕÏпռäµÄ°Ù·Ö±È¡£
Çåµ¥ 6. È·¶¨¾íµÄʹÓÃÇé¿ö
$ df
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk0s2 311909984 267275264 44122720 86% /
devfs 224 224 0 100% /dev
fdesc 2 2 0 100% /dev
map -hosts 0 0 0 100% /net
map auto_home 0 0 0 100% /home
ÊÇ·ñÏ£Íû²éÕÒ´óÐÍÎļþ£¿Ê¹Óà find ÃüÁîʱ¸½´ø -size ²ÎÊý¡£Çåµ¥ 7 ÏÔʾÁËÈçºÎʹÓà find ÃüÁîÀ´²éÕÒ´óÓÚ 10MB µÄÎļþ¡£Çë×¢Ò⣬-size ²ÎÊýÒÔ KB Ϊµ¥Î»¼ÆÁ¿´óС¡£
Çåµ¥ 7. ²éÕÒ´óÓÚ 10MB µÄËùÓÐÎļþ
$ find / -size +10000k ¨Cxdev ¨Cexec ls ¨Clh {}\;
16. ²»Ê¹Óñà¼Æ÷´´½¨ÁÙʱÎļþ
ÒÔÏÂÊÇÒ»¸ö¼òµ¥Ê¾Àý£ºÄúÐèÒª¿ìËÙ´´½¨Ò»¸ö¼òµ¥ÁÙʱÎļþ£¬µ«²»Ï£ÍûÆô¶¯ÄúµÄ±à¼Æ÷¡£Ê¹ÓôøÓÐ > ÎļþÖض¨Ïò²Ù×÷·ûµÄ cat ÃüÁî¡£ÈçÇåµ¥ 8 Ëùʾ£¬Ê¹Óò»´øÎļþÃûµÄ cat ÃüÁîÖ»»ØÏÔÏò±ê×¼ÊäÈë¼üÈëµÄÈκÎÄÚÈÝ£»> Öض¨Ïò½«¸ÃÊäÈ벶»ñµ½Ö¸¶¨µÄÎļþÖС£Çë×¢Ò⣬ÄúÔÚ½áÊø¼üÈëʱ±ØÐëÌṩÎļþ½áÊø×Ö·û£¬Í¨³£Îª Ctrl-D¡£
Çåµ¥ 8. ¿ìËÙ´´½¨ÁÙʱÎļþ
$ cat > my_temp_file.txt
This is my temp file text
^D
$ cat my_temp_file.txt
This is my temp file text
ÐèÒªÖ´ÐÐÏàͬ²Ù×÷£¬µ«ÊǸ½¼Óµ½ÏÖÓÐÎļþ¶ø²»ÊÇ´´½¨ÐÂÎļþ¡£ÈçÇåµ¥ 9 Ëùʾ£¬¸ÄÓà >> ²Ù×÷·û¡£>> ÎļþÖض¨Ïò²Ù×÷·ûÏòÏÖÓÐÎļþ¸½¼ÓÄÚÈÝ¡£
Çåµ¥ 9.¿ìËÙÏòÎļþ¸½¼ÓÄÚÈÝ
$ cat >> my_temp_file.txt
More text
^D
$ cat my_temp_file.txt
This is my temp file text
More text
17. ʹÓà curl ÃüÁîÐÐʵÓù¤¾ß
curl ÃüÁîʹÄú¿ÉÒÔʹÓà HTTP¡¢HTTPS¡¢FTP¡¢FTPS¡¢Gopher¡¢DICT¡¢TELNET¡¢LDAP »ò FILE ÐÒé´Ó·þÎñÆ÷¼ìË÷Êý¾Ý¡£ÈçÇåµ¥ 10 Ëùʾ£¬ÎÒ¿ÉÒÔʹÓà curl ÃüÁî´ÓÃÀ¹ú¹ú¼ÒÆøÏó¾ÖÁ˽âÎÒËùÔÚλÖã¨Å¦Ô¼Öݲ¼·¨ÂÞÊУ©µÄµ±Ç°ÌìÆø×´¿ö¡£µ±Óë grep ÃüÁî×éºÏʹÓÃʱ£¬ÎÒ¿ÉÒÔ¼ìË÷²¼·¨ÂÞÊеÄÌìÆø×´¿ö¡£Ê¹Óà -s ÃüÁîÐÐÑ¡ÏîÀ´½ûÖ¹ curl ´¦ÀíÊä³ö¡£
Çåµ¥ 10. ʹÓà curl ¼ìË÷µ±Ç°ÌìÆø×´¿ö
$ curl ¨Cs http://www.srh.noaa.gov/data/ALY/RWRALY | grep BUFFALO
BUFFALO MOSUNNY 43 22 43 NE13 30.10R
ÈçÇåµ¥ 11 Ëùʾ£¬ÄúÒ²¿ÉÒÔʹÓà curl ÃüÁîÀ´ÏÂÔØ HTTP ÍйܵÄÎļþ¡£Ê¹Óà -o ²ÎÊýÀ´Ö¸¶¨±£´æÊä³öµÄλÖá£
Çåµ¥ 11. ʹÓà curl ÏÂÔØ HTTP ³ÐÔصÄÎļþ
$ curl -o archive.tar http://www.somesite.com/archive.tar
Õâʵ¼ÊÉÏÖ»ÊÇÄúʹÓà curl ÃüÁî¿ÉÒÔÍê³ÉµÄ²Ù×÷µÄÌáʾ¡£ÄúÖ»ÐèÔÚÃüÁîÌáʾ·û´¦¼üÈë man curl ÏÔʾ curl ÃüÁîµÄÍêÕûʹÓÃÐÅÏ¢£¬¾Í¿ÉÒÔ¿ªÊ¼Á˽â¸ü¶àÄÚÈÝ¡£
18. ×îÓÐЧµØÀûÓÃÕýÔò±í´ïʽ
´óÁ¿ UNIX ÃüÁîʹÓÃÕýÔò±í´ïʽ×÷Ϊ²ÎÊý¡£´Ó¼¼Êõ½Ç¶È¶øÑÔ£¬ÕýÔò±í´ïʽ ÊDZíʾijÖÖģʽµÄ×Ö·û´®£¨Ò²¾ÍÊÇ˵£¬ÓÉ×Öĸ¡¢Êý×ֺͷûºÅ×é³ÉµÄ×Ö·ûÐòÁУ©£¬ÓÃÓÚ¶¨ÒåÁã»ò¸ü³¤µÄ×Ö·û´®¡£ÕýÔò±í´ïʽʹÓÃÔª×Ö·û£¨ÀýÈ磬ÐǺŠ[*] ºÍÎʺŠ[?]£©À´Æ¥ÅäÆäËû×Ö·û´®µÄ²¿·Ö»òÈ«²¿ÄÚÈÝ¡£ÕýÔò±í´ïʽ²»Ò»¶¨°üº¬Í¨Åä·û£¬µ«Í¨Åä·û¿ÉÒÔʹÕýÔò±í´ïʽÔÚËÑË÷ģʽºÍ´¦ÀíÎļþʱ·¢»Ó¸ü´óµÄ×÷Óᣱí 1 ÏÔʾÁËһЩ»ù±¾ÕýÔò±í´ïʽÐòÁС£
±í 1. ÕýÔò±í´ïʽÐòÁÐ
Çåµ¥ 12 ÏÔʾÁËÓë grep ÃüÁîÒ»ÆðʹÓõÄһЩ»ù±¾ÕýÔò±í´ïʽ¡£
Çåµ¥ 12. ʹÓÃÕýÔò±í´ïʽºÍ grep
$ # Lists your mail
$ grep '^From: ' /usr/mail/$USER
$ # Any line with at least one letter
$ grep '[a-zA-Z]' search-file.txt
$ # Anything not a letter or number
$ grep '[^a-zA-Z0-9] search-file.txt
$ # Find phone numbers in the form 999-9999
$ grep '[0-9]\{3\}-[0-9]\{4\}' search-file.txt
$ # Find lines with exactly one character
$ grep '^.$' search-file.txt
$ # Find any line that starts with a period "."
$ grep '^\.' search-file.txt
$ # Find lines that start with a "." and 2 lowercase letters
$ grep '^\.[a-z][a-z]' search-file.txt
ÓйØÃüÁîÐÐÕýÔò±í´ïʽµÄÉîÈëÃèÊö£¬ÔĶÁ developerWorks ÎÄÕÂ"¶Ô»° UNIX£¬µÚ 9 ²¿·Ö£ºÕýÔò±í´ïʽ¡£"
19. È·¶¨µ±Ç°Óû§
ÓÐʱ£¬Äú¿ÉÄÜÏ£ÍûÈ·¶¨Ä³¸öÌض¨Óû§ÊÇ·ñÔËÐйýÄúµÄ¹ÜÀí½Å±¾¡£ÎªÕÒ³ö´ð°¸£¬Äú¿ÉÒÔʹÓà whoami ÃüÁîÀ´·µ»Øµ±Ç°Óû§µÄÃû³Æ¡£Çåµ¥ 13 ÏÔʾÁ˶À×ÔÔËÐÐµÄ whoami ÃüÁÇåµ¥ 14 ÏÔʾÁËʹÓà whoami È·±£µ±Ç°Óû§²»ÊǸùÓû§µÄ Bash ½Å±¾µÄժ¼¡£
Çåµ¥ 13. ´ÓÃüÁîÐÐʹÓà whoami
$ whoami
John
Çåµ¥ 14. Ôڽű¾ÖÐʹÓà whoami
if [ $(whoami) = "root" ]
then
echo "You cannot run this script as root."
exit 1
fi
20. ʹÓà awk ´¦ÀíÊý¾Ý
awk ÃüÁîËƺõʼÖÕ´¦ÔÚ Perl µÄÒõÓ°Ï£¬µ«Ëü¶ÔÓÚ¼òµ¥¡¢»ùÓÚÃüÁîÐеÄÊý¾Ý´¦ÀíÀ´ËµÊÇÒ»¸ö¿ìËÙ¡¢ÊµÓõŤ¾ß¡£Çåµ¥ 15 ÏÔʾÁËÈçºÎ¿ªÊ¼Ê¹Óà awk ÃüÁî¡£ÈôÒª»ñÈ¡ÎļþÖÐÿÐÐÎı¾µÄ³¤¶È£¬ÇëʹÓà length() º¯Êý¡£ÈôÒª²é¿´×Ö·û´® ing ÊÇ·ñ³öÏÖÔÚÎļþÎı¾ÖУ¬ÇëʹÓà index() º¯Êý£¬¸Ãº¯Êý·µ»Ø ing Ê״γöÏÖµÄλÖã¬ÕâÑùÄú¾Í¿ÉÒÔʹÓÃËüÀ´½øÐнøÒ»²½µÄ×Ö·û´®´¦Àí¡£ÈôÒª tokenize£¨Ò²¾ÍÊÇ˵£¬½«Ò»Ðвð·ÖΪµ¥´Ê³¤¶ÈµÄƬ¶Î£©Ä³¸ö×Ö·û´®£¬ÇëʹÓà split() º¯Êý¡£
Çåµ¥ 15. »ù±¾ awk ´¦Àí
$ cat text
testing the awk command
$ awk '{ i = length($0); print i }' text
23
$ awk '{ i = index($0,"ing"); print i}' text
5
$ awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) {print a[i]; i++;} }' text
testing
the
awk
command
´òÓ¡Îı¾ÎļþÖеÄÖ¸¶¨×Ö¶ÎÊÇÒ»Ïî¼òµ¥µÄ awk ÈÎÎñ¡£ÔÚÇåµ¥ 16 ÖУ¬sales Îļþ°üº¬Ã¿¸öÏúÊÛÈËÔ±µÄÐÕÃû£¬ºó¸úÿÔÂÏúÊÛÊý×Ö¡£Äú¿ÉÒÔʹÓà awk ÃüÁîÀ´¿ìËÙ»ñµÃÿ¸öÔµÄÏúÊÛ×ܶȱʡÇé¿öÏ£¬awk ½«Ã¿¸öÒÔ¶ººÅ·Ö¸ôµÄÖµÊÓΪ²»Í¬µÄ×ֶΡ£ÄúʹÓà $n ²Ù×÷·ûÀ´·ÃÎÊÿ¸ö×ֶΡ£
Çåµ¥ 16. ʹÓà awk ¶ÔÊý¾Ý½øÐлã×Ü
$cat sales
Gene,12,23,7
Dawn,10,25,15
Renee,15,13,18
David,8,21,17
$ awk -F, '{print $1,$2+$3+$4}' sales
Gene 42
Dawn 50
Renee 46
David 46
³ÉΪÃüÁîÐиßÊÖÐèÒª½øÐÐһЩʵ¼ù¡£°´ÕÕÏàͬµÄ·½Ê½´¦ÀíÎÊÌâºÜ¼òµ¥£¬ÒòΪÄúÒѾϰ¹ßÁË¡£À©Õ¹ÄúµÄÃüÁîÐÐ×ÊÔ´¿ÉÒÔÏÔÖøÌá¸ßÄúµÄ¹¤×÷ЧÂÊ£¬²¢´ÙʹÄú³¯×Å UNIX ÃüÁîÐиßÊֵķ½ÏòÇ°½ø£¡
ÍƼöÐÅÏ¢
- linuxÃüÁîѧϰ±Ê¼Ç£¨11£©£ºnlÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨5£©£ºrmÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨4£©£ºmkdirÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨1£©£ºlsÃüÁî
- ½«CentosµÄyumÔ´¸ü»»Îª¹úÄڵİ¢ÀïÔÆÔ´
- ʹÓÃNginxÌí¼Óheader·ÀÖ¹ÍøÒ³±»frame
- linuxϼÓËÙscp´«Êä´óÎļþµÄËÙ¶È
- linuxϵͳÉ϶ÔnginxÈÕÖ¾·Ö¸î´¦Àí
- lnmp/nginxϵͳÕæÕýÓÐЧµÄͼƬ·ÀµÁÁ´ÍêÕûÉèÖÃÏê½â
- Í»ÆÆÊ®Íò²¢·¢µÄNginxµÄÅäÖü°ÓÅ»¯
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- ²éÕÒ²¢É¾³ý.svnĿ¼Îļþ
- redis ÆßÖÖÊý¾ÝÀàÐ͵ÄʹÓó¡¾°
- linux ÏÂÎļþ¸´ÖƵ½windowsÏÂÂÒÂëµÄ½â¾ö°ì·¨
- nginx³öÏÖ502 upstream sent too big he....
- linuxÏÂsudoÅäÖÃÏê½â
- linuxÃüÁîѧϰ±Ê¼Ç£¨15£©£ºtailÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨14£©£ºheadÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨13£©£ºlessÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨12£©£ºmoreÃüÁî
- ¼ÓÃÜËã·¨±È½Ï3DES AES RSA ECC MD5 SHA1µÈ
ÆÀÂÛ