I am trying to create a script that will add a user to the UserDir enabled list or will remove it from UserDir disabled list in userdir.conf.For the sake of my question, let's say I have the following file:Lorem ipsumLorem ipsum    List 1 enabledLorem ipsumLorem ipsum    List 2 disabledLorem ipsumWhat I would like to do is when I run my script with the following ./script enabled user is to modify my file to:Lorem ipsumLorem ipsum    List 1 enabled userLorem ipsumLorem ipsum    List 2 disabledLorem ipsumAfterwards, if I do ./script disabled user it will remove user from List 1 and add it two list 2. Thus, the output of the file would be:Lorem ipsumLorem ipsum    List 1 enabledLorem ipsumLorem ipsum    List 2 disabled  userLorem ipsumMy question is not about how to write this script, it is about how to add/delete text into/from a file at a specific location.Off-topic question: Is there a better way to add/remove users in the UserDir list of the apache2?
Write In A File at Specific Location
files;text processing
This can be done with sed or awk. Take your pick. Personally, I like to use the tool with the smallest footprint. You can, of course, do it in any programming language like Perl, C, SNOBOL or PDP-8 machine code, but this may be ever so slightly beyond the scope of the question. ;)To delete a user from the UserDir enable line using sed:sed -e '/^[^#]*UserDir\s\+enabled\s\+/ s/\bfoo\b//g' -e 's/\s\+/ /g' FILE-IN >FILE-OUTThis does two things:For all lines that start with a non-commented out UserDir enabled, it removes the user foo (but not, for example, a user foobar).It removes superfluous white space.To add a user:sed -e '/^[^#]*UserDir\s+enabled\s+/ s/\(\s\+#\|$\)/ bar\1/' FILE-IN >FILE-OUTThis will look for all lines like Userdir enabled that isn't commented out, same as before. For any such lines found, it will add the user bar at the end. If the line ends in a comment, the comment is respected and left in there.To add a new UserDir enabled line after the UserDir disabled line:sed -e '/^[^#]*UserDir\s+enabled\s+/ a UserDir disabled foo bar baz' FILE-IN >FILE-OUTThis uses the append (a) sed command to add text after every non-commented-out line like UserDir enabled .... Change the a to an i to insert before the matched line.Some notes:Obviously, change enabled to disabled at will.We use \s+ instead of spaces to catch cases where other forms of white space are used (e.g. tabs), and/or multiple white space characters are encountered.You can combine both operations. Sed can run multiple expressions on each file. Each expression is provided after the -e option and they run in order, from left to right.Note that some characters that aren't escaped in some regular expression dialects are escaped for the regular expression flavour used by sed.These will require a temporary file (FILE-OUT) to store the edited file (FILE-IN).If you want to invoke these in-place (without redirecting to a temporary file), replace the FILE-IN >FILE-OUT with -i FILE.These work for sure on Linux. Your mileage may vary with sed versions from other Unices. You may find -i (above) isn't available and that regular expression support is more limited.Off-topic answer: it would so much easier if Apache would let you enter multiple UserDir directives and added up the user lists. It may do that, but a cursory check came up blank. I've never had to do this, so I'm not sure whether is possible.