# Thread: Working with columns III

1. Registered User
Join Date
Feb 2004
Posts
52

## Unanswered: Working with columns III

Hi !

Given a NxM matrix, let's say

M columns
N lines 0 1 1 ... 1
1 0 1 ... 1
1 1 0 ... 0
... ...
0 1 1 ... 1

I need to multiply each number by its column number, ie,

0 2 3 ... M
1 0 3 ... M
1 2 0 ... 0
... ...
0 2 3 ... M

Any help really appreciated !

Serg

Join Date
Jun 2002
Location
UK
Posts
525
You should be able to do this yourself by now ;-)

Code:
`awk '{for (i=1;i<=NF;i++){printf "%d"(i==NF?"\n":OFS),\$i*i}}' inputFile`

3. Registered User
Join Date
Apr 2004
Location
Boston, MA
Posts
325
Thanks again, Vlad and Damian !

I am learning a lot with your scripts !!!
Serg,
give it a try and we'll give you a hand if you get stuck.

Join Date
Jun 2002
Location
UK
Posts
525
Originally Posted by vgersh99
Serg,
give it a try and we'll give you a hand if you get stuck.
Oops! That's what I should have said.

5. Registered User
Join Date
Feb 2004
Posts
52
Thanks guys !

Usually I stuck in the syntax...things like i==NF?"\n":OFS are
not well digested yet...but I am learning.
The reason why I do not put my wrong trials is simple.
Since these forums are for open discussions, I chose not to let
people bored about my mistakes...and from the number of viewers
in these posts I believe this works...

Thanks again !!

Join Date
Jun 2002
Location
UK
Posts
525
i==NF?"\n":OFS

if <condition is true> then <value1> else <value2>

7. Registered User
Join Date
Feb 2004
Posts
52
Well, this is one example about getting stuck...
I need to run the awk command a number of times. I build
a FOR loop like this

for (( i = 800; i <= 50000; i+=100 )) ### Outer for loop ###
do
echo ""
printf " awk '{for (k=1;k<=NF;k++){printf \"%d\"(k==NF?\"\n\":OFS),\\$k*k}}' \$i.turn > \$i.matrix"
done

I added some "\" in the original awk line but it did not work
because the awk script contains this command \n.
How can I keep using \n in this case ? I tried double ( \\n ) but it did not
work either...

Thanks again,

Serg

8. Registered User
Join Date
Feb 2004
Posts
52
Thanks Damian !

9. Registered User
Join Date
Apr 2004
Location
Boston, MA
Posts
325
can't you simply do this:

Code:
```for (( i = 800; i <= 50000; i+=100 )) ### Outer for loop ###
do
echo ""
awk '{for (k=1;k<=NF;k++){printf "%d" (k==NF) ? "\n" : OFS, \$k*k}}' \$i.turn > \$i.matrix
done```

10. Registered User
Join Date
Feb 2004
Posts
52
Living and learning...

Thanks !

11. Registered User
Join Date
Feb 2004
Posts
52
Hi guys.

I tried to run your script but I just get
blank lines. I looked at the script and
tried to debug but I did not find anything wrong.

It's is doing something but the final result
is not being printed.

Thanks again,

Serg

12. Registered User
Join Date
Feb 2004
Posts
52
I found the problem.

Thanks.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•