Script to code every 2 consecutive entries as single entry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to code every 2 consecutive entries as single entry
# 1  
Old 04-08-2015
Script to code every 2 consecutive entries as single entry

All,

I come across the below requirement and my search on the previous posts did not result into any matches.
I have one column of data from a csv file like below. And I need to add additional column based on string count in first column.

Code:
Given column, Required column, Other columns
A, 1, ...leave all other columns in the file as it is ....
A, 1, ...leave all other columns in the file as it is ....
A, 1, ...leave all other columns in the file as it is ....
B, 1, ...leave all other columns in the file as it is ....
B, 1, ...leave all other columns in the file as it is ....
C, 2, ...leave all other columns in the file as it is ....
C, 2, ...leave all other columns in the file as it is ....
D, 2, ...leave all other columns in the file as it is ....
E, 3, ...leave all other columns in the file as it is ....
E, 3, ...leave all other columns in the file as it is ....
F, 3, ...leave all other columns in the file as it is ....
so.. on

Basically every 2 consecutive keys in column 1 should be coded as one single key. Also the number of rows for each key in column 1 will vary (as noticed here for the count of A's , B's and C and D).
My actual file has thousands of rows.

Thanks in advance..
Sidda
# 2  
Old 04-09-2015
Your explanation of what you are trying to do leaves me completely baffled. You seem to want to combine the rows with keys A and B to produce some unspecified output; combine the rows with keys C and D to produce some unspecified output; combine the rows with keys E and F to produce some unspecified output; ...

What is a "string count in first column"?

Please give us a CLEAR description of what you are trying to do and show us the output that is supposed to be produced from your small sample input file.
# 3  
Old 04-09-2015
Find the modified and clear output

Hi Don,
Here is my clear requirement.
Column-1 is my given data with unique strings where each string appearing more than once but in the order.
For example 'A' appears only in 3 rows but no where else in later part of the file. Similarly 'B' appears 2 times. But 'D' appear only once. etc.
Coming to the Required Index column "for every 2 keys in the first column there should be a single index and that too it should auto increment as shown in the given example below like 1, 2,3 and so.. on".

Code:
Given column, Required Index 
A, 1
A, 1
A, 1
B, 1
B, 1
C, 2
C, 2
D, 2
E, 3
E, 3
F, 3
etc.. 1000's of rows.

Hope this time my requirement is clearly depicted..

Regards
Sidda

Last edited by ks_reddy; 04-09-2015 at 01:57 AM.. Reason: Added code blocks.
# 4  
Old 04-09-2015
Perhaps something like:
Code:
awk '
BEGIN {	FS = OFS = "," }
$1 != last { last = $1; x = int(1 + c++ / 2) }
$2 = x
' file.csv

which with your original sample input in a file named file.csv produces the output:
Code:
A,1, ...leave all other columns in the file as it is ....
A,1, ...leave all other columns in the file as it is ....
A,1, ...leave all other columns in the file as it is ....
B,1, ...leave all other columns in the file as it is ....
B,1, ...leave all other columns in the file as it is ....
C,2, ...leave all other columns in the file as it is ....
C,2, ...leave all other columns in the file as it is ....
D,2, ...leave all other columns in the file as it is ....
E,3, ...leave all other columns in the file as it is ....
E,3, ...leave all other columns in the file as it is ....
F,3, ...leave all other columns in the file as it is ....

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
# 5  
Old 04-09-2015
Thanks..

Thanks Don.
Your script works very well, but it won't print the remaining columns it is. I am trying to modify that. Will be great if you have a smarter solution to print $0 as it is in addition to the columns 1(Given column) and 2(Generated Index column).

Regards
Sidda
# 6  
Old 04-09-2015
I don't get it.

In post #1 in this thread you showed us a sample input file. In post #3, you showed us the 1st two columns of the output you wanted. All of the input fields were preserved from the sample input your provided in post #1 with the contents of the 2nd field replaced by the numbers you said you wanted to be produced in that field in this output.

How is that different from the output produced by my script???
# 7  
Old 04-09-2015
Would this modification of Don Cragun's proposal do what you need?
Code:
awk '
BEGIN           {FS = OFS = "," }
$1 != last      { last = $1; x = int((1 + c++) / 2) }
$2 = x OFS $2
' file.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

2. Shell Programming and Scripting

awk script to call another script based on second column entry

Hi I have a text file (Input.txt) with two column entries separated by tab as given below: aaa str1 bbb str2 cccccc str3 dddd str4 eee str3 ssss str2 sdf str3 hhh str1 fff str2 ccc str3 ..... ..... ..... (1 Reply)
Discussion started by: my_Perl
1 Replies

3. How to Post in the The UNIX and Linux Forums

Help me, write a bash script to delete parent entry with all their child entry in LDAP UNIX server

Hi All, Please help me and guide me to write a bash/shell script on Linux box to delete parent entry with all their child entries. example: Parent is : ---------- dn: email=yogesh.kumar@wipro.com, o=wipro, o=in child is: ---------- dn: cn: yogesh kumar, email=yogesh.kumar@wipro.com,... (1 Reply)
Discussion started by: Chand
1 Replies

4. Shell Programming and Scripting

Removing 3 consecutive lines from script

Hi I need to remove three consecutive lines of code which appear multiple times during a script. Two of the lines also appear in other parts of the scripts and need to stay so I can't strip out the code per se - It needs to be the exact three lines. Hope that makes sense ! Any help much... (5 Replies)
Discussion started by: Grueben
5 Replies

5. UNIX for Dummies Questions & Answers

How to combine and insert missing consecutive numbers - awk or script?

Hi all, I have two (2) sets of files that are based on some snapshots of database that I want to merge and insert any missing sequential number. Below are example representation of these files: file1: DATE TIME COL1 COL2 COL3 COL4 ID 01/10/2013 0800 100 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

7. 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

8. Shell Programming and Scripting

Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this: cn=user.blr.ou=blr.india.o=company The line should be converted like this: cn=user.blr,ou=blr.india,o=comapny Was wondering how to do that using shell script. Please use tags where appropriate, thank you (4 Replies)
Discussion started by: saurabhkoar
4 Replies

9. Shell Programming and Scripting

Script in SED and AWK so that it treats consecutive delimiters as one

Hi All, I am trying to cut to do a cut operation, but since there are seems to be more than one deltimiters in some occasion I am not able to get the exact field. Can you please provide an SED and AWK script for treating the source file in such a way that all consecutive delimiters are treated... (3 Replies)
Discussion started by: rakesh.su30
3 Replies

10. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies
Login or Register to Ask a Question