删除文本文件中的行(调用)

时间:2012-01-31 10:30:07

标签: linux bash shell awk

那些是通话时间,所以预计比任何通话超过2秒或小于0.5秒(大多数在0.800或1.900之间)我试图找到删除某些行的脚本时遇到问题,这里是一个例子:

06:28:30.259    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:28:54.191    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:28:55.596    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:29:19.251    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:29:20.042    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:29:20.566    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:29:42.900    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:29:44.268    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:30:08.146    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:30:09.530    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:30:31.925    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:30:33.228    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:30:56.178    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error

目标是获得这个结果:

06:28:54.191    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:28:55.596    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:29:19.251    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:29:20.566    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:29:42.900    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:29:44.268    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:30:08.146    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:30:09.530    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error
06:30:31.925    qchatmgr_pal.c  197     E       +++PTT Press    Call Manager/Error
06:30:33.228    *InCallForm.c   934     E       FloorStatus: Granted    Legacy/Error

评论:

第一条消息应始终为“+++ PTT Press”,最新消息应为“FloorStatus:Granted” 应该删除第5行,因为它没有意义,呼叫设置应该(实际上)在0.800毫秒到1.9秒之间,所以少于0.5xx毫秒不太现实,所以应该删除该行。

有人可以给我一些提示,请继续前进,我不希望有人解决这个问题,因为它有点棘手。我一直在使用我在这个网站上找到的其他一些脚本(bash),但它们不适合这个。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

有点难看(也许你想使用awk或perl):

#!/bin/bash
FILE=calls.txt

## remove first line if it contains "Granted"/last line "+++PTT"
head -n 1 ${FILE} | grep -q Granted && sed -i '1d' ${FILE}
tail -n 1 ${FILE} | grep -q +++PTT && sed -i '$d' ${FILE}

## remove duplicated Granted entries
tac ${FILE} > ${FILE}.tac
sed -i '$!N; /^.*\(FloorStatus\).*\n.*\1.*$/!P; D' ${FILE}.tac
tac ${FILE}.tac > ${FILE}

LASTTIME=0

while read line ; do
  ## is line empty?
  if [ ! -z "${line}" ] ; then

    ## fetch times
    T=$(echo ${line} | cut -d " " -f 1)
    echo ${line} | grep -q +++PTT
    P=$?
    echo ${line} | grep -q Granted
    G=$?

    ## create timestamp
    #TSTAMP=$(date -d "${T}" +'%s%N')
    H=$(expr $(echo ${T} | cut -d : -f 1) \* 3600)
    M=$(expr $(echo ${T} | cut -d : -f 2) \* 60)
    S=$(echo ${T} | cut -d : -f 3 | sed 's#\.##g')
    TSTAMP=$(expr ${H} \+ ${M})
    TSTAMP=$(expr ${TSTAMP} \+ ${S})

    ## calculate diff
    D=$(expr ${TSTAMP} \- ${LASTTIME})

    ## less than threshold?
    #if [ ${D} -lt 800000000 ] ; then
    if [ ${D} -lt 800 -a ${P} -ne 0 ] ; then
      ## remove current call
      sed -i '/^'${T}'/d' ${FILE}
    fi
    LASTTIME=${TSTAMP}
  fi
done < ${FILE}

## remove duplicated +++PTT entries
tac ${FILE} > ${FILE}.tac
sed -i '$!N; /^.*\(+++PTT\).*\n.*\1.*$/!P; D' ${FILE}.tac
tac ${FILE}.tac > ${FILE}

答案 1 :(得分:0)

你可以使用这样的代码:

    while read FirstField SecondField ThirdField ForthFiled FifthField SeventhField
    do
    #you have your Values of a line in this seven variables

    done < yourfilename

此文件将读取您的所有文件并保存FirstField中的时间值和SecondField等中的* InCallForm.c中的第二个字段。 所以你可以在这个while循环中用你的文件做所有事情。 如果你不明白我可以告诉更多...