2015-11-07 63 views
1

我有一个dumpsys命令的输出,我想提取两种模式之间的线,唤醒锁&换行符。我得到这些线后,我需要一个特定的字符串。唤醒和锁出现在输出不止一次,而是唤醒锁定:大小只有一次,所以第一个模式可以成为"Wake*Locks*size"Bash获取两种模式之间的线和编辑它们

输出看起来是这样的(那里有上述许多线和低于此)

Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
WAKELOCK 'Term' (uid=35647, pid=362505, ws=null) 
#White space 

我想要“Wake Locks:size = *”和下一个换行符之间的连线,格式是我可以从中提取pid。也许是这样的:

for i in `dumpsys power | #Some way to get the desired lines#`; do 
Temp=`echo $i | awk '{print $4}'` 
$Temp #Assign var pid = number 
kill -9 $pid 
done 

UPDATE

更新的代码:

dumpsys power | 
sed -n '/Wake Locks: size=/,/^$/ p' | 
sed -e 's/.*pid=\([0-9]*\).*/\1/' | 
while read pid; do 
    kill -9 ${pid} 
done 
+0

有三个的PID你展示线。你想要哪一个?或者,你想要所有? – John1024

+0

每行有一个pid,所以他们都是 – BonBon

回答

1

当你没有在第一行,用sed会做一个匹配(你可先检查第一行)。
首先删除Wake Locks(以及该行)之前的所有行,继续删除剩余流中第一个空行的所有行,并且在您已经解析的情况下,获取pid(使用(\\)您会记住字符串,其值与\1一起打印)。

dumpsys power | sed -e '1,/Wake Locks: size=3/ d' \ 
        -e '/^$/,$ d' \ 
        -e 's/.*pid=\([0-9]*\).*/\1/' | while read pid; do 
    echo "You want to kill ${pid}" 
done 

编辑:
对不起,sed的可与-n标志一起使用:sed -n '/Wake Locks: size=3/,/^$/p'
所以,你可以将代码重写到更自然的

dumpsys power | 
    sed -n '/Wake Locks: size=3/,/^$/ p' | 
    sed -e 's/.*pid=\([0-9]*\).*/\1/' | 
    while read pid; do 
     echo "You want to kill ${pid}" 
    done 
+0

这是完美的!感谢你的回答 – BonBon

2

awk来救援!

$ awk '/Wake Locks: size=/{f=1} !NF{f=0} f' file 

会给你

Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
WAKELOCK 'Term' (uid=35647, pid=362505, ws=null) 

,如果你只需要PID值(有三个,其中之一?)你可以试试这个

awk -v FS="[ ,]" 'f{for(i=1;i<=NF;i++) 
         if($i~/pid=/) { 
          sub(/pid=/,"",$i);print $i 
         } 
        } 
/Wake Locks: size=/{f=1} 
       !NF{f=0}' file  
364345 
323345 
362505 

说明:设置标志f与空白行(NF:字段数= 0)时的模式和未设置。如果设置了标志,则在字段上迭代,当匹配的模式删除该模式以离开搜索到的数字时)。将字段分隔符设置为空格和逗号以从具有默认分隔符的数字中删除后缀逗号。您可以使用while read pid; do ... done < <(awk ...)中的结果或管道连接到可处理多行输入的其他脚本。

+0

所以我必须将输出写入文件并从那里继续,或者在这里进行管道工作?你也可以为你的答案添加解释,供将来参考。 – BonBon

0

如果我明白你希望正确地提取线,也可以实现使用bash内建(参数扩展/子串提取)相同:

#!/bin/bash 

## read from "$1" or stdin 
[ -r "$1" ] && infile="$1" || infile=/dev/stdin 

## check line beginnings for each line 
while read -r line; do 
    [ "${line%%:*}" = "Wake Locks" -o \ 
    "${line%% *}" = "WAKELOCK_NAME" -o \ 
    "${line%% *}" = "ANOTHER_WAKELOCK" -o \ 
    "${line%% *}" = "WAKELOCK_NAME" ] && echo "$line" 
done < "$infile" 

exit 0 

或者用grep可以使用文件包含图形你想在你的大文件中匹配并致电grep -f matchfile largefile。在这种情况下的matchfile可能看起来像:

grep的模式文件

$ cat dat/wakematch.txt 
^Wake Locks: 
^WAKELOCK_NAME 
^ANOTHER_WAKELOCK 
^WAKELOCK_NAME 

测试输入

$ cat dat/wake.txt 
junk and more junk 

Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
WAKELOCK 'Term' (uid=35647, pid=362505, ws=null) 
#White space 

junk and more junk 

Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
WAKELOCK 'Term' (uid=35647, pid=362505, ws=null) 
#White space 

使用/输出 - bash的参数扩展/子串提取

$ bash wakelock.sh dat/wake.txt 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 

$ bash wakelock.sh <dat/wake.txt 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 

使用grep

$ grep -f dat/wakematch.txt dat/wake.txt 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
Wake Locks: size=3 
WAKELOCK_NAME 'Term' (uid=93847, pid=364345, ws=null) 
ANOTHER_WAKELOCK 'Term' (uid=9190247, pid=323345, ws=null) 
0

使用GAWK

gawk '{if($0~/Wake Locks:/){while(!($0~/^\n$/) && getline >=1){print gensub(/.*pid=([0-9]+).*/,"\\1",$0);}}};' 

输出

364345 
323345 
362505 
相关问题