-2

I have input file like::: a.txt

1  0.4 0.8
2  0.5 3.0
5  0.8 3.5

I want output like this::: 1.txt

1 3 0.4 0.8
2 3 0.5 3.0
5 3 0.8 3.5

1 8 0.4 0.8
2 8 0.5 3.0
5 8 0.8 3.5

I want to add same number in second column.in loop how can i do this??

  • 1
    Where did the numbers 3 and 8 in the second column come from? – choroba Jul 13 '18 at 11:25
  • this is not 3,itz 5 only by mistake it happen. – claudia smith Jul 13 '18 at 11:28
  • i want a loop like this, which can add number that is given in loop let say for ((i=1; i<=10;i++))..... so that i = whatever number should come in coulmn second. like I have added 3 and 8 – claudia smith Jul 13 '18 at 11:28
  • 1
    please edit your question for additionnal information, comment are not indented and hard to read. – Archemar Jul 13 '18 at 12:12
0

You can also use awk to get the expected output,

for i in {3,8}; do awk -v a=$i '{print $1,a,$2,$3}' a.txt; echo "" ; done >  1.txt

Explanation:

Here -v option (var=value) is used to pass any variable to awk

print {$1,a,$2,$3} , here a is the variable that you want in the second column, the rest all columns are printed as they are

1

You need three nested loops

for n in 3 8 ; do
    while read -a cols ; do
        printf '%s %s' "${cols[0]}" $n
        for ((i=1; i<=${#cols[@]}; i++)) ; do
            printf ' %s' "${cols[i]}"
        done
        echo
    done <a.txt
    echo
done > 2.txt

You can also use sed:

for n in 3 8 ; do
    sed "s/ / $n /" a.txt
    echo
done > 1.txt
  • for ((i=2000; i<=2000; i++)) do for ((j=9; j<=9; j++)) do awk '{print $1, $j, $2}' triplet_mlecule_number-"$i"-"$j".ndx > insert-"$i"-"$j".txt done done hey.... I want to print this j in loop in second column, it can be any number in j. – claudia smith Jul 13 '18 at 11:50
  • @claudiasmith you can use -v option to use any variable defined outside awk, in your case , just assign j to a local variable of awk by using -v option for ((i=2000; i<=2000; i++)) do for ((j=9; j<=9; j++)) do awk -v a=$j '{print $1, a, $2}' triplet_mlecule_number-"$i"-"$j".ndx > insert-"$i"-"$j".txt I have included an example in my answer too, check if it solves your problem – Arushix Jul 13 '18 at 12:14
  • Do you really want to iterate from 2000 to 2000 and from 9 to 9? – choroba Jul 13 '18 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.