Reseting row count every given number of rows

Tags
count, number, row count, rows, shell scripts

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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 1 Week Ago
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:
Neo (1 Week Ago)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Splitting single row into multiple rows based on for every 10 digits of last field of the row kotra Shell Programming and Scripting 4 10-14-2016 05:59 PM
I want count of number of records to be printed on each row. ElijaRajesh UNIX for Dummies Questions & Answers 2 11-02-2015 04:43 AM
Get row number from file1 and print that row of file2 Abhiraj Singh Shell Programming and Scripting 6 02-27-2014 03:15 PM
Script to count number of rows ssk250 UNIX for Dummies Questions & Answers 9 02-20-2014 01:52 AM
Awk/sed script for transposing any number of rows with header row tntelle Shell Programming and Scripting 3 04-24-2013 06:01 PM
Finding row number along with length of row princetd001 UNIX for Dummies Questions & Answers 8 03-22-2013 06:28 AM
Extract and count number of Duplicate rows Arun Mishra Shell Programming and Scripting 5 03-08-2013 11:53 PM
The difference between end number in the early row and the start number in the next anjas Shell Programming and Scripting 2 07-30-2012 05:52 AM
Count the number or row with same value in a column sQew Shell Programming and Scripting 14 12-15-2010 08:07 PM
count number of rows based on other column values itsme999 UNIX for Dummies Questions & Answers 3 08-29-2010 06:11 PM
how to add the number of row and count number of rows juelillo Shell Programming and Scripting 6 07-15-2010 09:52 AM
count identical strings print last row and count dcfargo Shell Programming and Scripting 2 07-07-2010 11:36 AM
how to count number of rows and sum of column using awk pistachio UNIX for Dummies Questions & Answers 2 05-20-2010 01:55 AM
count number of nonempty columns in row grossgermany Shell Programming and Scripting 2 01-09-2009 05:35 AM
row count but only number part er_zeeshan05 UNIX for Dummies Questions & Answers 1 10-22-2008 11:18 PM