×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

test column values

test column values

(OP)
I've file with 2 columns
Cla Clb
5301 1922
5301 1923
5301 1925
5311 2020
5311 2021
5311 2022
5321 1880
5321 1885
5321 1886
5321 1887
... ...
I need to find cumul of diffrence values upper than 1 on column b with the same value of column a
if value of Cla is the same ex: 5301 do (1925-1923=2),(1923-1922=1) : cumul difference > 1 is equal 1 so cumul1=1
second value of Cla 5311 do (2022-2021=1), (2021-2020=1) : cumul difference > 1 is 0 so cumul2=0
third value of Cla 5321 (1887-1886=1), (1886-1885=1); (1885-1880=5) cumul difference > 1 is 1 so cumul3=1
at the end I'll calculate cumul1+cumul2+cumul3+...cumuln
using bash script or awk
attached wall file test

RE: test column values

Hi

So if there is at least one case when the difference between consecutive Clb values is greater than 1, then the cumul of the given Cla group is 1, otherwise 0 ?

CODE --> Awk

NR == 1 { next }
last[$1] && ! cumul[$1] { cumul[$1] =$2 - last[$1] > 1 } { last[$1] = $2 } END { for (i in cumul) printf "cumul %s = %s\n", i, cumul[i] }  Feherke. feherke.ga RE: test column values (OP) thanks for your prompt response the programm work but it do not the cumul like I need. in my example: 5301 1922 5301 1923 5301 1925 5301 1927 5311 2020 5311 2021 5311 2022 5311 2024 5321 1880 5321 1885 5321 1886 5321 1887 ze test : for$1=5301 we have (1923-1922=1); (1925-1923)=2; (1927-1925=2) cumul difference must be >1 cumul1= 0+2-1+2-1=2
for $1=5311 we have (2021-2020=0);(2022-2021=0);(2024-2022=2) cumul difference > 1 cumul2= 0+0+2-1=1 for$1=5321 we have (1887-1886=1);(1886-1885=1);(1885-1880=5) cumul difference > 1 cumul3= 0+0+5-1=4
the program do just one operation of difference
so
for a value of Cla we do val(n)-val(n-1)=a1; val(n-1)-val(n-2)=a2;...... and the cumul is if diff > 1 a1-1+a2-1+a3-1+.....
thanks a lot
execuse me for my longue explanation

RE: test column values

Hi

Not really sure as I can not find the relation between your "we have" values and "cumulX=" values, but maybe this :

CODE --> Awk

NR == 1 { next }
last[$1] { diff =$2 - last[$1]; if (diff > 1) cumul[$1] += diff - 1 }
{ last[$1] =$2 }
END { for (i in cumul) printf "cumul %s = %s\n", i, cumul[i] } 

Feherke.
feherke.ga

RE: test column values

(OP)
I've tested your programm it work fine I'll test it on the entire file. I'll write to you about result
good luck

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!