Concatenate rows and redefine range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate rows and redefine range
# 1  
Old 06-04-2015
Concatenate rows and redefine range

I'm trying to find a way to concatenate consecutive rows (key is column $1 and $2) if column $5 an $6 are integers and redefine ranges in columns $3&$4 and $5&$6

Unfortunately I'm still learning the very basics so I cannot figure a way of doing this with awk.

Input file

Code:
15 30  21  21  25.0  25.0
15 30  22  22  26.0  26.0
15 30  23  23  27.0  27.0
15 30  24  24  28.5  28.5
15 30  25  25  29.7  29.7
15 30  26  26  30.0  30.0
15 30  27  27  31.0  31.0
17 32 155 155 325.0 325.0
17 32 156 156 326.3 326.3
17 32 158 158 424.0 424.0
17 32 159 159 425.0 425.0
17 32 160 160 426.0 426.0


Desired output:

Code:
15 30  21  23  25.0  27.0
15 30  24  24  28.5  28.5
15 30  25  25  29.7  29.7
15 30  26  27  30.0  31.0 
17 32 155 155 325.0 325.0
17 32 156 156 326.3 326.3
17 32 158 160 424.0 426.0

Thanks in advance!
# 2  
Old 06-04-2015
Presumably, besides the condition that column $5 an $6 are integers (technically 27.0 is not an integer, but you seem to mean numbers that end with .0) they should als be only one higher than the previous line? Otherwise :
Code:
15 30  26  26  30.0  30.0
15 30  27  27  31.0  31.0
17 32 155 155 325.0 325.0

would need to be combined to :
Code:
15 30  26  155  30.0  325.0

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-04-2015
Hi,

Thank you for your answer, you are correct, I need to merge only the rows which ends in
Code:
.0

and yes should be one higher from the previous row if the same values in column
Code:
$1

and
Code:
$2

.
# 4  
Old 06-04-2015
How about this:

Code:
awk 'key {
    if (key == $1" "$2 && f5 ~ ".0$" && $5 ~ ".0$") {
        t3=$3
        t5=$5
        next
    }
    print key,f3,t3,f5,t5
}
{ key=$1" "$2; f3=t3=$3; f5=t5=$5 }
END { if(key) print key,f3,t3,f5,t5 } ' infile

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 06-04-2015
Thank you very much, worked like a charm.
# 6  
Old 06-05-2015
If x ~ ".0$" is to be an integer test, then should be x ~ "\.0$" or x ~ "[.]0$" or better the speaking x==int(x).
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 06-05-2015
As long as all of the data in the fields in question are written with a decimal point and one digit after the decimal point, you don't need to look for the decimal point. You could just check if the fields ends with a "0" with $x ~ "0$".
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate broken rows

I need to concatenate the rows that are broken (because of carriage return and line feed) in unix. Input 123|456|789|"" 987|786|"GRT "|"" 3455|896|654|456|"" 457|234|"RT"|"PR TY"|"" Output 123|456|789|"" 987|786|"GRT"|"" 3455|896|654|456|"" 457|234|"RT"|"PRTY"|"" (16 Replies)
Discussion started by: meet_calramz
16 Replies

2. Shell Programming and Scripting

Printing rows based on column range

Hello, I have a file with nearly 57K lines. I want to filter the lines based on the range of values in a column. For e.g. print lines whose 3rd filed is >=0.02. Input file: LOC_Os09g32030 LOC_Os02g18880 0.0200037219149773 undirected NA NA LOC_Os03g58630 LOC_Os09g35690 ... (1 Reply)
Discussion started by: Sanchari
1 Replies

3. Shell Programming and Scripting

Sum of range of rows and columns in matrix

Hi all, I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all... (4 Replies)
Discussion started by: CAch
4 Replies

4. Shell Programming and Scripting

Need help to read rows from one file and concatenate to another

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (6 Replies)
Discussion started by: carlos_anubis
6 Replies

5. UNIX for Dummies Questions & Answers

Read rows from source file and concatenate output

Hi guys; TBH I am an absolute novice, when it comes to scripting; I do have an idea of the basic commands... Here is my problem; I have a flatfile 'A' containing a single column with multiple rows. I have to create a script which will use 'A' as input and then output a string in in the... (0 Replies)
Discussion started by: carlos_anubis
0 Replies

6. Shell Programming and Scripting

delete rows between closest pattern or range

Hi I am having some problom deleting the lines between two specific lines in a file. need to delete lines between two closest lines. i.e need to find the closest range or pattern in a file with repeating patterns. Sample Input: WARNING <some text in n number of lines> ERROR:2597... (10 Replies)
Discussion started by: sudheer1984
10 Replies

7. Shell Programming and Scripting

Concatenate some of the rows...

i have a file as below and i need to load it into oracle. The problem is, some of the rows are in 2 lines. 123456_PosWlist ----- ----- IN 0/0 123456_PosWListRpt ----- ----- IN 0/0 123456_PosWListCSV ----- -----... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

8. Shell Programming and Scripting

concatenate two files with different No of rows

need a shell which perform following function file 1 ( every time new data comes) 1212 2323 3434 4545 5656 . . . . file 2 (fixed line) update bc_tbl set aix=data , bix=back where cix=U and serial=; now when i execute shell it will concatinate file 1, file 2 & make file 3 as... (3 Replies)
Discussion started by: The_Archer
3 Replies

9. Shell Programming and Scripting

Concatenate rows in to 2 files

I have 2 files FILEA 1232342 1232342 2344767 4576823 2325642 FILEB 3472328 2347248 1237123 1232344 8787890 I want the output to go into a 3rd file and look like: FILEC 1232342 3472328 (1 Reply)
Discussion started by: unxusr123
1 Replies

10. Shell Programming and Scripting

Concatenate 2 rows into 1 row

I need to search a file for two values (valueA & valueB). ValueA will be on a different row than valueB, and concatenate the two together on the same row of my output. Example: search input file for strings "node" and "OS", combine the two results into one row input node A text text OS... (4 Replies)
Discussion started by: indianadoug
4 Replies
Login or Register to Ask a Question