If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Working with columns III

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-04, 08:44
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Working with columns III

Hi !

Yet another question about columns...

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
Reply With Quote
  #2 (permalink)  
Old 06-18-04, 09:12
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
  #3 (permalink)  
Old 06-18-04, 09:14
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Quote:
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.
Reply With Quote
  #4 (permalink)  
Old 06-18-04, 09:15
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
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.
Reply With Quote
  #5 (permalink)  
Old 06-18-04, 11:43
Serg Serg is offline
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 !!
Reply With Quote
  #6 (permalink)  
Old 06-18-04, 11:47
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
i==NF?"\n":OFS

if <condition is true> then <value1> else <value2>
Reply With Quote
  #7 (permalink)  
Old 06-18-04, 11:59
Serg Serg is offline
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
Reply With Quote
  #8 (permalink)  
Old 06-18-04, 12:00
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Thanks Damian !
Reply With Quote
  #9 (permalink)  
Old 06-18-04, 14:49
vgersh99 vgersh99 is offline
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
Reply With Quote
  #10 (permalink)  
Old 06-18-04, 21:22
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Living and learning...

Thanks !
Reply With Quote
  #11 (permalink)  
Old 06-24-04, 15:26
Serg Serg is offline
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
Reply With Quote
  #12 (permalink)  
Old 06-24-04, 15:29
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
I found the problem.

Thanks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On