So I converted columns to rows but I want it to be tab delimited and also I want.....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting So I converted columns to rows but I want it to be tab delimited and also I want.....
# 1  
Old 06-11-2009
So I converted columns to rows but I want it to be tab delimited and also I want.....

Hi,

So my file looks like this:

title number
JR 2
JR 2
JR 4
JR 5
NM 5
NM 8
NM 2
NM 8

I used this line that I wrote to convert it to rows so it will look like this:
awk -F"\t" '!/^$/{a[$1]=a[$1]" "$3} END {for ( i in a) {print i,a[i]}}' occ_output.tab > test.txt

JR 2 2 4 5
NM 5 8 2 8

The problem is that the spacing is not tab delimited. I am missing something really obvious...

The next thing I want to do is bin the rows... lets say I wanted to bin every second value and average them.

so it will look like this:

JR 2 4.5
NM 6.5 5

Anyone know a quick liner on how to do that?

thanks
# 2  
Old 06-11-2009
do the averaging in your awk's END{} loop
# 3  
Old 06-11-2009
for first question
Code:
 awk '!/^$/{a[$1]=a[$1]"      "$2} END {for ( i in a) {print i,a[i]}}' filename

for second question
Code:
awk '!/^$/{var=$2}{getline}{a[$1]=a[$1]"      "(var+$2)/2}END{for ( i in a) {print i,a[i]}}' filename

# 4  
Old 06-11-2009
Hey thanks so much! I knew i was missing someting small... the second one works but it does not bin... so I want to take teh first two then average... not divide the values by 2.

How would I do that?

awk '!/^$/{var=$2}{getline}{a[$1]=a[$1]" "(var+$2)/2}END{for ( i in a) {print i,a[i]}}' filename


so the output lookslike this now

JR 2 2 4 5

I want it so take the the average of every two values (so the average of 2 +2 and then the average of 4+5). Now the output will look like this..

JR 2 4.5

thanks

Last edited by kylle345; 06-11-2009 at 11:35 PM..
# 5  
Old 06-12-2009
as long as your file is ordered file, it is ok to use below. Otherwise, you may need to order it first then use below

Code:
cat a.txt | paste -d" " - - - - | awk '{print $1" "($2+$4)/2" "($6+$8)/2}'

or you may use below perl code in case of your file is a mass

Code:
sub bin{
	my @tmp=@{$_[0]};
	my $result=[];
	for(my $i=0;$i<=($#tmp+1)/2-1;$i++){
		push @{$result}, ($tmp[$i*2]+$tmp[$i*2+1])/2;
	}
	return $result;
}
while(<DATA>){
	my @tmp=split;
	push @{$hash{$tmp[0]}}, $tmp[1];
}
foreach my $key (keys %hash){
	print $key," ";
	my $r_arr=bin($hash{$key});
	print join " ", @{$r_arr};
	print "\n";
}
__DATA__
JR 2
JR 4
NM 5
NM 8
JR 2
NM 2
JR 5
NM 8


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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort tab delimited file according to which rows have missing values

Hello! I have a tab delimited file with values in three columns. Some values occur in all three columns, other values are present in only one or two columns. I would like to sort the file so that rows with no missing values come first, rows with one missing values come next, and rows with two... (9 Replies)
Discussion started by: MBarrett1213
9 Replies

2. Shell Programming and Scripting

Delete and insert columns in a tab delimited file

Hi all , I have a file having 12 columns tab delimited . I need to read this file and remove the column 3 and column 4 and insert a word in column 3 as "AVIALABLE " Is there a way to do this . I am trying like below Thanks DJ cat $FILENAME|awk -F"\t" '{ print $1 "\t... (3 Replies)
Discussion started by: Hypesslearner
3 Replies

3. Shell Programming and Scripting

Remove blank columns from a tab delimited text file

Hello, I have some tab delimited files that may contain blank columns. I would like to delete the blank columns if they exist. There is no clear pattern for when a blank occurs. I was thinking of using sed to replace instances of double tab with blank, sed 's/\t\t//g' All of the examples... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

4. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

5. UNIX for Advanced & Expert Users

merge two tab delimited file with exact same number of rows in unix/linux

Hi I have two tab delimited file with different number of columns but same number of rows. I need to combine these two files in such a way that row 1 in file 2 comes adjacent to row 1 in file 1. For example: The content of file1: field1 field2 field3 a1 a2 a3 b1 b2 b3... (2 Replies)
Discussion started by: mary271
2 Replies

6. UNIX for Dummies Questions & Answers

How to echo space or tab delimited values into rows?

Hi, I have the following code: LIST=`ls | grep '.sql$'` echo $LIST The above code will give me something like.. file1.sh file2.sh file3.sh file4.sh file5.sh I want to display the values into rows using echo like... file1.sh file2.sh (5 Replies)
Discussion started by: adshocker
5 Replies

7. UNIX for Dummies Questions & Answers

Deleting columns from a tab delimited text file?

I have a tab limited text file with 10000+ columns. I want to delete columns 6 through 23, how do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Converted repeated rows into splitted columns

Dear Friends, I have an input file contains lot of datas, which is like repaeated rows report. The output file need to have column wise report, rather than row-wise. Input File random line 1 random line 2 random line 3 ------------------------------------- Start line 1.1 (9.9) ... (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

9. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

10. Shell Programming and Scripting

Reading columns in tab delimited file

I want to read only one column in "|" delimited file and write that column to a new file. For Ex: Input File 1|abc|324|tt 2|efd|11|cbcb 3||1|fg 4|ert|23|88 Output : I want to read column 3 in diff file. 324 11 1 88 Can anyone give me inputs on this ? (2 Replies)
Discussion started by: net
2 Replies
Login or Register to Ask a Question