Comparing values in column 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing values in column 1
# 1  
Old 05-08-2009
Comparing values in column 1

Hi to all,

I have the following text within inputfile

Code:
data1,value1,value2
data1,value3,value2
data1,value5,value6

data2,value1,value2
data2,value3,value4

data3,value1,value2
data3,value3,value4

data4,value1,value2
data4,value3,value4
data4,value5,value6

I would like to associate in the same line all values taking as common value $1. I mean, compare the values in column 1 for consecutive lines, and
if $1 of line_n is equal to $1 of line_n+1 put the data as follow:

Code:
data1,value1,value3,value5,value2,value4,value6
data2,value1,value3,value2,value4
data3,value1,value3,value2,value4
data4,value1,value3,value5,value2,value4,value6

I think this could be done using FNR and/or NR variables, but I donīt know
how to use them for this task.

I was trying the next line, but fails because some syntax error:

awk '{arr[i]==$1++}; {if{arr[i]==arr[i+1]}} {print $0, getline; print $0}' inputfile

and something like NR=FNR, but I donīt know how.

Please any help will be very appreciated.

Best regards
# 2  
Old 05-08-2009
if you have Python, here's an alternative
Code:
d={}
for line in open("file"):
    line=line.strip().split(",")
    d.setdefault(line[0],[])
    d[line[0]].extend(line[1:])
for i,j in d.iteritems():
    if i != "":print i,','.join(j)

output:
Code:
# ./test.py
data4 value1,value2,value3,value4,value5,value6
data1 value1,value2,value3,value2,value5,value6
data3 value1,value2,value3,value4
data2 value1,value2,value3,value4

# 3  
Old 05-08-2009
Hi ghostdog74, many thanks for your valuable help, it looks like works, but I donīt have python :-( to try your code. For that reason I was trying with awk.
# 4  
Old 05-08-2009
its just an alternative solution, however the "concept" will be the same for awk. use awk's associative arrays. split on "," with -F or FS, then get the first element as the key to the array. the rest of the columns as values. Then append those values as you go along each line of the file.
# 5  
Old 05-08-2009
Code:
nawk -F"," '{
if (_[$1]=="")
	_[$1]=sprintf("%s,%s",$2,$3)
else
	_[$1]=sprintf("%s,%s,%s",_[$1],$2,$3)
}
END{
for(i in _){
	num=split(_[i],arr,",")
	n=num/2
	printf i","
	for(j=1;j<=2;j++)
		for(k=1;k<=n;k++)
				printf arr[j+2*(k-1)]","		
	print ""
}
}' a.txt | sed 's/,$//'

perl:

Code:
open $fh,"<","a.txt";
while(<$fh>){
	chomp;
	my @tmp=split(",",$_);
	push @{$hash{$tmp[0]}->{1}},$tmp[1];
	push @{$hash{$tmp[0]}->{2}},$tmp[2];
}
foreach $key(keys %hash){
	print $key,",";
	print join ",",@{$hash{$key}->{1}};
	print ",";
	print join ",",@{$hash{$key}->{2}};
	print "\n";
}


Last edited by summer_cherry; 05-08-2009 at 06:34 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

5. 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

6. 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

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. 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

9. 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

10. Shell Programming and Scripting

comparing values

i have two file and i am comparing both.. in cmp1 ,the content is : the nu of file is : <some integer value> in cmp2 ,the content is : the nu of file is : so want a script which will take value (2) when cmp1 is compared with cmp2.. i mean cmp cmp1 cmp2 the the output will be he nu of... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies
Login or Register to Ask a Question