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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-27-2011, 01:59 PM
|
#1
|
LQ Newbie
Registered: Jul 2010
Posts: 22
Rep:
|
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
|
|
|
03-27-2011, 03:32 PM
|
#2
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
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.
|
03-27-2011, 04:04 PM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
03-27-2011, 07:52 PM
|
#4
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
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.
|
03-28-2011, 01:09 AM
|
#5
|
LQ Newbie
Registered: Jul 2010
Posts: 22
Original Poster
Rep:
|
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
|
|
|
03-28-2011, 02:15 AM
|
#6
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
Quote:
Originally Posted by ilukacevic
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.
|
03-29-2011, 02:48 PM
|
#7
|
LQ Newbie
Registered: Jul 2010
Posts: 22
Original Poster
Rep:
|
Quote:
Originally Posted by kurumi
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
|
|
|
All times are GMT -5. The time now is 06:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|