10

I want to multiply column 1 with column 2 (till end of file) in input file and should output 1 column and multiplied 3 column in separate file.

input.txt :

1 677679866
2 121867616
3 49413198
4 40415982

output.txt :

1 677679866
2 243735232
3 148239594
4 161663928
1
  • Is there a question here?
    – mikeserv
    Apr 22, 2014 at 2:01

2 Answers 2

20
awk '{ print $1, $1 * $2 }' input.txt > output.txt
0
4

Here is an awk solution:

$ awk '$0=$1" "$1*$2' input.txt 
1 677679866
2 243735232
3 148239594
4 161663928

But you can do it many ways, here is a perl solution:

$ perl -ape 's/$F[1]/$F[0]*$F[1]/e' input.txt
1 677679866
2 243735232
3 148239594
4 161663928

You can make change inplace, by using -i option:

$ perl -i.bak -ape 's/$F[1]/$F[0]*$F[1]/e' input.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .