Help converting column to row for multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help converting column to row for multiple files
# 1  
Old 06-20-2011
Help converting column to row for multiple files

Hi all,

I am pretty new at this so be gentle. Also, if there is any chance you could explain what the code you use is actually doing, that would really help me out, Im learning after all Smilie


So I am trying to convert a selected column of numbers from input file1 into a row in output file2

The input file looks like this

Quote:
104 "Congruent\blue_in_blue_1.bmp" 1037 1
104 "Congruent\blue_in_blue_1.bmp" 1225 1
104 "Congruent\blue_in_blue_2.bmp" 807 1
104 "Congruent\blue_in_blue_2.bmp" 960 1
104 "Congruent\blue_in_blue_3.bmp" 1061 1
104 "Congruent\blue_in_blue_3.bmp" 1214 1
104 "Congruent\blue_in_blue_3.bmp" 938 1
104 "Congruent\blue_in_blue_3.bmp" 940 1
104 "Congruent\blue_in_blue_3.bmp" 979 1
104 "Congruent\blue_in_blue_3.bmp" 994 1
... .... .... ..
Output should look like this
Quote:
1 1 1 1 1 1 1 1 1 1
I need to do this for roughly 200 files, and would like to concatenate each of them so that the final output looks like this

Quote:
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 0 1 1
1 0 0 1 1 0 1 1 1 1
Here is the code I have come up with so far.
Code:
#!/bin/sh

for condition in visual voice word
do
    while read subj
    do
        for row in `awk '{print $4}' ${condition}_${file}.txt`
           do
	       echo "$row" >> ${condition}_items.txt
    done		
	done <subjnumbers.txt
	     done

As you can see, my code is likely a mess, any assistance you can offer would be greatly appreciated.
# 2  
Old 06-20-2011
Code:
awk 'FNR==1{A[++a]=$4;next}{A[a]=A[a] OFS $4;next}END {while(A[++i]) print A[i]}' yourfile*.txt

---------- Post updated at 10:05 PM ---------- Previous update was at 10:04 PM ----------

Code:
[ctsgnb@shell ~/sand]$ ls f*.txt
f3.txt  f4.txt
[ctsgnb@shell ~/sand]$ cat f3.txt
104 "Congruent\blue_in_blue_1.bmp" 1037 1
104 "Congruent\blue_in_blue_1.bmp" 1225 1
104 "Congruent\blue_in_blue_2.bmp" 807 1
104 "Congruent\blue_in_blue_2.bmp" 960 1
104 "Congruent\blue_in_blue_3.bmp" 1061 1
104 "Congruent\blue_in_blue_3.bmp" 1214 1
[ctsgnb@shell ~/sand]$ cat f4.txt
204 "Congruent\blue_in_blue_2.bmp" 2037 2
204 "Congruent\blue_in_blue_2.bmp" 2225 2
204 "Congruent\blue_in_blue_2.bmp" 807 2
204 "Congruent\blue_in_blue_2.bmp" 960 2
204 "Congruent\blue_in_blue_3.bmp" 2062 2
204 "Congruent\blue_in_blue_3.bmp" 2224 2
[ctsgnb@shell ~/sand]$ awk 'FNR==1{A[++a]=$4;next}{A[a]=A[a] OFS $4;next}END {while(A[++i]) print A[i]}' f*.txt
1 1 1 1 1 1
2 2 2 2 2 2
[ctsgnb@shell ~/sand]$

---------- Post updated at 10:08 PM ---------- Previous update was at 10:05 PM ----------

if all your files have a fixed number of line (here : 6 lines) you can for example :

Code:
[ctsgnb@shell ~/sand]$ awk '{print $4}' f*.txt | xargs -n6
1 1 1 1 1 1
2 2 2 2 2 2
[ctsgnb@shell ~/sand]$

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 06-20-2011
Code:
#!/bin/sh

# Usage: SCRIPT FILE1 FILE2 ...

for f; do
    awk '{ printf $4 " "}' "$f"
    echo
done | sed 's/^ *//g; s/ *$//g'


Last edited by yazu; 06-20-2011 at 11:10 PM..
# 4  
Old 06-21-2011
Try this
Code:
 
perl -lane 'BEGIN{$,=" "}$arr[$i++]=$F[3]; if (eof){print @arr;@arr=();$i=0}' input1 input2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Row bind multiple csv files having different column headers

All, I guess by this time someone asked this kind of question, but sorry I am unable to find after a deep search. Here is my request I have many files out of which 2 sample files provided below. File-1 (with A,B as column headers) A,B 1,2 File-2 (with C, D as column headers) C,D 4,5 I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

2. Shell Programming and Scripting

Using loop command in UNIX for converting column to row

Dear folks I have 300 files which one of them are looking like: 1.SNP 0 0 1 0 I am looking for desire output: 1.SNP 0 0 1 0 I used this below command to run all of the 300 file at the same time for file in *.SNP; do awk '{printf( "%s ", $1 );} END {printf("\n");}' $file >... (1 Reply)
Discussion started by: sajmar
1 Replies

3. Shell Programming and Scripting

Converting a single row to multiple rows

Hi, I want to convert a single row values to multiple rows, but the no. of rows are not fixed. For example, I have a row as below abc-def-lmn-mno-xyz out put should be get abc get def get lmn get xyz (4 Replies)
Discussion started by: Suneel Mekala
4 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

Combining multiple column files into one with file name as first row

Hello All, I have several column files like this $cat a_b_s1.xls 1wert 2tg 3asd 4asdf 5asdf $cat c_d_s2.xls 1wert 2tg 3asd 4asdf 5asdf desired put put $cat combined.txt s1 s2 (2 Replies)
Discussion started by: avatar_007
2 Replies

6. Shell Programming and Scripting

converting column to row

Hi everyone.. I have a list of values in a file... 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 I am interested in converting this column to a row.. 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 can anyone pls help?? I am a liunx newbie.. Thanks.. (7 Replies)
Discussion started by: kjha
7 Replies

7. Shell Programming and Scripting

Converting Multiple rows to Single Row using unix commands

Can somebody help me in solving this.. Input data is like 0 A 1 B 2 C 3 D 0 A1 1 B1 2 C1 3 D1 0 A2 1 B2 2 C2 3 D2 Output should be like A B C D A1 B1 C1 D1 A2 B2 C2 D2 (7 Replies)
Discussion started by: Mahantesh Patil
7 Replies

8. Shell Programming and Scripting

Converting values in a ROW to COLUMN

Hi All, I needd to convert values in a row to a column. eg: Input is as: value1,value2,value3,value4,.........,value N Required Output: Value1 Value2 Value3 . . . Value N Please help.... (3 Replies)
Discussion started by: sambaman
3 Replies

9. Shell Programming and Scripting

Converting Single Column into Multiple rows

i have single column which is starting with same string(many number of rows) i have to convert each into a single row.how can i do that? laknar std mes 23 55 laknar isd phone no address amount 99 I have to convert above like below. laknar|std|mes|23|55 laknar|isd|phone... (3 Replies)
Discussion started by: laknar
3 Replies

10. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies
Login or Register to Ask a Question