Loop question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop question
# 1  
Old 10-05-2013
Loop question

Hi,

I would really appreciate some help with manipulating a file.
Here is my input file:

Code:
SNP_1	:	        1		SNP_11	:	        1
SNP_2	-0.12	        1		SNP_12	-0.3472	1
SNP_3	-0.708	1		SNP_13	0.5037	1
SNP_4	1.492	        0.5		SNP_14	-0.0685	1
SNP_5	1.27	        0.5		SNP_15	0.547	        1
SNP_6	1.832	        1		SNP_16	0.538	        0.5
SNP_7	-0.882	0.5		SNP_17	-1.0781	1
SNP_8	1.047	        0.5		SNP_18	-0.1406	1
SNP_9	1.201	        0.5		SNP_19	-0.3987	1
SNP_10	0.877	        1		SNP_20	0.2784	1
	        0.683				                0.4642

Here is the output I need:

Code:
SNP_1 -0.12
SNP_2 -0.708
SNP_3 1.492
SNP_4 1.27
etc
SNP_11 -0.3472
SNP_12 0.5037
etc

I don't really care about the order. Just need to have two columns where the first one is the columns of SNP_... and the second one is the z-score.
The first problem is that the z-score column is shifted down by one. Every 4th column is empty. Then I don't need every 3rd and 4th column, I need columns 1,2, 5,6, 9,10etc.
So, first I've tried to create 2 files with every 5th and 6th columns:

the file with every 5th column (starting with the 1st):
Code:
for((i=1;i<=7;i+4));
do
cut -f i in file > outfile1
done

the file with every 6th column (starting with 2nd):
Code:
for((i=2;i<=7;i+4));
do
cut -f i in file > outfile2
done

then outfile1 will look like this:

Code:
SNP_1	SNP_11
SNP_2	SNP_12
SNP_3	SNP_13
SNP_4	SNP_14
SNP_5	SNP_15
SNP_6	SNP_16
SNP_7	SNP_17
SNP_8	SNP_18
SNP_9	SNP_19
SNP_10	SNP_20

outfile2 will look like this:
Code:
:	         :
-0.12  	-0.3472
-0.708	0.5037
1.492 	-0.0685
1.27	        0.547
1.832	        0.538
-0.882	-1.0781
1.047	        -0.1406
1.201  	-0.3987
0.877	         0.2784
0.683	         0.4642

then I am thinking to delete the first line of outfile2:

Code:
sed '1d' outfile2 > outfile3

then I will print all columns in one for each file:

Code:
(cat outfile1) | xargs -n 1 > outfile4

(cat outfile3) | xargs -n 1 > outfile5

and, finally, I combine the two files:

Code:
paste outfile4 outfile5 > result

The problem is that the for loop with cut doesn't work. For some reason it keeps on complaining that there is illegal list value:

Code:
cut: [-cf] list: illegal list value

Any ideas how to solve this?

Many thanks for your time in advance!

---------- Post updated at 09:21 PM ---------- Previous update was at 06:36 PM ----------

it seems that the for loop with cut should look rather like this:

Code:
for((i=1;i<=11;i+=4)); do cut -f$i infile > outfile1; done
for((i=2;i<=11;i+=4)); do cut -f$i infile > outfile2; done

The problem is that is overwrites the columns and I end up having just the last column in my outfiles. Perhaps I should output into an array?

---------- Post updated at 09:54 PM ---------- Previous update was at 09:21 PM ----------

OK, I was able to make the initial file to work with like this:

Code:
SNP_4981	-0.682	1		SNP_4991	0.688	1
SNP_4982	-1.884	0.5		SNP_4992	-0.242	0.5
SNP_4983	-0.151	0.5		SNP_4993	0.768	0.5
SNP_4984	-2.069	0.5		SNP_4994	0.992	1
SNP_4985	0.596	1		SNP_4995	0.646	0.5
SNP_4986	-1.654	0.5		SNP_4996	-1.663	0.5
SNP_4987	1.718	0.5		SNP_4997	-0.367	0.5
SNP_4988	-2.137	0.5		SNP_4998	-1.328	0.5
SNP_4989	-1.573	0.5		SNP_4999	0.509	1
SNP_4990	-2.563	0.5		SNP_5000	-1.231	0.5

and then I can get my output using these commands:

