Consecutive count and reset in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Consecutive count and reset in awk
# 1  
Old 03-26-2017
Consecutive count and reset in awk

I have the following file:
Code:
A1 4.5807 6.4202
B1 2.5704 11.4414
C1 5.5607 5.28872
D1 3.5807 8.2132
E1 3.2206 9.13153
F1 3.0907 9.51532
G1 3.2707 8.99165
H1 2.4607 11.9515
A2 2.5505 11.5307
B2 2.3106 12.7279
C2 3.8507 7.63731
D2 2.6208 11.2214
E2 2.7609 10.652
F2 2.0604 14.2734
G2 2.6908 10.9295
H2 5.0007 5.88098
A3 2.9506 9.96713
B3 4.2505 6.91895
C3 4.5107 6.51983

And I am using the following script to output the NR and some other stuff:
Code:
gawk '{print ++k, "Source", $1, $2, $3, "Destination", 1}' C:\cygwin64\User\Output4

I, however, need to add an extra column (first column in my example below) listing the consecutive count in increments of 12. After 8 steps I should reset the count to the previous first value+1 .
The output file should look like this:
Code:
1	1	Source	A1	4.5807	6.4202	Destination	1
13	2	Source	B1	2.5704	11.4414	Destination	1
25	3	Source	C1	5.5607	5.28872	Destination	1
37	4	Source	D1	3.5807	8.2132	Destination	1
49	5	Source	E1	3.2206	9.13153	Destination	1
61	6	Source	F1	3.0907	9.51532	Destination	1
73	7	Source	G1	3.2707	8.99165	Destination	1
85	8	Source	H1	2.4607	11.9515	Destination	1
2	9	Source	A2	2.5505	11.5307	Destination	1
14	10	Source	B2	2.3106	12.7279	Destination	1
26	11	Source	C2	3.8507	7.63731	Destination	1
38	12	Source	D2	2.6208	11.2214	Destination	1
50	13	Source	E2	2.7609	10.652	Destination	1
62	14	Source	F2	2.0604	14.2734	Destination	1
74	15	Source	G2	2.6908	10.9295	Destination	1
86	16	Source	H2	5.0007	5.88098	Destination	1
3	17	Source	A3	2.9506	9.96713	Destination	1
15	18	Source	B3	4.2505	6.91895	Destination	1
27	19	Source	C3	4.5107	6.51983	Destination	1

Any help will be greatly appreciated

Last edited by Xterra; 03-26-2017 at 02:19 PM..
# 2  
Old 03-26-2017
Try
Code:
awk '{print NR*12 - 11  - int((NR-1)/8)*95,  ++k, "Source", $1, $2, $3, "Destination", 1}' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-26-2017
Awesome! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Reset MPxIO total path count

We recently moved our netapp disks onto new infrastructure by attaching the new controllers to our fibre switch and netapp migrating the disks. Disks kept the same address. We originally had 4 paths, but after attaching the new controllers we had 8. Now I want to remove the old paths. But can't. ... (5 Replies)
Discussion started by: garydeena
5 Replies

2. Shell Programming and Scripting

Count consecutive characters

Need to count consecutive characters in a string and give the output as below i/p=aaaabbcaa o/p=a4b2c1a2 (10 Replies)
Discussion started by: prasanna2166
10 Replies

3. Shell Programming and Scripting

Bash only count consecutive days

I was wondering how I would go around to do this. This is an example of my output Sun Aug 21 2016 03:00:00, BLAH Mon Aug 22 2016 03:54:00, BLAH Tue Aug 23 2016 04:22:11, BLAH Thu Aug 25 2016 05:00:00, BLAH Now what I would like to do is only count CONSECUTIVE days so in the... (1 Reply)
Discussion started by: rodriguesm
1 Replies

4. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

5. Shell Programming and Scripting

select entry from consecutive line awk

Hi folks, I have a file with four columns that looks like the following (tab separated) 1 1 1 a 2 2 2 b 3 3 3 c 4 4 4 d I would like to create a file with always 4 columns but where the third entry corresponds to the thirst entry of the next line and so on, to get the following 1 1 2 a... (4 Replies)
Discussion started by: klebsiella
4 Replies

6. Solaris

Reset failed login count

Hi, Can someone tell me the command to do this in solaris 5.10 please? I've trawled around the internet for ages but all I can find is the AIX command... Thanks (2 Replies)
Discussion started by: Grueben
2 Replies

7. Shell Programming and Scripting

extracting multiple consecutive columns using awk

Hello, I have a matrix 200*10,000 and I need to extract the columns between 40 and 77. I dont want to write in awk all the columns. eg: awk '{print $40, $41, $42,$43 ... $77}'. I think should exist a better way to do this. (10 Replies)
Discussion started by: auratus42
10 Replies

8. Shell Programming and Scripting

Count the Consecutive Occurance of "X" in awk

Hi All, I have a data as follow: 0 0 0 X X 0 X X X 0 X 0 0 X 0 0 (16 Replies)
Discussion started by: nica
16 Replies

9. Shell Programming and Scripting

consecutive row comparison in awk (?)

hi, I'm totally new to this forum and to awk. I have what I thought was a simple problem, but I can't get anything to work. Here is an example input file: 3.85 4018.4 3.9 4068.4 3.95 4082.9 4 4099.7 # Property:.......etc. 0 4733.3 0.05 4659.7 0.1 4585.6 0.15 4466.2 Two... (2 Replies)
Discussion started by: ogga
2 Replies

10. Shell Programming and Scripting

need help cutting consecutive lines with sed or awk

HI All, I want to cut 5 lines after a pattern using sed or awk. can any one tell me how to do it ? (2 Replies)
Discussion started by: raghin
2 Replies
Login or Register to Ask a Question