Wednesday, September 17, 2014

Perl : Find files using File::Find

How to find the list of files in Perl  like the UNIX find command? The answer is File::Find CPAN module. Let us see in this article how to use the File::Find module.

File::Find:
   File::Find module contains 2 modules: find and finddepth. Both are used to find the files, the difference being the order in which the files and directories are parsed. find has all the options like the Unix find command.

Wednesday, August 13, 2014

sed - 20 examples to remove / delete characters from a file

In this article of sed series, we will see the examples of how to remove or delete characters from a file. The syntax of sed command replacement is:
$ sed 's/find/replace/' file
 This sed command finds the pattern and replaces with another pattern. When the replace is left empty, the pattern/element found gets deleted.

Let us consider a sample file as below:
$ cat file
Linux
Solaris
Ubuntu
Fedora
RedHat

Tuesday, November 12, 2013

How to use SUID for shell scripts in Linux?

Question : How to write a shell script which will read the required passwords/connect strings from a config file? Other users should be able to execute the scripts, however they should not be able to read the config file.

Say, I have a config file, myconfig.txt, which contains credentials for an oracle connection:
$ cat myconfig.txt
SQLUSR="scott"
SQLPWD="pwd123"

Tuesday, August 13, 2013

Python - How to write a list to a file?

How to write an entire list of elements into a file? Let us see in this article how to write a list l1 to a file 'f1.txt':

1. Using write method:
#!/usr/bin/python

l1=['hi','hello','welcome']

f=open('f1.txt','w')
for ele in l1:
    f.write(ele+'\n')

f.close()
 The list is iterated, and during every iteration, the write method writes a line to a file along with the newline character.

Tuesday, July 30, 2013

Python - How to find the location of a module?

When a module is imported into a program, how to find out the path from where the module is getting picked?

    In the Unix world, when a command say ls is executed, the operating system searches in a list of directories for the command executable and once found, executes the command. The list of directories is maintained in the PATH variable.
    Similarly, in Python, whenever a module is imported, say "import collections', python also searches in a list of directories to find the module file "collections.py", and once found, includes the module. The list of directories to be searched is maintained in a list, sys.path, from the sys module. Hence, this can be accessed using the sys.path.