LinuxQuestions.org
Review your favorite Linux distribution.
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 10-12-2018, 05:53 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
Question ruby append/delete to file if pattern


Hello,

I do have this setup:

ldap_config.conf

ldap_filters = yes
...
...

Now, if file contains this line ldap_filters = yes then append this :
ldap_filters_users =<here is a list of users>

Code:
users = ['user1','user2',...'userN']
file_contents = File.read(file_name)
file_contents.gsub!(/ldap_filters = yes/, "ldap_filters = yes\nldap_filter_users =(|(user1)(user2)(userN))")
File.write(file_name, file_contents)
What I'm a bit lost in howto do it:

users array is a dynamic one

- loop through array of users
- read the file "ldap_config.conf" and if line match any or all of users delete that line from file


An explicit example may help me better
 
Old 10-12-2018, 05:06 PM   #2
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
It's been a long time since I've used Ruby, but I managed to write something that does what you need.
Code:
#!/usr/bin/env ruby

users = %w/user1 user2/
key = 'ldap_filter_users'
separator = '|'

File.open('ldap_config.conf', 'r+') do |f|
    content = f.read

    if content.index(users.join(separator)) or users.any? {|u| content.index(u)}
        puts content.gsub(/#{key}.*$\n?/, '')
    elsif content.index('ldap_filters = yes')
        f.write("#{key} = #{users.join(separator)}")
    end
end

Last edited by individual; 10-12-2018 at 08:13 PM. Reason: There was a database earlier, and I was unable to edit my post.
 
Old 10-16-2018, 07:24 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
I'm quite noob in ruby, but I've managed to understand the code, but here is something that also couldn't find explain well:

read file => Loop through user array => if file_line match user1 or user2 or userN [ ldap_filter_users =(|(user1)(user2)(userN))] => delete that line from file ...
 
Old 10-16-2018, 09:59 AM   #4
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by fritz001 View Post
I'm quite noob in ruby, but I've managed to understand the code, but here is something that also couldn't find explain well:

read file => Loop through user array => if file_line match user1 or user2 or userN [ ldap_filter_users =(|(user1)(user2)(userN))] => delete that line from file ...
Sorry I didn't comment it, but it seems like you understand the logic. What are you confused about?
 
Old 10-16-2018, 11:43 AM   #5
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by individual View Post
Sorry I didn't comment it, but it seems like you understand the logic. What are you confused about?

~Solved~ I've got a typo while testing the code :!

Thanks for the input!
 
Old 10-16-2018, 01:09 PM   #6
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
!!latest edit !!

..as I've checked once more the code...

here is a simple config file:

ldap_config.conf
Code:
#line1
#line2
[ldap_settings]
ldap_filters = yes
ldap_filter_users = should be added right here 
[ad_settings]
#line1
#line2
[kerberos_settings]
#line1
#line2

1st case : users = %w/user1/
ldap_filter_users = (user1) =>this is the easiest case

2nd case : users = %w/user1 user2 userN/
ldap_filter_users = (|(user1)(user2)(userN))


Code:
 
if users.size == 1
                       $_var  =  "(uid=#{users[0]})"
else
  users.each.with_index do |x, index|
                                if index == 0
                                   $_var  =  "(|(uid=#{x})"
                                elsif  index == users.size - 1
                                   $_var  <<  "(uid=#{x}))"
                                else
                                   $_var  <<  "(uid=#{x})"
                                end
        end
end

Have a better method then this?
 
Old 10-16-2018, 03:51 PM   #7
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by fritz001 View Post
!!latest edit !!

..as I've checked once more the code...

here is a simple config file:

ldap_config.conf
Code:
#line1
#line2
[ldap_settings]
ldap_filters = yes
ldap_filter_users = should be added right here 
[ad_settings]
#line1
#line2
[kerberos_settings]
#line1
#line2

1st case : users = %w/user1/
ldap_filter_users = (user1) =>this is the easiest case

2nd case : users = %w/user1 user2 userN/
ldap_filter_users = (|(user1)(user2)(userN))


Code:
 
if users.size == 1
                       $_var  =  "(uid=#{users[0]})"
else
  users.each.with_index do |x, index|
                                if index == 0
                                   $_var  =  "(|(uid=#{x})"
                                elsif  index == users.size - 1
                                   $_var  <<  "(uid=#{x}))"
                                else
                                   $_var  <<  "(uid=#{x})"
                                end
        end
end

Have a better method then this?
Since you need the line to be at a certain position, you'll have a much easier time reading each line from the file and filtering it that way. Here is an example in pseudo-code:
Code:
for line in file
    if all users in the user array are not in line
        if "ldap_filters = yes" in line
            write "ldap_filter_users = yes"
            write "ldap_filter_users = (|(user1)(user2)(userN))"
        else
            write line
For your other problem, which seems to be constructing the users line, something like this will work:
Code:
users.length == 1 ? "(#{users[0]})" : '(|' + users.map{|u| "(#{u})"}.join + ')'
Where users is your array of users.

Last edited by individual; 10-16-2018 at 03:55 PM. Reason: Forgot to handle only one user.
 
Old 10-17-2018, 08:42 AM   #8
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Much appreciation for effort, but either I'm incompetent or I just can't simple figure out the append/insert method:


Code:
cat ldap_config.conf
line1
line2
line3
line4
 ldap_filters = yes
s
v
Code:
File.open("ldap_config.conf", "r+") do |f|
        f.each do |line|
                f.puts("Hello") if line.match(/ldap_filters =/)
        end
end
=>output


Code:
line1
line2
line3
line4
 ldap_filters = yes
Hello
last lines of file have been removed

Code:
File.open("ldap_config.conf", "r+").each do |line|
                puts("#{line}")
                puts("Hello") if line.match(/ldap_filters =/)
end
=>output

Code:
line1
line2
line3
line4
 ldap_filters = yes
Hello
s
v
 
  


Reply


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
[SOLVED] How to delete everything from a file, once a particular pattern is found, including the pattern ansh007 Linux - General 1 03-07-2017 08:15 AM
Delete rows accoring to pattern in a file. Siggloo Programming 3 04-24-2015 09:09 AM
[SOLVED] shell script to delete a pattern from file deepmala8 Linux - Newbie 7 09-29-2011 11:28 PM
[SOLVED] How to search file by pattern and then delete corresponding lines in shell cyatomato Programming 8 09-17-2010 08:08 AM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:11 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration