简体   繁体   中英

Remove lines in a text file (Calls)

Those are calls times, so is expected than any call takes more than 2 secs or less than 0.5 secs(mostly is between 0.800 or 1.900 max.)I have a problem trying to find a script to delete some lines, here is an example:

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

The goal is obtain this result:

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

Comments:

The first message should be "+++PTT Press" always and the latest should be "FloorStatus: Granted" Line 5 should be deleted because it does not have sense, a call setup should be (really) between 0.800 ms to 1.9 secs so less then 0.5xx ms is not so realistic so that line should be removed.

Could someone give me some tips please to move forward, I dont expect somebody solve this because it's a bit tricky. I've been working using some other scripts (bash) which I found here in this website but they dont fit to this.

Thank you in advance.

It is a little bit ugly (maybe you want to use awk or 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}

you can use a code like this:

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

    done < yourfilename

this file will read all your file and save values of time in FirstField and the second field like *InCallForm.c in the SecondField and etc. so you can do every thing with your file in this while loop. if you didn't understand I can tell more...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM