join rows based on the column values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting join rows based on the column values
# 1  
Old 11-03-2011
Question join rows based on the column values

Hi,
Please help me to convert the input file to a new one.

input file:
--------
Code:
1231231231 3 A
4561223343 0 D
1231231231 1 A
1231231231 2 A
1231231231 4 D
7654343444 2 A
4561223343 1 D
4561223343 2 D

the output should be:
--------------------
Code:
1231231231 3#1#2 A
4561223343 0 D
1231231231 4 D
7654343444 2 A
4561223343 1#2 D

I have to merge the rows if 3rd column is A or D and 2nd column should not equal to zero.Separate rows for A and D.

Last edited by radoulov; 11-03-2011 at 05:46 PM.. Reason: Code tags!
# 2  
Old 11-03-2011
Code:
$ cat lmerge.awk

# Only run the code inside the bracket if $2 is nonzero.
$2 {
    # Append second column to array with index like "1231231231,D".
    # Value gets stored like #1#2#3#4.  We'll chop off the first # later.
    A[$1","$3]=A[$1","$3] "#" $2
    # Print this line directly if $2 is zero.
    } ! $2

END { # Print the contents of the array, chopping the index X into V[1]="1231231231",
       # V2="D", skipping past the first character of A[X], and printing in the order we want.
        for(X in A)
        {
                split(X, V, ",");
                print V[1], substr(A[X],2), V[2];
        }
}

$ awk -f lmerge.awk < data

4561223343 0 D
4561223343 1#2 D
1231231231 3#1#2 A
7654343444 2 A
1231231231 4 D

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-03-2011
Thanks a lot Corona688. Can you please explain the code so that in future I can write the logic myself.

Smilie
# 4  
Old 11-03-2011
I added tons of comments in the meantime, if those aren't sufficient, ask more specific questions and I'll give more specific answers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command -Filter rows in fixed width file based on column values

Hi All, I am trying to select the rows in a fixed width file based on values in the columns. I want to select only the rows if column position 3-4 has the value AB I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Discussion started by: ashok.k
2 Replies

2. UNIX for Dummies Questions & Answers

Join 2 files based on certain column

I have file input1.txt 11103|11|OTTAWA|City|AA|CAR|0|0|1|-1|0|8526|2014-09-07 23:00:14 11103|11|OTTAWA|City|BB|TRAIN|0|0|2|-2|6|6359|2014-09-07 23:00:14 11104|11|CANADA|City|CC|CAR|0|0|2|-2|0|5947|2014-09-07 23:00:14 11104|11|CANADA|City|DD|TRAIN|0|0|2|-2|1|4523|2014-09-07 23:00:14... (5 Replies)
Discussion started by: radius
5 Replies

3. Shell Programming and Scripting

Choosing rows based on column values

I have a .csv file: A,B,0.6 C,D,-0.7 D,E,0.1 A,E,0.45 D,G, -0.4 I want to select rows based on the values of the 3rd columns such that it is >=0.5 or <= -0.5 Thanks. A,B,0.6 D,G, -0.7 (1 Reply)
Discussion started by: Sanchari
1 Replies

4. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

5. Shell Programming and Scripting

join two files based on one column

Hi All, I am trying to join to files based on one common column. Cat File1 ID HID Ab_1 23 Cd 45 df 22 Vv 33 Cat File2 ID pval Ab_1 0.3 Cd 10 Vv 0.0444 (3 Replies)
Discussion started by: newpro
3 Replies

6. UNIX for Dummies Questions & Answers

Extracting rows from a space delimited text file based on the values of a column

I have a space delimited text file. I want to extract rows where the third column has 0 as a value and write those rows into a new space delimited text file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on numerical values of a column

I have a text file where the second column is a list of numbers going from small to large. I want to extract the rows where the second column is smaller than or equal to 0.0001. My input: rs10082730 9e-08 12 46002702 rs2544081 1e-07 12 46015487 rs1425136 1e-06 7 35396742 rs2712590... (1 Reply)
Discussion started by: evelibertine
1 Replies

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

9. UNIX for Dummies Questions & Answers

How to assign scores to rows based on column values

Hi, I'm trying to assign a score to each row which will allow me to identify which rows differ. In the example file below, I've used "," to indicate column separators (my actual file has tab separators). In this example, I'd like to identify that row 1 and row 5 are the same, and row 2 and row... (4 Replies)
Discussion started by: auburn
4 Replies

10. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question