LinuxÒÁµéÔ°ÂÛ̳'s Archiver

oldwell_nirvana ·¢±íÓÚ 2006-12-22 17:25

¡¾ÇóÖú¡¿ÈçºÎʵÏÖ×Ö·û´®²»Æ¥ÅäʱµÄ²Ù×÷

cat file
/mnt/d/ccc.doc
/getip.sh
/jojo/game.sh
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
/ffff.gif
/db/aaaa.pic

ÏëÒª×öµ½Ò»µã:
1. Èç¹ûfile ÖÐûÓÐ×Ö·û´®string µÄ»°(²»Æ¥Åä), ÔòÔÚÎļþÄ©Ìí¼ÓÒ»ÐÐ, ÄÚÈÝΪstring;
2. Èç¹ûfile ÖÐÓÐ×Ö·û´®string µÄ»°(Æ¥Åä), Ôò°Ñ¸ÃÐиÄΪstring ok

ÇëÎÊÓÃsed ÃüÁî¸ÃÔõôд

zhuomingliang ·¢±íÓÚ 2006-12-22 17:39

sed ÎÒûÓз½·¨
  grep µÄÎÒµ½ÓÐ
  [CODE]liangke@liangke-desktop:/tmp$ cat file
/mnt/d/ccc.doc
/getip.sh
/jojo/game.sh
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
/ffff.gif
/db/aaaa.pic

liangke@liangke-desktop:/tmp$ grep 'mnt' file
/mnt/d/ccc.doc
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
liangke@liangke-desktop:/tmp$ echo $?
0

liangke@liangke-desktop:/tmp$ grep 'mntd' file
liangke@liangke-desktop:/tmp$ echo $?
1
liangke@liangke-desktop:/tmp$ [/CODE]

dearvoid ·¢±íÓÚ 2006-12-22 18:43

[code]
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file
hello
world
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f

/string/ {
    s/string/string ok/
    q
}

$ a\
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f

/string/ {
    s/string/string ok/
    q
}

$ a\
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] sed -i -f file.sed file ; cat file
hello
world
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] sed -i -f file.sed file ; cat file
hello
world
string ok
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] o
[/code]

dearvoid ·¢±íÓÚ 2006-12-22 18:52

file.sed ÕâÑùдҲÐÐ:
[code]
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f

s/string/string ok/
t done

$ a\
string

:done
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] o
[/code]

li-jiahuan ·¢±íÓÚ 2006-12-22 19:28

ÁÁÐÖû´ïµ½LZÒªÇ󣬺Ç
dearvoidµÄ´úÂëÖ»ÔÚÖ»Óе¥ÐÐstringʱҲ»á¹¤×÷Õý³£
ÔÚÖ»ÔËÐÐ1£¬2´ÎʱҲÕý³£
[code]

