Print for loop variable in output too

Hi,

This is my input file

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

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

the output will be something like

10 
20
30

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

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.

echo "$i \c" ; program $i | wc -l

Dear DGPickett,

Thanks for ur time.

But, how do I remove the column separators?

for i in `cat input` - Dangerous Backticks

Use while loop instead:

#!/bin/bash

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

Even simpler, just translate them to spaces:

while read line
do
        echo "$line \c"
        program ${line} | wc -l
done < input | tr '-:' '  '

Each tool does it's little, simple bit.

1 Like