Indexing each repeating pattern of rows in a column using awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Indexing each repeating pattern of rows in a column using awk/sed
# 1  
Old 09-07-2014
Indexing each repeating pattern of rows in a column using awk/sed

Hello All,

I have data like this in a column.
Code:
0
1
2
3
0
3
4
5
6
0
1
2
3
etc.

where 0 identifies the start of a pattern in my data.
So I need the output like below using either awk/sed.
Code:
0  1
1  1
2  1
3  1
0  2
3  2
4  2
5  2
6  2
0  3
1  3
2  3
3  3

Basically indexing every repeating pattern and add additional column with that index number.

Thanks in advance
Sidda
# 2  
Old 09-07-2014
As I am sure you already know, sed doesn't do arithmetic...
Try:
Code:
awk '
$1 == 0{ pat++ }
{ printf("%s  %d\n", $0, pat) }
' data

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-07-2014
Or
Code:
awk '{printf "%s  %d\n", $0, p+=$1==0}' file
0  1
1  1
2  1
3  1
0  2
3  2
4  2
5  2
6  2
0  3
1  3
2  3
3  3

or even
Code:
awk '{print $0, p+=$1==0}' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

3. Shell Programming and Scripting

Create new rows for each column value with awk

Hi, I have the following type of data that is separated by tabs: -2 abandonar abandono abandonas abandona abandonamos abandonáis abandonan -4 abandonado abandonada abandonados abandonadas -2 abandona abandonos ... (1 Reply)
Discussion started by: owwow14
1 Replies

4. Shell Programming and Scripting

Awk: Interchange the Rows and column

Hi, I have following input and want to change it to following output INPUT 01-APR-14,KB,822714 01-APR-14,MB,8133431 02-APR-14,KB,757140 02-APR-14,MB,7770368 03-APR-14,KB,815427 03-APR-14,MB,7590511 04-APR-14,MB,7529895 04-APR-14,KB,779561 05-APR-14,MB,8151537 05-APR-14,KB,809675 ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

5. Shell Programming and Scripting

awk and or sed command to sum the value in repeating tags in a XML

I have a XML in which <Amt Ccy="EUR">3.1</Amt> tag repeats. This is under another tag <Main>. I need to sum all the values of <Amt Ccy=""> (Ccy may vary) coming under <Main> using awk and or sed command. can some help? Sample looks like below <root> <Main> ... (6 Replies)
Discussion started by: bk_12345
6 Replies

6. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

7. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. Shell Programming and Scripting

awk transpose rows to column

Need to transpose in awk rows to column like this: input: A1,6,5,4 3,2,1, A2,8,7,9,10,11,12,13,14 A3,1,2,3,5,7,8,9 A4,9,4,8,1,5,3, output: A1,1 A1,2 A1,4 ... A2,7 A2,8 ... A3,1 A3,2 ... A4,1 A4,3 (5 Replies)
Discussion started by: sdf
5 Replies

9. Shell Programming and Scripting

Sed Replace repeating pattern

Hi, I have an sqlplus output file using the character ';' as a delimiter and I would like to replace the fields without datas (i.e delimited by ';;') by ';0;' Example: my sqlplus output: 11;22;33;44;;;77;; What I would like to have: 11;22;33;44;0;0;77;0; Thanks in advance for your... (2 Replies)
Discussion started by: popesk
2 Replies

10. Shell Programming and Scripting

Print rows, having pattern in specific column...

Hello all, :) I have a pattern file some what like this, cd003 cd005 cd007 cd008 and input file like this, abc cd001 cd002 zca bca cd002 cd003 cza cba cd003 cd004 zca bac cd004 cd005 zac cba cd005 cd006 acz acb cd006 cd007 caz cab cd007 ... (25 Replies)
Discussion started by: admax
25 Replies
Login or Register to Ask a Question