Run out of ideas. Help?

October 4, 2010 at 19:22:56
Specs: Windows 7
Lately I've been trying to code up a script in c shell whose general purpose is to extract some data from a selection of files and output it to a new file in a certain format. Mostly I've got it sorted, but there's just one small task which I can't figure out how to code up, so I was wondering if anyone could offer some suggestions on how to go about this. Just ideas are enough. You don't need to code it up unless you really want to. :)

Essentially I want to extract the 3rd, 8th, 13th, etc lines from the specified file until the end of the file is reached, then output all these lines to my output file on the one line. Input file will typically be 6, 11, 16, etc lines long.

What I've got so far:

# defines max as the number of lines on the read file
set max = `wc -l xx01`
# extracts mth line
set m = 3
while (m < $max)
set line0_??? = `awk 'FNR==${m}' xx01` # incorrect code - not fixed yet
@ m = $m + 5
end
set line = (line0_1 line0_2 ... line0_n)

If I attack the problem in this manner my main issue is that I can't figure out how to set n variables then set these as an array. I can do this if I know what n is, but not in general terms.

If I can get some suggestions as to how I might accomplish this, either by building on what I've got or starting again from scratch, I'd really appreciate it.

Thanks in advance.

guest


See More: Run out of ideas. Help?

Report •

#1
October 4, 2010 at 22:34:16
Ok, I've done some more fiddling, and currently my code is this:

set max = `wc -l xx01`
set m = 3
set line0 = ()
while ($m < $max[1])
set lineseg = `awk 'FNR==$m' xx01`
@ m = $m + 5
set line0 = ($line0 $lineseg)
end
echo $line0

This does what I want, with the exception of the awk line. It doesn't seem to like me calling a variable $m for the line number, rather than explicitly stating it. I'm not very well versed in awk. Is there a way around this issue?

guest


Report •

#2
October 5, 2010 at 07:11:34
This link discusses 3 methods for embedding shell variables in awk:

http://www.tek-tips.com/faqs.cfm?fi...


Report •

#3
October 5, 2010 at 18:03:36
Brilliant! Just what I was looking for. I've now got my code to do what I was trying to achieve. Thanks!

guest


Report •
Related Solutions


Ask Question