Sponsored Content
Top Forums Shell Programming and Scripting How to get the new columns with conditions? Post 303037557 by jiam912 on Tuesday 6th of August 2019 05:08:37 AM
Old 08-06-2019
How to get the new columns with conditions?

Using the below input file, I extract the minimum and maximum for column 3, using as key column 2, I get also the total times key in column 2 counts. ( column 4 in output file )

Using output file column 4, I want to add 2 columns more ( like output desired )

Column 5 in the fist line starts with 1 and add the value in column 4 (column 6).
for second line, column5 takes the value in column 6(+1), and do the same for line1 (dd the value in column 4 (column 6).

Input file

Code:
1222  10000  20002
1222  10000  20003
1222  10000  20004
1222  30000  20010
1222  30000  20011
1222  30000  20012
1222  30000  20013
1222  30000  20014
1222  40000  10000
1222  40000  10001
1222  40000  10002
1222  40000  10003
1222  40000  10004
1222  50000  30000
1222  50000  30001
1222  50000  30002
1222  50000  30003
1222  50000  30004

Code used

Code:
awk '{ currKey = $2 }
currKey != prevKey { prt(); min=$3; cnt=0 }
{ prevRec=$0; prevKey=currKey; max=$3; cnt++ }
END { prt() }

function prt(   f) {
    if ( cnt ) {
        split(prevRec,f)
        print f[2], min, max, cnt
    }
}' file1 | sort -k1n

Code:
Output from code

Code:
10000 20002 20004 3
30000 20010 20014 5
40000 10000 10004 5
50000 30000 30004 5

Code:
Output desired

Code:
110000 20002 20004 3   1   3
230000 20010 20014 5   4   8
240000 10000 10004 5   9  13
350000 30000 30004 5  14  18

Appreciate your help in advance
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

2. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

3. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

4. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

7. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

8. Shell Programming and Scripting

Sum of multiple columns based on few conditions

Input file: 0014,A,2020,K,011115000000,xyxxxxxxxxxxxxxx ,B,00001,0003200,0000111 0014,A,2020,K,011115000000,xyxxxxxxxxxxxxxx ,B,00001,0003300,0000100 0014,A,2020,K,011116000000,xyxxxxxxxxxxxxxx ,B,00001,0003400,0000100 0014,A,2020,K,011116000000,xyxxxxxxxxxxxxxx ,B,00007,0003100,0000200... (6 Replies)
Discussion started by: vinus
6 Replies

9. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

10. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies
COLRM(1)						    BSD General Commands Manual 						  COLRM(1)

NAME
colrm -- remove columns from a file SYNOPSIS
colrm [start [stop]] DESCRIPTION
The colrm utility removes selected columns from the lines of a file. A column is defined as a single character in a line. Input is read from the standard input. Output is written to the standard output. If only the start column is specified, columns numbered less than the start column will be written. If both start and stop columns are spec- ified, columns numbered less than the start column or greater than the stop column will be written. Column numbering starts with one, not zero. Tab characters increment the column count to the next multiple of eight. Backspace characters decrement the column count by one. ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect the execution of colrm as described in environ(7). EXIT STATUS
The colrm utility exits 0 on success, and >0 if an error occurs. SEE ALSO
awk(1), column(1), cut(1), paste(1) HISTORY
The colrm command appeared in 3.0BSD. BSD
August 4, 2004 BSD
All times are GMT -4. The time now is 04:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy