Convert Column Values to a Range of Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Column Values to a Range of Values
# 1  
Old 10-04-2013
Convert Column Values to a Row with Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example:

"Column A"
1
2
3
4
10
12
14
15
16
17
18

"Column B"
1
4
5
6
7
10

etc.

I want the output to be like this:

"Column A" 1-4, 10, 12, 14-8
"Column B" 1, 4-7, 10

I'm just starting with shell scripting, so a code with a description of how to adjust variables would be greatly appreciated!

Last edited by newbio; 10-04-2013 at 07:27 PM..
# 2  
Old 10-05-2013
This sounds more like a homework assignment than a "real" project. If this is a homework assignment, you need to close this thread and start a new thread in the Homework and Coursework Questions forum.

If not, you need to provide a lot more information about the input columns and the output you want.

Are the input columns in separate text files, a single text file with all of the data in a single column and some particular input lines indicating the start of a new column, actual columns in a text file (with what used to distinguish field boundaries), or something else? Are the column titles all in double quotes? Are the values in the input columns sorted (and, if not, is the script supposed to sort the output to appear as though the input had been sorted), or should it be a comma separated list of increasing (or increasing and decreasing) range values in the order in which they appeared in the input? (And, please use CODE tags to show sample input, sample output, and code segments!)

What variables do you want to adjust (and why)?

If you want us to help you learn how to do something, you need to provide us with more information about what you have (what shell, operating system, and operating system version are you using), what you want to do, and what resources you have to do it. This is not a forum for you to give vague comments about a project you'd like for someone else to design and implement for you.
# 3  
Old 10-05-2013
Here's an example, assuming the column separator is a pipe:
Code:
awk -F"|" '{
	switch (NR) {
		case 1:
			t1 = $1 " ";
			t2 = $2 " ";
			next;
		case 2:
			t1 = t1 $1; n1=$1;
			t2 = t2 $2; n2=$2;
			next;
		default:
			if ($1!=n1+1 && $1!="") {
				t1 = t1 (p1!=n1? "-" n1:"") ", " $1;
				p1 = $1;
			}
			if ($2!=n2+1 && $2!="") {
				t2 = t2 (p2!=n2? "-" n2:"") ", " $2;
				p2 = $2;
			}
			if ($1 != "")
				n1 = $1;
			if ($2 != "")
				n2 = $2;
	}
}
	END {print t1 (p1!=n1? "-" n1:""); print t2 (p2!=n2? "-" n2:"")}
' file


Last edited by jethrow; 10-05-2013 at 04:05 AM..
# 4  
Old 10-05-2013
Sorry, this is not homework, I am not a student anymore. I apologize for my inexperience, I did not know how to use code tags. By variables I mean, for example, if the code requires the path to a file, it would be nice to know that is what an abbreviation means. The data is in several formats, as I was not sure what would be easiest to use - I have it in excel and txt formats (single column with the column names followed by their data, as well as in multiple columns), and the data is in increasing order with no quotes anywhere. I would like the output to be the column name in one cell and then the data in one or more cells (in increasing order, which is the order they are in currently) as it appears above. I am using a new apple computer with Xcode to try to write various "awk" commands to no avail, but due to my lack of experience I am not sure exactly what to use.
Thanks much for the suggestion, jethrow, I will try it!
# 5  
Old 10-05-2013
Quote:
Originally Posted by newbio
Sorry, this is not homework, I am not a student anymore. I apologize for my inexperience, I did not know how to use code tags. By variables I mean, for example, if the code requires the path to a file, it would be nice to know that is what an abbreviation means. The data is in several formats, as I was not sure what would be easiest to use - I have it in excel and txt formats (single column with the column names followed by their data, as well as in multiple columns), and the data is in increasing order with no quotes anywhere. I would like the output to be the column name in one cell and then the data in one or more cells (in increasing order, which is the order they are in currently) as it appears above. I am using a new apple computer with Xcode to try to write various "awk" commands to no avail, but due to my lack of experience I am not sure exactly what to use.
Thanks much for the suggestion, jethrow, I will try it!
Moderator's Comments:
Mod Comment At the top of the quick reply frame, you'll see this note:
Code:
Do you have any code fragments or data samples in your post? If so wrap them in code
                                                                                 co
tags using the code tag button in the editor below (hint: it looks like this --> de)

What that means is that you should select the code icon button at the top of the editing frame to add code tags and then type or paste your sample input data, output data, or code, between the tags it added to your editing frame.


Since you have an excel file containing the data you want to process, use the export as CSV file feature in excel to produce a text file containing your data with commas separating columns in your data. Post a representative sample of your filename.csv file (in CODE tags) and show us the corresponding output that you want the script to produce (in CODE tags).

Also show us the output of the command uname -a so we will be able to tailor the script to the capabilities provided by your operating system.

We may then be able to help you write an awk script to process your data.

PS I haven't examined jethro's suggested awk script in detail, but it uses some constructs (such as the switch() statement) that are not available in standard versions of awk. And it seems to assume that your input file always contains two columns. (I assumed that the
Quote:
etc.
line in your first post meant that there could be several more columns in your input. I'm sure you'll explain how many columns could appear in your input in your next post when you show us your sample input file.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 Query -Convert multi values from column to rows

Hi Team I am using DB2 artisan tool and struck to handle multi values present in columns that are comma(,) separated. I want to convert those column values in separate rows . For example : Column 1 Column2 Jan,Feb Hold,Sell,Buy Expected Result Column1 ... (3 Replies)
Discussion started by: Perlbaby
3 Replies

2. Shell Programming and Scripting

Create range of values and count values.

Gents, It is possible to generate a range of values according to column 1 and count the total of rows in the range. example input 15.3 15.5 15.8 15.9 16.0 16.1 16.8 17.0 17.5 18.0 output desired 15.0 - 15.9 = 4 (10 Replies)
Discussion started by: jiam912
10 Replies

3. Shell Programming and Scripting

Convert Column data values to rows

Hi all , I have a file with the below content Header Section employee|employee name||Job description|Job code|Unitcode|Account|geography|C1|C2|C3|C4|C5|C6|C7|C8|C9|Csource|Oct|Nov|Dec|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep Data section ... (1 Reply)
Discussion started by: Hypesslearner
1 Replies

4. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Average values in a column based on range

Hi i have data with two columns like below. I want to find average of column values like if the value in column 2 is between 0-250000 the average of column 1 is some xx and average of column2 is ww then if value is 250001-5000000 average of column 1 is yy and average of column 2 is zz. And my... (5 Replies)
Discussion started by: bhargavpbk88
5 Replies

6. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

7. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

8. Shell Programming and Scripting

Convert column values into row

hi, I have a requirement where in I read the values from a file using awk. The resulting data should be converted into row format from column format. For ex: My log file login.lst contains the following SERVER1 DB1 SERVER2 DB2 SERVER3 DB3 SERVER4 DB4 I use awk to grep only the server... (6 Replies)
Discussion started by: senthil3d
6 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 pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question