Trying to use gsub using variables

I need to be able to match and substitute text in a string and I’m
trying to use gsub with no luck…probably because it’s looking for an
regular expression.

Does anyone know how I can achieve this same thing using another method
perhaps?
Here is my code that is not working:

x = “NEW_WORD”
acl.gsub!(/WORD_TO_MATCH_AND_REPLACE/, x )

thanks

jackster

jackster the jackle wrote:

Here is my code that is not working:

x = “NEW_WORD”
acl.gsub!(/WORD_TO_MATCH_AND_REPLACE/, x )

the x is fine, your text inside the /…/ would be wrong.

e.g.
irb(main):025:0> u=“Oct”
=> “Oct”
irb(main):026:0> s
=> “abc def Sep 10 10:22 filename”
irb(main):027:0> s.gsub(/Sep/,u)
=> “abc def Oct 10 10:22 filename”

On 27.09.2008 05:18, Nit K. wrote:

jackster the jackle wrote:

Here is my code that is not working:

x = “NEW_WORD”
acl.gsub!(/WORD_TO_MATCH_AND_REPLACE/, x )

the x is fine, your text inside the /…/ would be wrong.

How do you know? OP has neither posted what’s in “acl” nor what he
intends to replace.

robert

thanks guys…I’m making progress but what I have been slowly leading up
to is searching using a variable then replacing it with a variable.

acl = “now is the time”
x = time
y = TEST

acl.gsub(/x/,y)

How do I get gsub to use the value of x rather “x” itself in my match?

thanks

Jackster

jackster the jackle wrote:

thanks

Jackster

/#{x}/

On 28.09.2008 03:03, jackster the jackle wrote:

“OUT”,“PP” and “AA”.
puts acl
@acl_list.each do |acl|
h.each {|key,value|
acl.gsub!(/#{key}/,value)}
if acl[/#{value}/]
acl.gsub!(/^/,k.to_s)
k+=1
puts acl
end

It seems you are doing this too complicated and making your life harder
than necessary. If I read you correctly this is what you want to do:
you want to read a file where each line falls in one of four fixed
classes (IN, OUT…). You want to number each class individually and
print that out.

If you use a Hash you can do something like this (proper variable naming
goes a long way in making this readable and understandable):

counters = Hash.new 0

ARGF.each do |line|
line_class = line[/\b(?:IN|OUT|PP|AA)\b/]
raise “Unknown rule” unless line_class
print counters[line_class] += 1, " ", line
end

Cheers

robert

On 28.09.2008 10:46, Robert K. wrote:

If you use a Hash you can do something like this (proper variable naming
goes a long way in making this readable and understandable):

counters = Hash.new 0

ARGF.each do |line|
line_class = line[/\b(?:IN|OUT|PP|AA)\b/]
raise “Unknown rule” unless line_class
print counters[line_class] += 1, " ", line
end

PS: Forgot to mention that you may have to adjust the regexp matching to
be more robust. In your case you may want to do

line_class = line[/^access-list\s+(IN|OUT|PP|AA)\b/, 1]

Cheers

robert

Thanks alot Robert…I’m not familiar with ARGF so I’m having a bit of
trouble getting the code you sent properly inserted into what I already
have.

What does the ARGF do?

thanks again

jackster

2008/9/28 jackster the jackle [email protected]:

Thanks alot Robert…I’m not familiar with ARGF so I’m having a bit of
trouble getting the code you sent properly inserted into what I already
have.

What does the ARGF do?

What did you find out so far?

robert

thanks…I got that to work…now for my last question…I am trying to
match on a hash value. I have a text file that gets opened with 4
different types of statements in it…the values are contained in my
hash called “h”. The values in the hash are (IN,OUT,PP,AA). @acl_list
contains a bunch of firewall rules that either have the value IN,OUT,PP
or AA in them. What I am trying to do is loop through @acl_list,
substitute the value of h with the key (which works great) but then, I
need to put the number 1 in the first firewall rule that contains IN,
then the number 2 in the second firewall rule that contains IN etc.
When I’m finished with the “IN” rules, I want to do the same thing for
“OUT”,“PP” and “AA”.

What I have here works great but only for the “IN” rules:

k= 1
@acl_list.each do |acl|
h.each {|key,value|
acl.gsub!(/#{key}/,value)}
if acl[/IN/]
acl.gsub!(/^/,k.to_s)
k+=1
puts acl
end

What I really need is to match on the value of the hash, then add the
numbers to the beginning of each line. I thought the following would
work but it doen’t (although #{value} does have the correct info in it).
Can anyone help me figure out how I can get the loop to match on the
value of my hash? I think if I can get this to work, everything will be
the way I need it.

k= 1
@acl_list.each do |acl|
h.each {|key,value|
acl.gsub!(/#{key}/,value)}
if acl[/#{value}/]
acl.gsub!(/^/,k.to_s)
k+=1
puts acl
end

Thanks again!
jackster

Here’s an example of what is actually contained in @acl_list:
access-list IN permit tcp 172.2.1.0 255.255.0.0 172.1.1.0 255.255.255.0
eq ftp
access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.10 255.255.255.0 eq
13782
access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0
eq ftp
access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0
eq ftp
access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0
eq ftp
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.255.0 eq
www
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq
ftp
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.100.0 255.255.0.0 eq
ssh
access-list AA permit tcp 172.2.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq
www
access-list AA permit tcp 172.22.0.0 255.255.0.0 172.21.100.0 255.0.0.0
eq 8080

My end result is that I want these same rules to look like this:
1 access-list IN permit tcp 172.2.1.0 255.255.0.0 172.1.1.0
255.255.255.0 eq ftp
2 access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.10 255.255.255.0
eq 13782
3 access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0
eq ftp
1 access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0
255.255.255.0 eq ftp
2 access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0
255.255.255.0 eq ftp
1 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.255.0
eq www
2 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq
ftp
3 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.100.0 255.255.0.0
eq ssh
1 access-list AA permit tcp 172.2.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq
www
2 access-list AA permit tcp 172.22.0.0 255.255.0.0 172.21.1.0 255.0.0.0
eq www