Adding number in one column

Hello

I have something like this

a 1
b 1 
c 1
d 1
e 1

This is inside 1.dat

and this is what I am trying to get

a 2
b 2
c 2
d 2
e 2

and write this as 2.dat

How can I do this with foreach loop to 100;

a 100
b 100
c 100
d 100
e 100

100.dat

Thank you!

try:

for i in {2..100}
do
   rm -f $i.dat
   while read a b
   do
      b=$(( b + i - 1 ))
      echo $a $b >> $i.dat
   done < 1.dat
done
1 Like

---------- Post updated at 01:59 PM ---------- Previous update was at 01:33 PM ----------

Thank you

Does it work in csh script?

I think that foreach command works in csh script

1 Like

csh example:

#!/bin/csh

set i=2
set lines="`cat 1.dat`"
while ( $i <= 100 )
   rm -f $i.dat
   set l=1
   while ( $l <= $#lines )
      set arr = ( $lines[$l] )
      @ arr[2] = $arr[2] + $i - 1
      echo $arr[1] $arr[2] >> $i.dat
      @ l = $l + 1
   end
   @ i = $i + 1
end
1 Like

Like you did with the input, you can redirect the output

for i in {2..100}
do
   while read a b
   do
      b=$(( b + i - 1 ))
      echo "$a $b"
   done < 1.dat > $i.dat
done

This is far more efficient than each time open-append-close.
(The csh does not have this capability.)