简体   繁体   中英

awk range in file from shell input

I have a file containing some output as such:

1395943855344,187,HTTP Request,200,OK,TID Requests 1-1,text,true,6730,184
1395943855546,17,HTTP Request,200,OK,TID Requests 1-1,text,true,517,17
1395943855565,131,HTTP Request,200,OK,TID Requests 1-1,text,true,7453,128
1395943855703,16,HTTP Request,200,OK,TID Requests 1-1,text,true,517,16
1395943855723,140,HTTP Request,200,OK,TID Requests 1-1,text,true,7629,137
1395943855782,204,HTTP Request,200,OK,TID Requests 1-2,text,true,8160,200
1395943855870,15,HTTP Request,200,OK,TID Requests 1-1,text,true,518,15
1395943855890,147,HTTP Request,200,OK,TID Requests 1-1,text,true,8178,143
1395943856003,17,HTTP Request,200,OK,TID Requests 1-2,text,true,516,17
1395943856025,152,HTTP Request,200,OK,TID Requests 1-2,text,true,8113,148
1395943856044,18,HTTP Request,200,OK,TID Requests 1-1,text,true,518,18
1395943856068,126,HTTP Request,200,OK,TID Requests 1-1,text,true,7961,122
1395943856175,179,HTTP Request,200,OK,TID Requests 1-3,text,true,7901,175
1395943879200,79,HTTP Request,200,OK,TID requests 2-57,text,true,226,79
1395943879201,100,HTTP Request,200,OK,TID requests 2-89,text,true,226,100
1395943879201,27,HTTP Request,200,OK,TID requests 5-135,text,true,226,27
1395943879201,289,HTTP Request,200,OK,TID requests 4-9,text,true,226,289
1395943879201,294,HTTP Request,200,OK,TID requests 4-138,text,true,226,294
1395943879201,367,HTTP Request,200,OK,TID requests 2-17,text,true,226,367
1395943879201,369,HTTP Request200,OK,TID Requests 1-107,text,true,7781,301
1395943879201,86,HTTP Request,200,OK,TID requests 3-91,text,true,226,86
1395943879201,86,HTTP Request,200,OK,TID requests 4-26,text,true,226,86
1395943879202,122,HTTP Request,200,OK,TID requests 2-99,text,true,226,122
1395943879202,125,HTTP Request,200,OK,TID requests 4-10,text,true,226,125
1395943979202,312,HTTP Request,200,OK,TID requests 3-86,text,true,226,312
1395943979202,376,HTTP Request,200,OK,TID requests 2-49,text,true,226,376
1395943979202,86,HTTP Request,200,OK,TID requests 2-126,text,true,226,86
1395943979202,89,HTTP Request,200,OK,TID requests 3-75,text,true,226,89
1395943979202,94,HTTP Request,200,OK,TID requests 2-149,text,true,226,94
1395943979203,129,HTTP Request,200,OK,TID requests 2-45,text,true,226,129
1395943979203,134,HTTP Request,200,OK,TID requests 2-26,text,true,226,134
1395943979203,37,HTTP Request,200,OK,TID requests 2-51,text,true,226,37
1395943979203,89,HTTP Request,200,OK,TID requests 2-55,text,true,226,89
1395943979204,123,HTTP Request,200,OK,TID requests 2-85,text,true,226,123
1395943979204,93,HTTP Request,200,OK,TID requests 2-53,text,true,226,93

I am trying to grab a range of lines from 1395943855 to 1395943879. I can do that in from the command line by issuing:

awk '/1395943855[0-9]+/,/1395943879[0-9]+/' input_file.txt

I would like to do that programmaticly from a shell or bash script. Where 1395943879 and 1395943879 are variables fed into the script.

I have tried this but it didn't work and am not sure why.

echo 1395943855 1395943879 | awk -v v1=$1 -v v2=$2 '/v1[0-9]+/,/v2[0-9]+/' input_file.txt

v1 is not special in an awk /regex/ command. Also, $1 refers to script parameters and not anything written to stdin.

Rather than trying to compose and match regex, how about just printing lines based on whether the number is in some range?

start=1395943855000
end=1395943879999
awk -F, -v "from=$start" -v "to=$end" '$1 >= from && $1 <= to' input_file.txt

If the input file is sorted on the first column we could avoid some comparisons as follows:

start=1395943855000
end=1395943879999
awk -F, -v start=$start -v end=$end '$1 < start { next } $1 > end { exit } { print }' input_file.txt

If you do not want to include the lines starting with 1395943879:

awk -v from=1395943855 -v to=1395943879 '
    $1 ~ "^"from {p=1} 
    $1 ~ "^"to   {exit} 
    p
' file

If you DO want to include them:

awk -v from=1395943855 -v to=1395943879 '
    $1 ~ "^"from {p=1} 
    $1 ~ "^"to   {end=$1} 
    end && $1 != end {exit} 
    p
' file

To put that into a script:

#!/bin/sh
awk -v from="$1" -v to="$2" '
    $1 ~ "^"from {p=1} 
    $1 ~ "^"to   {end=$1} 
    end && $1 != end {exit} 
    p
' file

and invoke it like: ./script.sh 1395943855 1395943879

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