Adding number in one column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding number in one column
# 1  
Old 03-16-2016
Adding number in one column

Hello

I have something like this

Code:
a 1
b 1 
c 1
d 1
e 1

This is inside 1.dat

and this is what I am trying to get

Code:
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;

Code:
a 100
b 100
c 100
d 100
e 100

100.dat

Thank you!

Last edited by Scrutinizer; 03-16-2016 at 03:16 PM.. Reason: code tags
# 2  
Old 03-16-2016
try:
Code:
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

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-16-2016
---------- Post updated at 01:59 PM ---------- Previous update was at 01:33 PM ----------

Quote:
Originally Posted by rdrtx1
try:
Code:
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


Thank you

Does it work in csh script?

I think that foreach command works in csh script
This User Gave Thanks to jeo_fb For This Post:
# 4  
Old 03-16-2016
csh example:
Code:
#!/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

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 03-16-2016
Quote:
Originally Posted by rdrtx1
try:
Code:
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

Like you did with the input, you can redirect the output
Code:
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.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

2. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

3. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

5. Shell Programming and Scripting

Adding a new column as sequential number but with a little complication

I am a newbie to shell programming and maybe somebody can help me out a little. Here's my problem: I got a PIPE delimited file with header record. I need to add a new column name as RECORDKEY. I would like to use a counter to generate this new value for each record. I plan to do a while loop and... (4 Replies)
Discussion started by: johnhips
4 Replies

6. Shell Programming and Scripting

Taking largest (negative) number from column of coordinates and adding positive form to every other

Hello all, I'm new to the forums and hope to be able to contribute something useful in the future; however I must admit that what has prompted me to join is the fact that currently I need help with something that has me at the end of my tether. I have a PDB (Protein Data Bank) file which I... (13 Replies)
Discussion started by: crunchgargoyle
13 Replies

7. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

8. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

9. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question