Inserting a row if patterns not followed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a row if patterns not followed
# 1  
Old 08-11-2015
Inserting a row if patterns not followed

Hi all,

I have a file of thousands of lines or rows with 2 columns. It is supposed to follow a certain pattern or blocks of 6 rows based on column 1. Below is an example:
Code:
a_1 1
a_1 0.9
a_1 12
a_1 3
a_1 0
a_1 1
b_2 1
b_2 0
b_2 3
c_1 1
c_1 0
c_1 3
c_1 2
c_1 6
c_1 2
d_3 3
d_3 4
e_1 3
e_1 3
e_3 4
e_3 56
...
...

What I need is if the pattern does not follow the pattern of 6 rows in each of the blocks, then insert the content of the row in that pattern of block and add zero (0) in the second column.

The expected format I would like to get is:

Code:
a_1 1
a_1 0.9
a_1 12
a_1 3
a_1 0
a_1 1
b_2 1
b_2 0
b_2 3
b_2 0
b_2 0
b_2 0
c_1 1
c_1 0
c_1 3
c_1 2
c_1 6
c_1 2
d_3 3
d_3 4
d_3 0
d_3 0
d_3 0
d_3 0
e_1 3
e_1 3
e_1 0
e_1 0
e_1 0
e_1 0
e_3 4
e_3 56
e_3 0
e_3 0
e_3 0
e_3 0

Please help me know how to do this editing in awk or in sed
# 2  
Old 08-11-2015
Try:

Code:
awk -v n=6 '
  function fill() {
    while (++i<=n)
      print p,0
    i=0
  } 
  $1!=p && NR>1 {
    fill()
  }
  {
    ++i
    p=$1
    print
  }
  END{
    fill()
  }
' file

This User Gave Thanks to Scrutinizer 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

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

3. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

6. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

7. Shell Programming and Scripting

Inserting a row using shell

hi all, In first run,I am inserting a row into an oracle table through shell script, in the same shell script when I query this table the count comes as 0. When I run the same script again then the count comes correctly, Can anyone help how can I get the count of the row correctly in the... (3 Replies)
Discussion started by: Pratiksha Mehra
3 Replies

8. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

9. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question