Reseting row count every given number of rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reseting row count every given number of rows
# 1  
Old 01-10-2019
Reseting row count every given number of rows

I have a file with 48 rows. I am counting 6 rows and adding 6 to that number and repeating the operation, and then output the value in column 1. For the second column, I would like to get sort of a binary output (1s and 2s) every 3rd row. This is what I have:
Code:
 awk '{print ++src + (int((++count-1)/6)*6), (int(CNT++/3)+1)%2+1}' test.txt

And I am getting this:
Code:
1 2
 2 2
 3 2
 4 1
 5 1
 6 1
 13 2
 14 2
 15 2
 16 1
 17 1
 18 1
 25 2
 26 2
 27 2
 28 1
 29 1
 30 1
 37 2
 38 2
 39 2
 40 1
 41 1
 42 1
 49 2
 50 2
 51 2
 52 1
 53 1
 54 1
 61 2
 62 2
 63 2
 64 1
 65 1
 66 1
 73 2
 74 2
 75 2
 76 1
 77 1
 78 1
 85 2
 86 2
 87 2
 88 1
 89 1
 90 1

But the desired output is this:
Code:
1 1
 2 1
 3 1
 4 2
 5 2
 6 2
 13 1
 14 1
 15 1
 16 2
 17 2
 18 2
 25 1
 26 1
 27 1
 28 2
 29 2
 30 2
 37 1
 38 1
 39 1
 40 2
 41 2
 42 2
 49 1
 50 1
 51 1
 52 2
 53 2
 54 2
 61 1
 62 1
 63 1
 64 2
 65 2
 66 2
 73 1
 74 1
 75 1
 76 2
 77 2
 78 2
 85 1
 86 1
 87 1
 88 2
 89 2
 90 2

I am sort of getting the desired output but I can only thing there are better ways to accomplish this task.
Any help will be appreciated.
# 2  
Old 01-10-2019
Try this instead:
Code:
awk '{print NR + 6 * int((NR - 1) / 6), int(((NR - 1) % 6) / 3) + 1}' test.txt

If you're running this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk. (Please get into the habit of telling us what operating system and shell you're using every time you start a new thread!)

The above code does not put the leading <space> on the last 47 rows of your output, but it otherwise matches the output you said you want. If you really want the leading <space> in the output, except on the first output line, I will leave adding that <space> to the output as an exercise for the reader.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

I want count of number of records to be printed on each row.

we want the count of number of records to be printed on each row. For Ex: if there are 5 records on one unique id , the count "5'' should be printed on each record in other column. Please help for this. I am using unix & Cygwin. Below are sample records: KCZ0650473... (2 Replies)
Discussion started by: ElijaRajesh
2 Replies

2. UNIX for Dummies Questions & Answers

Script to count number of rows

Hi, I need a solaris shell script to read multiple files and count number of unique name rows(strings) from those files. The input and output should be like this Input: file 1 abc cde abc ... (9 Replies)
Discussion started by: ssk250
9 Replies

3. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

4. Shell Programming and Scripting

Extract and count number of Duplicate rows

Hi All, I need to extract duplicate rows from a file and write these bad records into another file. And need to have a count of these bad records. i have a command awk ' {s++} END { for(i in s) { if(s>1) { print i } } }' ${TMP_DUPE_RECS}>>${TMP_BAD_DATA_DUPE_RECS}... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

5. Shell Programming and Scripting

Count the number or row with same value in a column

This is the source file, we called it errorlist.out 196 server_a server_unix_2 CD 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd server_unix_2 CD 196 server_d server_unix_2 CD 196 server_es server_win_1 CD 196 ... (14 Replies)
Discussion started by: sQew
14 Replies

6. UNIX for Dummies Questions & Answers

count number of rows based on other column values

Could anybody help with this? I have input below ..... david,39 david,39 emelie,40 clarissa,22 bob,42 bob,42 tim,32 bob,39 david,38 emelie,47 what i want to do is count how many names there are with different ages, so output would be like this .... david,2 emelie,2 clarissa,1... (3 Replies)
Discussion started by: itsme999
3 Replies

7. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

8. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

9. Shell Programming and Scripting

count number of nonempty columns in row

Hi, Suppose i have a inputfile in csv format. How to use awk to count 'the number of nonempty columns in each row' minus one, and add the value as a new column in the end For cosmetic reason, it's even better to include a descriptive label for the last column in the first row. for... (2 Replies)
Discussion started by: grossgermany
2 Replies

10. UNIX for Dummies Questions & Answers

row count but only number part

hi i am pretty new to unix .i am ETL guy I need a unix script to take row count of a file and write it to another file the problem with wc-l is it include filename also wc -l abc.dat will give me like 1000 abc.dat i just want 1000 to be written can u just take 2 min to write a simple... (1 Reply)
Discussion started by: er_zeeshan05
1 Replies
Login or Register to Ask a Question