ºìÁªLinuxÃÅ»§
Linux°ïÖú

Âí¸çlinux·ÖÏíÖ®LinuxÈý½£¿ÍÖ®sedºÍawkµÄ»ù±¾Ó¦ÓÃ

·¢²¼Ê±¼ä:2014-08-25 17:48:50À´Ô´:ºìÁª×÷Õß:СС295813080
http://www.magedu.com/

sed¡¢awkºÍgrep±»³ÆΪLinuxµÄÈý½£¿Í£¬±¾ÎÄÖ÷Òª½²ÊösedºÍawkµÄ»ù±¾Ó¦Ó㬹ØÓÚgrepºÍÕýÔò±í´ïʽµÄÓ¦Óã¬ÇëÒƲ½±¾È˵ÄÁíÍâһƪ²©ÎÄ£¬Á´½ÓΪhttp://wangfeng7399.blog.51cto.com/3518031/1360804
Ò»¡¢sed
1£©sed¼ò½é
sedµÄÈ«³ÆΪstream editor£¬sedÔÚ´¦Àíʱһ´ÎÖ»¶ÁÈ¡ÎļþµÄÒ»Ðв¢¶ÔÕâÒ»ÐнøÐд¦Àí£¬²¢ÇÒsed½«´¦ÀíºóµÄÊý¾ÝÖ»»áÏÔʾÔÚÆÁÄ»ÉÏ£¬²¢²»»á¶ÔÔ­Îļþ½øÐÐÐ޸ģ¬ËùÒÔ˵sedÊÇÒ»¸öÐб༭Æ÷
2£©sedµÄʹÓøñʽ
sed [options] "script" FILE....
3)Ïê½â¸÷Ñ¡ÏîÓë±à¼­ÃüÁîµÄʹÓÃ
Ñ¡Ï
-n£º¾²Ä¬Ä£Ê½£¬²»Êä³öģʽ¿Õ¼äÄÚµÄÄÚÈÝ£¬Ä¬ÈÏ´òÓ¡¿Õ¼äģʽÄÚµÄÄÚÈÝ
-r£ºÀ©Õ¹µÄÕýÔò±í´ïʽ
-f Îļþ£ºÖ¸¶¨sed½Å±¾Îļþ
-i£ºÖ±½Ó±à¼­Ô´Îļþ
±à¼­ÃüÁî
d£ºÉ¾³ý
p£º´òÓ¡
i \:ÔÚ±»Ö¸¶¨µ½µÄÐÐÇ°Ãæ²åÈëÎı¾
a \:ÔÚ±»Ö¸¶¨µ½µÄÐÐÏÂÃæ²åÈëÎı¾
r Îļþ·¾¶£ºÔÚÖ¸¶¨µÄλÖòåÈëÁíÍâÒ»¸öÎļþµÄÄÚÈÝ
w Îļþ·¾¶£º½«·ûºÏÌõ¼þµÄËùÓÐÐб£´æÖÁÖ¸¶¨µÄÎļþÖÐ
= ÏÔʾ·ûºÏÌõ¼þµÄËùÔÚÐеÄÐкÅ
s@@@£º²éÕÒÌõ¼þÒ²¿ÉÒÔʹÓÃģʽ£¬µ«ÊDZ»Ìæ»»µÄÄÚÈݲ»ÄÜʹÓÃģʽ
ʹÓøñʽ£º
sed 'µØÖ·¶¨½çs@²éÕÒÌõ¼þ@Ìæ»»ºóµÄÎı¾@ÐÞÊηû
µØÖ·¶¨½ç£º×Ô¶¨ÒåµÄÆðʼÐе½½áÊøÐУ¬Ä¬ÈÏΪȫÎÄ
ÓÐÈýÖÖ·½Ê½±íʾ
startline£¬endline
/pat1/£¬/pat2/
/pattern/
Ó÷¨£ºsed [options] 'addr1[,addr2]±à¼­ÃüÁî'FILE...
sed [options] "addr1[,addr2]±à¼­ÃüÁî"FILE...
sedÔÚ×öÌ滻ʱ»¹Ö§³ÖÕýÔò±í´ïʽ£¬ÕýÔò±í´ïʽµÄʹÓñ¾´¦²»×ö½²½â¡£
ÐÞÊηû£º
g£ºglobal£¬È«¾ÖÌæ»»
i£ºignore-case£¬²»Çø·Ö×Ö·û´óСд
4£©Á·Ï°
1£©Ìæ»»/etc/inittabÎļþÖС±id:3:initdefault:"Ò»ÐÐÖеÄÊý×ÖΪ5
sed 's@\(id:\)[0-9]\(:initdefault\)@\15\2' /etc/inittab
2£©É¾³ý/etc/init.d/funcionsÎļþÖеĿհ×ÐÐ
sed "/^$/d" /etc/init.d/funcions
3£©É¾³ý/etc/inittabÎļþÖÐλÓÚÐÐÊ×µÄ#£»
sed 's@^#@@g' /etc/inittab
4£©É¾³ý/etc/rc.d/rc.sysinitÎļþÖÐÒÔ#ºó¸úÖÁÉÙÒ»¸ö¿Õ°××Ö·û¿ªÍ·µÄÐеÄÐÐÊ×µÄ#ºÍ¿Õ°××Ö·û¡£
sed "s@^#[[:space:]]\{1,\}\@@g" /etc/rc.d/rc.sysinit
5£©É¾³ý/boot/grub/grub.confÎļþÖÐÐÐÊ׵Ŀհ××Ö·û¡£
sed "s@^[[:space:]]\{1,\}@@g" /boot/grub/grub.conf
6£©È¡³öÒ»¸öÎļþ·¾¶µÄĿ¼Ãû³Æ£¬Èç/etc/sysconfig/network,ÆäĿ¼Ϊ/etc/sysconfig,¹¦ÄÜÀàËÆdirnameÃüÁ
echo /etc/sysconfig/network | sed "s@[^/]\{1,\}/\?$@@"
¶þ¡¢awk
1£©¼ò½é
awkÊÇÒ»¸ö±¨¸æÉú³É¹¤¾ß£¬Ö¼ÔÚ°ÑÎļþÖжÁÈ¡µ½µÄÿһÐеÄÿ¸ö×ֶηֱð½øÐиñʽ»¯£¬¶øºó½øÐÐÏÔʾ£¬awkÖ§³ÖʹÓñäÁ¿¡¢Ìõ¼þÅжϡ¢Ñ­»·¡¢Êý×é
ÏÖÔÚlinuxϵͳÉÏÓõ½µÄ¶¼ÊÇgawk
2£©¸ñʽ£º
awk [options] 'script'FILE....
awk [options] '/pattern/{action}'FILE...
3)Ñ¡ÏîÓëģʽµÄʹÓÃ
Ñ¡Ïî
-F:Ö¸¶¨Çиî·û£¬Ä¬ÈÏΪ¿Õ°×
$0:ÕûÐÐÏÔʾ
$1,$2.......:λÖòÎÊý
ģʽ
µØÖ·¶¨½ç£º
/pat1/£¬/pat2/£¬±íʾһ¸ö·¶Î§
/pattern/£¬±íʾ±»Æ¥Åäµ½µÄÐÐ
±í´ïʽ
>,>=,<,<=,==,!=,~
BEGIN:±íʾÔÚÖ´Ðж¯×÷Ç°Òª×öµÄ²Ù×÷
END£º±íʾÔÚ¶¯×÷Ö´ÐкóµÄ²Ù×÷
awkµÄ³£ÓÃËÄÖÖ·Ö¸ô·û£º
ÊäÈ룺
Ðзָô·û
×ֶηָô·û
Êä³ö£º
Ðзָô·û
×ֶηָô·û
awkµÄÄÚÖñäÁ¿
NF:×îºóÒ»¸ö×Ö¶Î
FS:Ö¸¶¨ÊäÈë·Ö¸ô·û£¬ÔÚBEGINÖж¨Ò壬Ï൱ÓÚ-F£¬Ä¬ÈÏΪ¿Õ°×
OFS:Ö¸¶¨Êä³öʱµÄ·Ö¸ô·û£¬ÔÚBEGINÖж¨Ò壬ĬÈÏΪ¿Õ°×
4£©Á·Ï°
1.ÏÔʾGIDСÓÚ500µÄ×é
awk -F : '$3<500{print $1}' /etc/group
2.ÏÔʾĬÈϵÄshellΪnologinµÄÓû§
awk -F : '$7~/nologin$/{print $1}' /etc/passwd
3£¬ÏÔʾeth0Íø¿¨ÎļþµÄÅäÖÃÐÅÏ¢£¬×¢Ò⣬ֻÏÔʾµÈºÅºóÃæµÄÖµ
awk -F = '{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0
4.ÏÔʾ/etc/sysctl¡£confÎļþÖж¨ÒåµÄÄں˲ÎÊý£ºÖ»ÏÔʾÃû³Æ£¬
awk -F =' !/^#|^$/{print $1}'/etc/sysctl.conf
5.ÏÔʾeth0Íø¿¨µÄipµØÖ·£¬Í¨¹ýifconfigÃüÁî½á¹û½øÐйýÂË
ifconfig eth0 |awk '/inet addr/{print $2}' |awk -F : '{print $2}'
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. µÀ123 ÓÚ 2015-04-16 15:00:51·¢±í:

    ÇóºìÆì8µÄgdb°²×°°ü