awk script to filter the numbers which are around the set value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to filter the numbers which are around the set value
# 1  
Old 08-09-2012
awk script to filter the numbers which are around the set value

Hi All,

I have one sensor output(over the same) for a set value of 20.
Code:
Time(in Sec), Data
1,   16
2,   20
3,   24
4,   22
5,   21
6,   20
7,   19.5
8,   20
9,   20.5
10, 20
11, 20
12, 19.5

Here we can see like after 5 sec of time the data value reaches to 20+-0.5 range.
So I want a script to print the values (my output)
Code:
6,   20
7,   19.5
8,   20
9,   20.5
10, 20
11, 20
12, 19.5

in this case.
The strict requirement here is we should skip the data data point 20 that comes after 2sec. (Sometimes even multiple entries of 20 followed by some value which is not in the rnage of 20+-0.5.

I tried the script
Code:
awk -F, '$2<=20.5 && $2 >=19.5' input.csv

but this doesn't give my required output.

Thanks
Sidda
# 2  
Old 08-09-2012
What is your required output?
That awk script will give you the output listed.
# 3  
Old 08-09-2012
Required output is not coming with the mentioned awk script by me.

Hi Elixir,

The required output is different than what we get from

Code:
awk -F, '$2<=20.5 && $2 >=19.5' input.csv

I request you to look into the output once more.
I don't need the second line
2, 20
in my output.
Basically I need the last portion of the data column where it is falling within my setvalue. If there are any transients in between before the data settles down we need to ignore those entries.
I hope I made much clear now.

Regards
Sidda

Last edited by Franklin52; 08-09-2012 at 07:24 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 08-09-2012
OK. Sorry for being a dumbo Smilie
Code:
awk -F, '{
if($2>=19.5 && $2<=20.5)
 all=all?(all RS $0):$0
else
 all=""} END{print all}' infile

# 5  
Old 08-09-2012
Code:
awk -F"," 'BEGIN{i=1}{if($2<=20.5 && $2 >=19.5){a[i]=$0;i++}else{i=1}}END{for(j=1;j<i;j++){print a[j]}}' infile

# 6  
Old 08-09-2012
I did not get any output..

Hi Elixir,

Thanks a lot for replying back with the code.
But when I run this code I did not get any output.
I am unable to figure out the logic on why no output at all.
Can you please check the logic again ?

Regards
Sidda
# 7  
Old 08-09-2012
i had tested this on your data and i got the below output
Code:
6,   20
7,   19.5
8,   20
9,   20.5
10, 20
11, 20
12, 19.5

logic is i am storing the value in an array for values in the range 20+-0.5 if the values does not lie in the range i am resetting the array index i=1.
so in the end i will get last set of values in the required range
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter and delete numbers in a list

Hello, I've got a list of a single column numbers like 3000.66 3002.55 3062.23 3242.12 etc... I would like to delete all numbers higher than for example 3060.00 and lower than 2990.00 How can I do that? Thanks in advance (2 Replies)
Discussion started by: Board27
2 Replies

2. Shell Programming and Scripting

awk - set numbers [ 1 ... n] from the 6 line

Hi, i have a file, where measurement-data is stored in the first column. The file has also a header of 5 lines. I want to set counting up numbers in front of any particular measurement-value; should start at the 6. line with starting number 1. i try to solve it with ... awk 'NR > 6 { print... (6 Replies)
Discussion started by: IMPe
6 Replies

3. Shell Programming and Scripting

awk : Filter a set of data to parse header line and last field of multiple same match.

Hi Experts, I have a data with multiple entry , I want to filter PKG= & the last column "00060110" or "00088150" in the output file: ############################################################################################### PKG= P8SDB :: VGS = vgP8SOra vgP8SDB1 vgP8S001... (5 Replies)
Discussion started by: rveri
5 Replies

4. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

5. Shell Programming and Scripting

How set filter netstat -an | grep -P '\:'38''

Hi, I can write sh script for Linux platform I run: netstat -an | grep -P '\:'38''| grep ESTABLISHED but result: # netstat -an | grep -P '\:'38''| grep ESTABLISHED tcp 0 0 172.16.1.107:383 172.16.1.81:49981 ESTABLISHED tcp 0 0... (8 Replies)
Discussion started by: ostapv
8 Replies

6. Shell Programming and Scripting

Compute the median of a set of numbers with AWK?

Is there a way in awk to compute the median of a set of numbers in a file in the following format. 34 67 78 100 23 45 67 (3 Replies)
Discussion started by: Lucky Ali
3 Replies

7. Shell Programming and Scripting

awk filter numbers inbetween

Hi, I have a file which has a number in each line ( i think they are strings ) I will have a $first and $last variable, which are strings but contains only numbers. Also a file $f, I want to filter out the lines in $f with only numbers in between $first and $last. Do I need to consider the... (2 Replies)
Discussion started by: a27wang
2 Replies

8. Programming

Libpcap: Set a filter. (C)

Hi I'm reading about the libpcap documentation. I see that with the function: pcap_setfilter ( pcap_t *p, struct bpf_program *fp) we can choose to take one packet instead another. I read that the struct bpf_program allow us to create a packet filter , ....so if I want just sample some... (2 Replies)
Discussion started by: Dedalus
2 Replies

9. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question