[0 No.2058 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string

[0 No.2059 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string ok

[0 No.2060 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string ok ok
[/code]

À´¸ö²»Âײ»ÀàµÄ£¬½áºÏÂ¥ÉÏÁ½¸ö´ó´óµÄÏë·¨
ÀÄÓà &&, || ÁË£º£©
[code]
[0 No.2098 huan@huan ~/tmp]$ cat txt
hello
world

[0 No.2099 huan@huan ~/tmp]$ grep -q 'string.*ok' txt ||  { grep -q 'string' txt &&  sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string

[0 No.2100 huan@huan ~/tmp]$ grep -q 'string.*ok' txt ||  { grep -q 'string' txt &&  sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string ok

[0 No.2101 huan@huan ~/tmp]$ grep -q 'string.*ok' txt ||  { grep -q 'string' txt &&  sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string ok

[0 No.2102 huan@huan ~/tmp]$

[/code]

µ±È»
Èç¹ûÂ¥Ö÷˵Ã÷ÓзûºÏ '^string$' ÕâÑùµÄÐÐ
»á¼òµ¥Ð©

zhuomingliang ·¢±íÓÚ 2006-12-22 20:39

°¥£¬¸Õ²Åæ×ÅÅäÖÃubuntuÁË£¬ÌâÄ¿ÎÒ¶¼Ã»Óп´Íê¡£

li-jiahuan ·¢±íÓÚ 2006-12-22 21:14

ÖØ·µubuÀ²£¿
д¸öÐĵÃ
·¢ÉÏubuntu/debian°æÀïßÂ

ÎÒÏÖÔÚ¶¨ÖÆubuntu edgy
ͬʱÔÚдÎĵµ
¹ýÒ»ÕóÌùÉÏÀ´

zhuomingliang ·¢±íÓÚ 2006-12-22 21:28

×Ô¼ºÃ»ÓÐÃþË÷ʲô¶«Î÷£¬Òò´ËûÓÐÐĵÃ:rolleyes:
ÏÖÔÚÀíÂÛÉÏÊÇÔÚlinuxÏ£¬Êµ¼ÊÔÚwindowsÏÂ
ubu ºÜ²»´íÁË£¬ff Ò²¿ìÁË£¬¿Éϧff ²»ÊǺÜÎȶ¨£¬ÓÚÊÇÐéÄâÁËÒ»¸ödeepxin xp£¬×ªÁ˸öȦȦ¡£:D

dearvoid ·¢±íÓÚ 2006-12-22 22:29

°³Ö»ÊÇÌṩµã¶ù˼·£¬²»ÊÇÍêÕûµÄ½â¾ö·½°¸ :)

oldwell_nirvana ·¢±íÓÚ 2006-12-23 10:28

·Ç³£¸Ðл¼¸Î»´ó´ó£¬ ÎÒÒªµÄ¾ÍÊÇ˼·¡£

¶ÔÁË£¬ »¹ÓÐÒ»¸öÎÊÌâÇë½Ì£º

Èç¹ûÎÒÓÃC ³ÌÐòµ÷ÓÃSHELL£¬ ÏëÒªµÃµ½SHELL ³ÌÐò·µ»ØµÄ±äÁ¿Öµ£¬ ¸ÃÔõô´¦Àí£¿

dearvoid ·¢±íÓÚ 2006-12-23 10:35

¿´¿´ system() ¸ú waitpid() Á½¸öº¯ÊýµÄ man page ¾ÍÇå³þÁË

dearvoid ·¢±íÓÚ 2006-12-23 10:37

[QUOTE=dearvoid]¿´¿´ system() ¸ú waitpid() Á½¸öº¯ÊýµÄ man page ¾ÍÇå³þÁË[/QUOTE]
°³Àí½â´íÁË :(

[COLOR=Magenta]SHELL ³ÌÐò·µ»ØµÄ±äÁ¿Öµ[/COLOR]ÔõôÀí½â?

oldwell_nirvana ·¢±íÓÚ 2006-12-23 10:48

¾ÙÀýÀ´Ëµ£º
result = `grep string file  | awk '{print $2}'` ÕâÌõSHELL ÃüÁî

ÎÒÔÚC³ÌÐòÖÐÊÇÕâôÓõģº
sprintf(cmd, "%s\n",  "grep string file  | awk '{print $2}'" ) ;
system(cmd);

ÎÒÏëÔÚC ³ÌÐòÖлñµÃ$result µÄÖµ£¬ ÓÐʲô°ì·¨£¿

li-jiahuan ·¢±íÓÚ 2006-12-23 11:01

µÃµÈdearvoid»Ø¸´ÁË
ÎÒÖ»»áÒ»µãBashºÍÒ»µãPerl
PerlÔÚÕÕÑùÒ²ÊÇÓà ``È¡·µ»ØÖµ
system()Ö»Êǵ÷Óò¢·µ»ØÍ˳ö״̬
[code]
huanlf@huanlf-desktop:~$ perl -e ' $a=`echo hello world`; print "$a\n" '
hello world

huanlf@huanlf-desktop:~$
[/code]
²»Á˽âC

ÁíÍâ
Èç¹ûÊDz»Í¬Ö÷Ìâ
ÕÕÀí¿ªÐÂÖ÷Ìâ·¢ÎÊ£º£©

dearvoid ·¢±íÓÚ 2006-12-23 11:27

[QUOTE=oldwell_nirvana]¾ÙÀýÀ´Ëµ£º
result = `grep string file  | awk '{print $2}'` ÕâÌõSHELL ÃüÁî

ÎÒÔÚC³ÌÐòÖÐÊÇÕâôÓõģº
sprintf(cmd, "%s\n",  "grep string file  | awk '{print $2}'" ) ;
system(cmd);

ÎÒÏëÔÚC ³ÌÐòÖлñµÃ$result µÄÖµ£¬ ÓÐʲô°ì·¨£¿[/QUOTE]
FYI:
There is no way for a child process to pass an environment variable back to its parent process. But you can get the output of a command. The "official" way to do that is to use the popen() function.

oldwell_nirvana ·¢±íÓÚ 2006-12-23 11:32

ͨ¹ýpopen() ºÍдÁÙʱÎļþ½»»¥ÊÇûÓÐÎÊÌâµÄ¡£
ÎÒÏëÒ²¿ÉÄÜÊÇûÓÐÕÐÁË£¬ лл´ó´ó£¡

dearvoid ·¢±íÓÚ 2006-12-23 11:37

[QUOTE=oldwell_nirvana]ͨ¹ýpopen() ºÍдÁÙʱÎļþ½»»¥ÊÇûÓÐÎÊÌâµÄ¡£
ÎÒÏëÒ²¿ÉÄÜÊÇûÓÐÕÐÁË£¬ лл´ó´ó£¡[/QUOTE]
Õâ¸öʱºò scripting languages µÄÓÅÊÆ¾ÍÏÔÏÖ³öÀ´ÁË :) ²»¹ý Python ºÃÏñҲûÓÐ built-in µÄÕâÖÖ feature, Ò²Ö»ÄÜÓà popen()

li-jiahuan ·¢±íÓÚ 2006-12-24 17:37

ºÙºÙ£¬Perl ...

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.