Print for loop variable in output too


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print for loop variable in output too
# 1  
Old 02-19-2013
Print for loop variable in output too

Hi,

This is my input file

Code:
cat input
chr1:100-200
chr1:220-300
chr1:300-400

Now, I would like to run a program that will take each of the input record

Code:
for i in `cat input`; do program $i | wc -l;done

the output will be something like

Code:
10 
20
30

But, I would like to print the $i to my output. So, my final output will be

Code:
cat output
chr1 100 200 10
chr1 220 300 20
chr1 300 400 30

I tried echoing in the for loop, but it didn't work. Thanks in advance.
# 2  
Old 02-19-2013
Code:
echo "$i \c" ; program $i | wc -l

# 3  
Old 02-19-2013
Quote:
Originally Posted by DGPickett
Code:
echo "$i \c" ; program $i | wc -l

Dear DGPickett,

Thanks for ur time.

But, how do I remove the column separators?
# 4  
Old 02-19-2013
for i in `cat input` - Dangerous Backticks

Use while loop instead:
Code:
#!/bin/bash

while read line
do
        c=$( program ${line} | wc -l )
        line=${line/:/ }
        line=${line/-/ }
        echo "${line} ${c}"
done < input

This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-20-2013
Even simpler, just translate them to spaces:
Code:
while read line
do
        echo "$line \c"
        program ${line} | wc -l
done < input | tr '-:' '  '

Each tool does it's little, simple bit.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. Shell Programming and Scripting

Print loop output on same line dynamically

Hi, I am trying to print copy percentage completion dynamically by using the script below, #!/bin/bash dest_size=0 orig_size=`du -sk $sourcefile | awk '{print $1}'` while ; do dest_size=`du -sk $destfile | awk '{print $1}'` coyp_percentage=`echo "scale=2; $dest_size*100/$orig_size"... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

4. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

5. Shell Programming and Scripting

Use while loop - output variable

I'm very much a newbie and hence why this is going to be a stupid question. I'm attempting to create a korn shell script that pulls zone file locations and includes the copy command in the output. What? getzonedir.ksh #!/bin/ksh while read -r domain do ls */*"$domain" > $dir1 echo "cp... (5 Replies)
Discussion started by: djzah
5 Replies

6. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

7. UNIX for Dummies Questions & Answers

Need to get a loop variable from output of a command

Hi, I am trying to get a loop counter i and set its value as the ouput of a command: i=`printmo TEST1 | grep -i TEST2 | wc -l` Then I want to use i as counter to run a loop i number of times. Like if i gets a value of 5 I'll have to run loop 5 times. But will i here be a numeric... (3 Replies)
Discussion started by: pat_pramod
3 Replies

8. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

9. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

10. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies
Login or Register to Ask a Question