ProgrammingThis 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.
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.
It looks more to me like he wants to take the bracketed parts as a sequence of integers, and expand them into individual entries based on that sequence. [11:15] becomes five single lines with [11], [12], [13], [14], and [15]. But the whole problem is still not entirely clear.
You say it's a "big file", but you only showed us one line. Does every line have sequences like that, or are there other lines interspersed? Are the sequences all single digit increments? Are the patterns regular or irregular in any way? Do you want the output to be a single file, with only the expanded lines, or with the expanded lines following the unexpanded lines, or what?
Please clarify.
And please use [code][/code] tags around your code (including input and output text), to preserve formatting and to improve readability.
I see integers not decimals.
To clarify, you want to strip the bits between : and ], aka take turn [nn:nn] into [nn]?
yaa.. its integers.
first we need to calculate the range between MDQ[11:15] , its 5.
and need to split the line into 5 lines as given below:
MDQ[11]
MDQ[12]
MDQ[13]
MDQ[14]
MDQ[15]
It looks more to me like he wants to take the bracketed parts as a sequence of integers, and expand them into individual entries based on that sequence. [11:15] becomes five single lines with [11], [12], [13], [14], and [15]. But the whole problem is still not entirely clear.
You say it's a "big file", but you only showed us one line. Does every line have sequences like that, or are there other lines interspersed? Are the sequences all single digit increments? Are the patterns regular or irregular in any way? Do you want the output to be a single file, with only the expanded lines, or with the expanded lines following the unexpanded lines, or what?
Please clarify.
And please use [code][/code] tags around your code (including input and output text), to preserve formatting and to improve readability.
Yes. Its a big file, i showed here only one line and every other lines has same sequence.
Yes, should the first range dictate the number of rows, are the other ranges redundant (only their first number need be read?) or do they dictate just the pattern/stepping for values in those rows&columns, or can they cause more rows to be added, etc?
#!/usr/bin/env ruby
range=[]
File.open("file").each do |line|
if line[/^MDQ\[(\d+):(\d+)\]/]
num=line.scan(/DATA\[(\d+):/)[0][0]
line.scan(/^MDQ\[(\d+):(\d+)\]/){|x,y| range=(x..y).to_a }
range.each do |i|
line.gsub!(/(MDQ|TPR_AAWD)\[.[^\]]*?\]/,"\\1[#{i}]")
puts line.gsub!(/DATA\[.[^\]]*?\]/,"DATA[#{num}]")
num.succ!
end
else
puts line
end
end
Your solutions are working fine for me.... Thanks a lot for your help.....
Grail, Can you please explain the FS = "[][]" in the first line of the awk script.
FS = "[][]"
I belive this is setting the field separator to the regular expression [][] which is the character set containing ] or [. The definition of a character set is the outer [] and I think the double quotes might be superfluous.
FS = [\]\[] might be clearer, you can test if it works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.