Code:
awk '{for(i=2;i<=NF;i+=3){print $i}}' test > test2
awk '{for(i=1;i<=NF;i+=3){print $i}}' test > test1
paste test1 test2 > result

the output looked like this:

Code:
SNP_4981        -0.682
SNP_4991        0.688
SNP_4982        -1.884
SNP_4992        -0.242
SNP_4983        -0.151
SNP_4993        0.768
SNP_4984        -2.069
SNP_4994        0.992
SNP_4985        0.596
SNP_4995        0.646
SNP_4986        -1.654
SNP_4996        -1.663
SNP_4987        1.718
SNP_4997        -0.367
SNP_4988        -2.137
SNP_4998        -1.328
SNP_4989        -1.573
SNP_4999        0.509
SNP_4990        -2.563
SNP_5000        -1.231

Many thanks for your time!

Last edited by Don Cragun; 10-06-2013 at 12:08 AM.. Reason: Changed lots of HTML and ICODE tags to CODE tags.
# 2  
Old 10-06-2013
The immediately obvious problem is that the string i is not a numeric value. Try changing:
Code:
cut -f i in file > outfile2

to:
Code:
cut -f $i in file > outfile2

I would also guess that you meant to have the single file infile as an input file instead of the two files in and file. If that is correct, what you probably wanted is:
Code:
cut -f $i infile > outfile2

You have a few lines like this with different output files. They all need to be fixed.
# 3  
Old 10-06-2013
Code:
awk '{
	if (NR==1) F=NF;
	else { n=0;
		for (i=1;i<=NF;i++)
			if (NF!=F) print a[++n] " " $i;
			else if ((i-2)%3==0) print  a[++n] " " $i;
	} n=0;
	for (i=1;i<=NF;i++)
		if ((i-1)%3==0) a[++n]=$i;
}' file

# 4  
Old 10-06-2013
Thank you for the comments and suggestions!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question about for loop

Hi, I have code like below disk_list=$(ls /root/file1) for disk in $disk_list do pvcreate $i done I know what pvcreate command does, but I do not understand what this $i do here. can someone please explain. (2 Replies)
Discussion started by: stew
2 Replies

2. Shell Programming and Scripting

While loop Question

Hi, I have a requirement where I have to check for 10 files in Unix location. If all of the files present in that directory then i have execute another process. can you help me resolving this issue. sample location /home/usr abc.txt cde.txt aaaa.txt lll.txt ooo.txt if all the... (3 Replies)
Discussion started by: manasvi24
3 Replies

3. Shell Programming and Scripting

For loop question

I have two files. In file one, there are many columns, but only two of interest to me. Column 1 contains a list of individuals, defined by an ID number. Column 10 contains the diagnosis that each individual has (I am a physician). All together, there are 3000 lines in this file, one line per... (2 Replies)
Discussion started by: awc228
2 Replies

4. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

5. Shell Programming and Scripting

For Loop Question

I am struggling with the for loop. I have a file name heros.txt and I would like to go through a list in file where.txt and see if I can find the name from where inside heros. One of the problems that I am having is I dont understand how to setup the for loop to find the list to search.:wall: ... (6 Replies)
Discussion started by: captaindoogles
6 Replies

6. Shell Programming and Scripting

loop question

hey guys what im trying to do is do a simple script that will ask for a password and on the 5th time it says access denied if the right password is still not entered this is what i have so far can anyone help me im not good with scripting thanks in advance #!/bin/bash secretname=secret... (2 Replies)
Discussion started by: randallrivy11
2 Replies

7. Shell Programming and Scripting

For Loop Question

I'm improving the way an existing script handles arrays, but the results aren't what I had in mind: e="Too many consecutive errors... System is probably unstable!" e="Cancelable Timer Wait Failed!" for errcd in ${e} do echo ${errcd} done The for loop interprets the spaces... (2 Replies)
Discussion started by: ironhalo
2 Replies

8. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

loop question

HI ALL How to read first 10 line from a file using any method (5 Replies)
Discussion started by: aliahsan81
5 Replies

10. Shell Programming and Scripting

Loop question

hi, how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen? thanks eg: #!/bin/sh FILE="somefile.txt" text_line="" what kind of loop would use here? (18 Replies)
Discussion started by: strike
18 Replies
Login or Register to Ask a Question