SED replacing the word after a word in a file

>> Tuesday, 13 November 2018



There is an application that writes a particular command into a log file whenever it runs.  And that command includes a password switch.  Which means the password is joyfully sat in a log file.  Even though you have to be able to log onto the server, and have permissions to read the log, it is still not ideal.  To strip the password out of the log I run:

sed -i 's/--password[^ ]* /--password replaced /g' myfile.txt

it says find the string "--password morecharacters " and change it to "--password replaced "

Lets break it down:

-i - edit file in place

's/regexp/replacement/g'
Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.

[^ ]* regular expression for match every character except a space - note there is a space after the ^  and before the ] and after the *
so it is saying match the word --password and whatever comes after it ignoring the first space
and up to the next space.       
      
g  do it for all iterations found in the file (global replace) 



cat myfile.txt:
          here be my command --password mypassword --anotherswitch and more words

sed -i 's/--password[^ ]* /--password replaced /g' myfile.txt

cat myfile.txt:

          here be my command --password replaced --anotherswitch and more words



  





Read more...

Oracle report all users, their roles and privileges

>> Wednesday, 31 October 2018


I took the sql for listing privileges recursively for Oracle users from Rene Nyffenegger's site, it is very good but only works for one user at a time as it stands, I wanted to run a report that outputted the same way for every user in my database.  So it needed to go into a PL/SQL loop.



set serveroutput on

declare

TYPE re_type IS TABLE OF varchar2(1024);
v_statement varchar2(1024);
v_result re_type;

BEGIN
for x in ( select username from dba_users)
LOOP
v_statement := 'select concat(lpad('' '', 2*level), granted_role) line1 from (select null grantee, username granted_role from dba_users where username = ''' || x.username || ''' union select grantee, granted_role from dba_role_privs union select grantee, privilege from dba_sys_privs ) start with grantee is null connect by grantee = prior granted_role';

execute immediate v_statement BULK COLLECT INTO v_result;

for indx in v_result.FIRST .. v_result.LAST
loop DBMS_OUTPUT.PUT_LINE(v_result(indx));
end loop; 

END LOOP;
END;
/

You get a formatted output like:

MYUSER1
    MYROLE_SESSION
      ALTER SESSION
      CREATE SESSION
    CONNECT
      CREATE SESSION
    MYROLE1
    MYROLE2
  MYUSER2
    CONNECT
      CREATE SESSION
    MYROLE1
    MYROLE3 

Particular thanks to Pete for seeing me past my quotes and array woes

Read more...

using find to find files but ignore lost+found

>> Tuesday, 19 September 2017

Because it took me a while to work this out and I know I'll never remember how I did it:

e.g.

/usr/bin/find /path/to/search/ -name "myfile_name_in_some_form*.tar.gz"  -not \( -path ".lost+found" -prune \) -print  -mtime +92 -exec ls {} \;


delete my backup files over 92 days old:

/usr/bin/find /backup/mydir/ -name "mybackupfile*.tar.gz"  -not \( -path ".lost+found" -prune \) -print  -mtime +92 -exec rm {} \;


An alternative:

/usr/bin/find /backup/mydir/ \! -readable -prune -o  -name "mybackupfile_*.tar.gz" -print  -mtime +92 -exec rm {} \;

Read more...

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP