Grep values from column 2 in reference of column 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep values from column 2 in reference of column 1
# 8  
Old 05-16-2018
And I have no idea what the automatic variable is supposed to do.

Wouldn't grepping the entire file against itself just print the entire file?
# 9  
Old 05-16-2018
By boosting your awk code I get this all-in-one
Code:
awk '
  function range_to_out() {
    out=(out sep (start == last ? start : (start "-" last)))
  }
  function print_out() {
    printf "%s  %s\n", p1, out
  }
  NR == 1 { start=last=$2; p1=$1; next } 
  {
    if ($2 == last+1) { last=$2 } else {
      range_to_out(); sep=","; start=last=$2
    }
  }
  $1 != p1 || length(out) > 50 { print_out(); sep=out=""; p1=$1 }
  END { range_to_out(); print_out() }
' file

This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 05-17-2018
MadeInGermany, many thanks for your help..
# 11  
Old 05-20-2018
input file updated

MadeinGermany.

I am trying to figure out how to have same output like in my code.

The information in the column 2 for input file needs to be in output file from column 21 to 80, filling all this range,


this what i got using your code
Code:
X52152              21-34,36-82,84,83,85-107,109-191,193,192,194-197,206,                         
X52152              198-199,207,200,202,209,203,211,204-205,213,208,210,212,                            
X52152              214-216,218-221,233,222,245,223,246,249,251,248,224-232,                            
X52152              234-243,247,250,253-330,332,331,333-336,338,337,339-462,                            
X52152              467,463-466,468-480,482-514,516,515,517-529,531-700,702,                            
X52152              701,703-752,754-764,766-801,803,802,804-1016,1019,1017-1018,                        
X52152              1020-1035,1037-1104,1107,1105,1108,1106,1109-1169,1172-1176,                        
X52152              1178-1209,1221,1210-1220,1222-1242,1244-1264,1266-1281,                             
X52152              1283,1282,1284-1312,1314,1313,1315-1319,1321-1366,1368-1403,                        
X52152              1405-1465,1467-1498,1500-1502,1504-1527,1529-1598,1600-1796,

this what i got using my code and i want to get the same output with your code
Code:
X52152              21-34,36-82,84,83,85-107,109-191,193,192,194-197,206,                                                   
X52152              198-199,207,200,202,209,203,211,204-205,213,208,210,212,                                                
X52152              214-216,218-221,233,222,245,223,246,249,251,248,224-232,                                                
X52152              234-243,247,250,253-330,332,331,333-336,338,337,339-462,467,                                            
X52152              463-466,468-480,482-514,516,515,517-529,531-700,702,701,                                                
X52152              703-752,754-764,766-801,803,802,804-1016,1019,1017-1018,                                                
X52152              1020-1035,1037-1104,1107,1105,1108,1106,1109-1169,1172-1176,                                            
X52152              1178-1209,1221,1210-1220,1222-1242,1244-1264,1266-1281,1283,                                            
X52152              1282,1284-1312,1314,1313,1315-1319,1321-1366,1368-1403,                                                 
X52152              1405-1465,1467-1498,1500-1502,1504-1527,1529-1598,1600-1796.

I have changed this in your code

Code:
 $1 != p1 || length(out) > 51 { print_out(); sep=out=""; p1=$1 }

to get close to my out file, but i cant.

I attache a file named file.zp which contends the input information.

Appreciate your help.
# 12  
Old 05-23-2018
The following seems to do it
Code:
awk '
  function range_to_out() {
    out=(out sep)
    addout=(start == last ? start : (start "-" last))
    if (length(out addout) >= 60) {
      print_out()
    }
    out=(out addout)
    sep=","
  }
  function print_out() {
    printf "%s  %s\n", p1, out
    sep=out=""
  }
  NR == 1 { start=last=$2; p1=$1; next } 
  {
    if ($2 == last+1) { last=$2 } else {
      range_to_out(); start=last=$2
    }
  }
  $1 != p1 { print_out(); p1=$1 }
  END { range_to_out(); print_out() }
' file


Last edited by MadeInGermany; 05-23-2018 at 04:35 PM.. Reason: bug fix
This User Gave Thanks to MadeInGermany For This Post:
# 13  
Old 05-23-2018
Hi MadeInGermany.
Appreciate your support, I will try the code and let you know, many tks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Need to grep for decimal values only in the second column.

Hi, I have a file in which I need to print all the lines that have decimal values in the second column. The below prints all the decimal values from the second column but I need the complete line to be printed. cat hello.out | sed 's/ */:/g' | cut -d : -f 2 | ggrep -Eo "+\.+" Can you... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

4. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

5. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

6. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

7. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

9. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question