Combine incrimental line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine incrimental line
# 1  
Old 10-14-2017
Combine incrimental line

hi guys,

i am writing a bash script.. that produce output some thing like this:

Code:
road[0] 100 300 500
road[1] 100 300 500
road[2] 100 300 500
road[3] 100 300 500
street[0] 400 200 700
street[1] 400 200 700
path 200 100 900

i would like to combine all the entries that having the same but incremental (not sure what is the correct term) first column in condition that the rest of the column are exact match. best way to explain is by the following desire output:

Code:
road[0:3] 100 300 500
street[0:1] 400 200 700
path 200 100 900

im a beginner in script writing. mostly using bash script to automate my task and in my bash script contain nothing but very useful tool from unix itself like sed awk tail sort uniq column etc...

Last edited by Scott; 10-14-2017 at 10:31 PM.. Reason: Code tags
# 2  
Old 10-14-2017
When writing your Bash script, why didn't you sum up the values as you went and output that instead? It seems you had all the information you needed from the input to that script to do that.
# 3  
Old 10-14-2017
i understand your feedback but the script that i wrote is just extracting some data from other file.. which the bit like data is originally there..

thanks anyway for responding this..
# 4  
Old 10-14-2017
Just for understanding, do the bits in brackets in the desired output ([0:3], for example) represent the minimum and maximum values found in the input (e.g. read[0] ... read[1] ... ... read[3]), or what?
# 5  
Old 10-14-2017
Quote:
Originally Posted by Scott
Just for understanding, do the bits in brackets in the desired output ([0:3], for example) represent the minimum and maximum values found in the input (e.g. read[0] ... read[1] ... ... read[3]), or what?
yes yes they are representing minimum and maximum..

so that let say if we have
Code:
road[0] 100 300 500
road[1] 100 300 500
road[2] 100 300 500
road[3] 100 300 500
road[4] 100 400 600
road[5] 100 400 600

it will output something like this:

Code:
road[0:3] 100 300 500
road[4:5] 100 400 600

# 6  
Old 10-14-2017
This is why you should write a specification of what you want (not withstanding the fact that you haven't shown us any of your work), instead of just throwing data at us. First the brackets, now the following fields as well need to be considered. How should they be considered?
This User Gave Thanks to Scott For This Post:
# 7  
Old 10-14-2017
Quote:
Originally Posted by Scott
This is why you should write a specification of what you want (not withstanding the fact that you haven't shown us any of your work), instead of just throwing data at us. First the brackets, now the following fields as well need to be considered. How should they be considered?
actually that is all im looking for.. actually for the field i try to mention at post #1 "in condition that the rest of the column are exact match" im sorry if the english is not good. maybe i need to use field instead of column..

as for the erlier script that i had done. they are just simple sed to remove the any line that match header and foote. also ditch out some unwanted data like the last 2 column using awk.

this is the original file
Code:
header title
 
road[0] 100 300 500 0.3 0.5
road[1] 100 300 500 0.3 0.6 
road[2] 100 300 500 0.3 0.7
road[3] 100 300 500 0.3 0.8
road[4] 100 400 600 0.3 0.9
road[5] 100 400 600 0.3 0.9
street[0] 400 200 700 0.5 0.3
street[1] 400 200 700 0.5 0.4
path 200 100 900 0.6 0.1

footer

in my bash im using follwing line to get my almost desired data:
Code:
less <file> | sed '/header\|footer/d' | awk '{print $1, $2, $3, $4}'

the code get me this
Code:
road[0] 100 300 500
road[1] 100 300 500
road[2] 100 300 500
road[3] 100 300 500
road[4] 100 400 600
road[5] 100 400 600
street[0] 400 200 700
street[1] 400 200 700
path 200 100 900

and i wish my output to be like this so that my output will not look so mess especially when the same bit goes up until more than 100.
Code:
road[0:3] 100 300 500
road[4:5] 100 400 600
street[0:1] 400 200 700
path 200 100 900

sorry for the confusion and thanks for the clarification.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Combine multiline to one line till a blank line

Hello, I have a file as :- ABC DEF GHI JKL <BlankLine> MNO PQR STU VWX <BlankLine> YZA I need it as below:- ABCDEFGHIJKL; MNOPQRSTUVWX; (3 Replies)
Discussion started by: jassi10781
3 Replies

3. Shell Programming and Scripting

Combine 2 line with space issue

Hello all, i am new to linux , and please need your help and suggestion on.... when vi 1.txt :set list, it looks like $ is displaying the end of line Filter: vlan1-BUM-1M $ BUM-1M 0 ... (4 Replies)
Discussion started by: samoptimus
4 Replies

4. Shell Programming and Scripting

Combine line before the pattern

Hi, I am not very familiar with sed and awk and i have a huge file to process which is impossible to do manually. I want to print out beginning from "Network" until end of line only (excluding the Version). AND. the decription to be all in one line. File: Version: 2.0 Network: xxx... (9 Replies)
Discussion started by: The One
9 Replies

5. Shell Programming and Scripting

Combine common line from 2 Huge files

Hi, I am having 2 huge files having line count more than 10million. The files look like: File 1 45905099 2059 942961505 3007 8450875165 7007 615565331 3015 9415586035 9012 9871573 5367 4415655 4011 44415539519 5361 3250659295 4001 5950718618 9367 File 2 44415539519 TQ03... (2 Replies)
Discussion started by: rochitsharma
2 Replies

6. UNIX for Dummies Questions & Answers

What is the command use to combine line using sed

Hi all, What is the sed command use to combine line? Example: Below is an output after extracted from few commands aaa bbb ccc ddd eee fff ggg and i would like to combine all the line as shown below, aaa,bbb,ccc,ddd,eee,fff (5 Replies)
Discussion started by: 793589
5 Replies

7. UNIX for Dummies Questions & Answers

combine files line by line

Hi all, I once knew of a simple unix command to do this, but I can't remember it and I can't find it by searching. I have two files. ### FILE A #### A1 A2 A3 A4 A5 ### FILE B #### B1 B2 B3 B4 B5 (2 Replies)
Discussion started by: Digby
2 Replies

8. Shell Programming and Scripting

How to combine text data into one line?

The following input needs to be manipulated as follows: INPUT from file or results of command: ============start: Medium identifier : a45c0213:47eb5485:0aec:0321 Medium label : SQL Disk_11516 Location : Protected : None ... (2 Replies)
Discussion started by: rcky_mntere
2 Replies

9. Shell Programming and Scripting

Combine output on same line

I am trying to get some data from a file and print it on the same line. I have a script that gets the date/time and calculates a DB query call time and sends to a file. I need to take this file and send it in a xcel like format with multiple data columns. example output file (fndbq.out) ... (3 Replies)
Discussion started by: theninja
3 Replies

10. Shell Programming and Scripting

Combine reports and append a line between each

I am new to Unix and have tried to write a ksh script to do the following without success: I have several reports in a directory (report1, report2, report3, etc). I would like to combine all of these reports into one file (REPORTS). I would like to append *** End of Report *** to each report so... (2 Replies)
Discussion started by: ldevito1
2 Replies
Login or Register to Ask a Question