如何从文本中抓去 字符串

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

如何从文本中抓去 字符串

#1

帖子 543082593 » 2009-03-02 8:53

本人ubuntu 有的时候 某些程序会出现”假死“的现象 于是 这些命令就很常用
ps -A | grep ...
kill -9 processID
于是 为了 方便(也不想用那个gnome-system-monitor) 所以就写了一个很简单的shell脚本 如下:

#! /bin/bash
echo "this script is to kill the process that seems dead"
echo "input the name of the process!(when you don't know the exact name you can do it like this [rhythmbox ==> rhy]"
read processname
ps -A | grep $processname > .proctmp
kill -9 `cut .proctmp -d " " -f 2`
echo " the choose process `cut .proctmp -d " " -f 8` has been killed"
echo " goodbye"

想必 大家看出来了 ps -A | grep $processname > .proctmp 和 cut .proctmp -d " " -f 8 我是想得到 进程的名字
比如 我首先打开stardict进程 然后 终端中执行以上脚本
就可以得到 很好的效果 the choose process stardict has been killed
但是 某些进程 最后却不能正确的显示名字 也就是 cut .proctmp -d " " -f 8 不能得到正确的结果
那从文本中截取字符串 应该用什么命令呢
fall again
smooth criminal
they don't care about us
billie jean
beat it
dangerous
the lost children
childhood
ben
i will be there
speechless
she is out of my life
rock with you
...
LOVE U FOREVER
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 如何从文本中抓去 字符串

#2

帖子 eexpress » 2009-03-02 9:13

pgrep -l啊。
cut -f 2
被大字,看着累。
● 鸣学
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 如何从文本中抓去 字符串

#3

帖子 HuntXu » 2009-03-02 9:18

代码: 全选

[hunt@bubble ~]$ O_< ps -A|grep urxvt|awk '{print $4}'
urxvt
HUNT Unfortunately No Talent...
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 如何从文本中抓去 字符串

#4

帖子 HuntXu » 2009-03-02 9:20

killall算了...
HUNT Unfortunately No Talent...
头像
543082593
帖子: 234
注册时间: 2008-11-07 8:41

Re: 如何从文本中抓去 字符串

#5

帖子 543082593 » 2009-03-02 9:21

eexpress 写了:pgrep -l啊。
cut -f 2
被大字,看着累。
恩 恩
下次 小点字 主要是 字小了 感觉 不醒目
呵呵 不过谢谢你
问题解决了
fall again
smooth criminal
they don't care about us
billie jean
beat it
dangerous
the lost children
childhood
ben
i will be there
speechless
she is out of my life
rock with you
...
LOVE U FOREVER
回复