basickarl
basickarl

Reputation: 40464

Get match in text file and pipe to new file

I have a bash question with grep (or awk). I'm trying to get every single instance of the Delay in the line of TOTAL. So in the case below, it would be 5 instances of 0. These instances matched are to also be piped to a new file where each instance shall be on it's own new line.

Timestamp         Stream    Packets   Losses Misorder       Rate    Delay
17/02/01.10:58:26 stream_0      625        0        0    5.00  M  38473.2 F
17/02/01.10:58:26 TOTAL                           625        0        0    5.00  M
-------------------------------------------------------------------------------------
17/02/01.10:58:27 stream_0      625        0        0    5.00  M  38473.2
17/02/01.10:58:27 TOTAL                           625        0        0    5.00  M
-------------------------------------------------------------------------------------
17/02/01.10:58:28 stream_0      625        0        0    5.00  M  38473.2 F
17/02/01.10:58:28 TOTAL                           625        0        0    5.00  M
-------------------------------------------------------------------------------------
17/02/01.10:58:29 stream_0      625        0        0    5.00  M  38473.2
17/02/01.10:58:29 TOTAL                           625        0        0    5.00  M
-------------------------------------------------------------------------------------
17/02/01.10:58:30 stream_0      625        0        0    5.00  M  38473.3
17/02/01.10:58:30 TOTAL                           625        0        0    5.00  M
-------------------------------------------------------------------------------------

So I'm looking for the following match (denoded with <-this):

Timestamp         Stream    Packets   Losses Misorder       Rate    Delay
17/02/01.10:58:26 stream_0      625        0        0    5.00  M  38473.2 F
17/02/01.10:58:26 TOTAL                           625        0        0(<-this) 5.00  M
-------------------------------------------------------------------------------------

Example of output file (filename doesn't matter):

0
0
0
0
0

I'm a JavaScript programmer, but I'm only permitted to do this via bash!

Upvotes: 1

Views: 66

Answers (2)

dawg
dawg

Reputation: 103824

You can do an all Bash solution as well:

regex="TOTAL[[:blank:]]+([[:digit:]]+)[[:blank:]]+([[:digit:]]+)[[:blank:]]+([[:digit:]]+)"
while IFS= read -r line || [[ -n "$line" ]]; do
    if [[ "$line" =~ $regex ]]
    then
        echo "${BASH_REMATCH[3]}"
    fi  
    done <file >output_file 

Or, just match TOTAL, break the fields on spaces by putting into an array, and print the fourth field:

while IFS= read -r line || [[ -n "$line" ]]; do
    if [[ "$line" =~ TOTAL ]]
    then
        arr=($line)
        echo "${arr[4]}"
    fi  
    done <file >output_file 

Upvotes: 0

janos
janos

Reputation: 124646

With awk, you can filter by lines that contain TOTAL, and print the 5th column:

awk '/TOTAL/ { print $5 }' > file.txt

Upvotes: 1

Related Questions