For cycle and formatting stdout


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For cycle and formatting stdout
# 1  
Old 07-09-2015
For cycle and formatting stdout

Dear all,

I want to create a table using a shell script. Hope someone can help.

I created a variable that contains the path for different files.
For all the files I want to do calculations and print it to stdout (or file) as a table.

I tried this code:
Code:
paths=`cat $tabdelim_file | awk '{print $2}' 
names=`cat $tabdelim_file | awk '{print $1}' 

echo -e "header1\theader2\theader3\theader4\theader5"

for i in $path
do
wc -l $i | awk '{print $1}'
awk '{print $3-$2}' $i | awk '{sum+=$1} END {print sum, sum/NR, (sum/2844551878)*100}'
# To header5 I want to print the name for the $i-th tag from $names - dont know how to do it
done

However the output is shifted like:
Code:
header1\theader2\theader3\theader4\theader5
cmd1_out for first element $i
cmd2_out (generates 3 values for header2-4) for first element $i
cmd1_out for second element $i
cmd2_out (generates 3 values for header2-4) for second element $i
etc

How can I create a desired output?
Code:
header1\theader2\theader3\theader4\theader5
cmd1_out\tcmd2_out1\tcmd2_out2\tcmd2_out3\tcmd3_out1\t
cmd1_out\tcmd2_out1\tcmd2_out2\tcmd2_out3\tcmd3_out1\t

Thank you for the answer,

Regards,
Laszlo
# 2  
Old 07-09-2015
Not only am I not understanding what you require but am missing something...
Where is the path variable?
Should the line be:
for i in $paths ?
Please supply valid input data, OS, shell, etc...
TIA
# 3  
Old 07-09-2015
Sorry for confusion.

I am using #!/bin/sh on unix enviroment and you are rigth its

Code:
for i in $paths

Basicly I have a list file with two columns. The first is an ID the second is a path to the file.

I want to create a table (tab separated) like this filled with the information obtained in the for cycle:

Code:
Number_of_rows Sum Average % ID

The calculations are ok, but i don't know how to format the results to a table.
# 4  
Old 07-09-2015
Please post input and output samples, and the logics/algorithms that connect them. What you say in post#3 doesn't match your desired output from post#1.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-09-2015
Example $tabdelim_file (tabdelimited):

Code:
ID1 path_to_file1
ID2 path_to_file2
IDn path_to_filen

Example path_to_file1 file (tabdelimited):
Code:
node1 7990338 7990408
node2 16847079 16847153
noden <integer> <integer>

Actual output:
Code:
Number of element       Coverage in bp  Average Width   %         ID
611
45690 74.7791 0.00160623
1699
3030247 1783.55 0.106528

Desired output (tabdelimited or fit number under header):
Code:
Number of element       Coverage in bp  Average Width   %         ID
611 45690 74.7791 0.00160623 ID1
1699 3030247 1783.55 0.106528 ID2

# 6  
Old 07-09-2015
Unfortunately, although asked twice to become more precise and detailed in your specification, you still leave a wide field open for guesses from our side...

As a first hint to achieve your desired output, change
Code:
wc -l $i | awk '{print $1}'

to
Code:
wc -l $i | awk '{printf "%d\t", $1}'

.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 07-09-2015
This is a guess based on my understanding of your posts. Maybe there's enough information in it for you to fix what you want. Or perhaps, you can come back with more information about where it fails to meet your request.

Code:
printf "header1\theader2\theader3\theader4\theader5\n"
while read id fname; do
    awk -v id="$id" '{sum+=($3-$2)} END{print NR, sum, sum/NR, (sum/2844551878)*100, id}' OFS="\t" "$fname"
done < $tabdelim_file


Last edited by Aia; 07-09-2015 at 05:24 PM..
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cycle for with for-then-else

Hi, i would like to insert a if-then-else function in to cycle for -------------- cat test -------------- # cat test ALFA BETA GAMMA ----------------------- This is my script: #!/bin/bash for i in $(cat test); if ; then echo "ok" else (5 Replies)
Discussion started by: elilmal
5 Replies

2. Shell Programming and Scripting

for cycle question

i have a question how to modify below script to generate the expect result below : test.sh #!/bin/bash for ((i=0; i < 25; i++)) do echo $1$i done current result: test.sh 20090101 200901010 200901011 200901012 200901013 200901014 200901015 200901016 200901017 200901018 (2 Replies)
Discussion started by: bleach8578
2 Replies

3. Shell Programming and Scripting

for cycle

Hello, I have a question: is there a way to have a "for" cycle done a certain number of times. For example in c++ I can do this: for (i=o;i<10;i++) and the cycle will be repeated 10 times. in UNIX for example I do this: for i in `cat /etc/host` do done and the cycle will be repeated... (6 Replies)
Discussion started by: jcpetela
6 Replies

4. Programming

clock cycle count

Hello everybody! Is there a way to count the clock cycles (that a program took to finish) in C? thanx:o (5 Replies)
Discussion started by: nicos
5 Replies

5. Shell Programming and Scripting

wildcard in a if cycle

hello everybody, I need help on putting a wildcard match inside an if condition (I'm using korn shell): if ] then echo ' ' echo ''$MYSEL' is not a correct option' echo ' ' else ..... i tried also #if -ne "``" and a lot of combinations of `"' but I didn't find the... (2 Replies)
Discussion started by: elionba82
2 Replies

6. Shell Programming and Scripting

For cycle

Hello, I have files in a dir. I what to create a FOR cycle that will do this FOR <condition> do file=`ls <directory> | tail -1` echo $file mv -f $file <another dir> done What I want to now is what should I put in the <condition>. The condition I want is that the FOR will execute... (3 Replies)
Discussion started by: nagomes
3 Replies

7. Shell Programming and Scripting

shell cycle

Hello I got a cycle in the script which open another scripts. if then action fi Scripts action will be running 2 times at the same time. Inside of action() is insert into the table. But what I want is that only first script can do insert into table. So how to do... (2 Replies)
Discussion started by: mape
2 Replies
Login or Register to Ask a Question