Print first row of column a, last row of column b if column a has the same value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print first row of column a, last row of column b if column a has the same value
# 1  
Old 10-12-2016
Print first row of column a, last row of column b if column a has the same value

I have a table with this structure:

Code:
cola colb colc
1 19 lemon
20 31 lemon
32 100 lemon
159 205 cherries
210 500 cherries

and need to parse it into this format:

Code:
cola colb colc
1 100 lemon
159 500 cherries

So I need the first row of cola and the last row of colb if colc has the same pattern.

Code:
awk 'a[$3]++'

gives me only the first rows and I cannot find a solution for the last row.

Last edited by Don Cragun; 10-12-2016 at 05:57 PM.. Reason: Add CODE tags for sample input and output.
# 2  
Old 10-12-2016
Code:
awk '
        ! ( $3 in A_min ) {
                A_min[$3] = $1
        }
        {
                A_min[$3] = ( A_min[$3] < $1 ? A_min[$3] : $1 )
                A_max[$3] = ( A_max[$3] > $2 ? A_max[$3] : $2 )
        }
        END {
                for ( k in A_min )
                        print A_min[k], A_max[k], k
        }
' file

# 3  
Old 10-12-2016
Quote:
Originally Posted by coppuca
I have a table with this structure:

Code:
cola colb colc
1 19 lemon
20 31 lemon
32 100 lemon
159 205 cherries
210 500 cherries

and need to parse it into this format:

Code:
cola colb colc
1 100 lemon
159 500 cherries

So I need the first row of cola and the last row of colb if colc has the same pattern.

Code:
awk 'a[$3]++'

gives me only the first rows and I cannot find a solution for the last row.
Are all of the lines in your table with the same colc values adjacent (as in you sample) or can they appear on separated lines?

Are values in cola and colb always in increasing order in your table (as in your sample) or can they appear in random order?

Where is you table located? (In an array, in a file, in a database, etc.?)

If you have a problem, why don't you try to write some code to solve that problem? Why do you expect to find the solution to all of your problems without writing any code of your own? Why do you think that an awk script that never looks at $1 or $2 could be used to print minimum values of $1 and maximum values of $2?
# 4  
Old 10-13-2016
Code:
echo 1 1 ! | awk 'NR==1 {print; next} ! l[$NF]++ {f[$NF]=$1; if (s) print f[w], s, w} {s=$2; w=$NF}' infile -

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

3. Shell Programming and Scripting

Split column when value in column is blank in any row

Hi Experts, In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data My issue is i am not able to get header for column from raw file into new file which... (1 Reply)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

How to print column based on row number

Hi, I want to print column value based on row number say multiple of 8. Input file: line 1 67 34 line 2 45 57 . . . . . . line 8 12 46 . . . . . . line 16 24 90 . . . . . . line 24 49 67 Output 46 90 67 (2 Replies)
Discussion started by: Surabhi_so_mh
2 Replies

8. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

9. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

10. Shell Programming and Scripting

Print row if value in column 1 is the first occurence

Hi All, I would like to have a script which is able to perform the below. Print the whole row if column1 which is "0001" for the below example is the first occurrence. Subsequent "0001" occurrence will not be printed out and so on. Can any expert help ? Input: 0001 k= 40 0001 k= 2... (7 Replies)
Discussion started by: Raynon
7 Replies
Login or Register to Ask a Question