LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-27-2011, 01:59 PM   #1
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Rep: Reputation: 0
using for loop in awk


Dear all,

my situation is following: I would like to use the for loop (not necessarily for loop) with awk command in such a way that awk doesn't evaluate first the first row through all the iterations of the loop, but to evaluate all the rows for the first iteration, then all the rows for the second iteration, and so on.

For example, if I have a file like

2 3
4 5

and I want to print $1,$2,$1*t,$2*t, for the values of t from 1 to 3, I would like to get

2 3 2 3
4 5 4 5
2 3 4 6
4 5 8 10
2 3 6 9
4 5 12 15

and not

2 3 2 3
2 3 4 6
2 3 6 9
4 5 4 5
4 5 8 10
4 5 12 15

what I always get with my tries. Two of my tries are

awk '{ for (t=1; t<=2; t++) print $1,$2,$1*t,$2*t}' proba.dat > proba1.dat

awk '{ t=1
while (t<=5) {
print $1,$2,$1*t,$2*t
t++
}
}' proba.dat > proba2.dat



Does anyone have a solution to my problem? I would appreciate any hints and help.

Thank you in advance!

Yours,
Igor
 
Old 03-27-2011, 03:32 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
You'll need to read the input into an array, and do the output in a loop at end:
Code:
awk '{ line[NR] = $0 }
 END { for (t = 1; t <= 3; t++)
           for (i = 1; i <= NR; i++) {
               printf("%s", line[i])
               n = split(line[i], field)
               for (k = 1; k <= n; k++)
                   printf(" %g", t * field[k])
               printf("\n")
           }
     }' proba.dat > proba1.dat
Does this solve your problem?
 
1 members found this post helpful.
Old 03-27-2011, 04:04 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
There are different ways to perform a loop using awk. You can rewind the file to parse it multiple times, or you can pass the file name as argument multiple times. Some options are covered in this thread, see in particular post #2 and post #6. The suggestion by Nominal Animal is good, unless the file size is huge.
 
Old 03-27-2011, 07:52 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
I would recommend using a tool that can seek and tell a file (Python|Perl|Ruby etc) . an example in Ruby
Code:
#!/usr/bin/env ruby 
# Ruby1.9+
f=File.open('file')
1.upto(3) do |i|
    while not f.eof
        s = f.readline.split.map(&:to_i)
        puts "#{s[0]} #{s[1]} #{s[0]*i} #{s[1]*i}"
    end
    f.pos = 0 # goes back to start of file...
end
f.close
example run
Code:
$ ruby myscript.rb
2 3 2 3
4 5 4 5
2 3 4 6
4 5 8 10
2 3 6 9
4 5 12 15
 
1 members found this post helpful.
Old 03-28-2011, 01:09 AM   #5
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Dear Nominal Animal and kurumi,

both of your solution work well. Thank you both!

The only issue I have is with the ruby script...I don't know how to pipe the output to a file. I'll try to find it on the web.

Yours,

Igor
 
Old 03-28-2011, 02:15 AM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by ilukacevic View Post

The only issue I have is with the ruby script...I don't know how to pipe the output to a file. I'll try to find it on the web.

Yours,

Igor
you can pipe the file when you run the script because there is a command called puts (print to screen)
Code:
$ ruby myscript.sh > new file
Or you can do it inside the script itself by opening output file handle. ( You can check it out yourself on how to do it)
 
1 members found this post helpful.
Old 03-29-2011, 02:48 PM   #7
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kurumi View Post
you can pipe the file when you run the script because there is a command called puts (print to screen)
Code:
$ ruby myscript.sh > new file
Or you can do it inside the script itself by opening output file handle. ( You can check it out yourself on how to do it)

OK. Thanks, again!

Igor
 
  


Reply

Tags
awk, loop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use awk in a for loop? cliffyao Programming 2 10-27-2010 10:51 PM
[SOLVED] awk for-loop babaqga Linux - Software 4 09-02-2010 03:57 AM
for loop in awk command jssidhu4 Programming 7 03-31-2010 04:19 AM
awk loop related kalyanofb Programming 3 02-16-2007 02:31 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 01:34 PM


All times are GMT -5. The time now is 12:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration