Friday, April 28, 2006

XSL Transform with Ant

Ant provides a taskthat will perform an XSL transformation on multiple XML files. Below is an example of using this task:

<target name="TransformAll">
<xslt basedir="${basedir}/xml"

destdir=
"${basedir}/html"
includes=
"*.xml"
style="${basedir}/xslt/test.xsl"
/>


The basedir parameter specificies the directory of the files to transform. The includes parameter can be used to filter out specific files in the basedir to transform. The destdir specifies where the transformed files will be stored. The style parameter specifies the XSL file to be used to transform the input files.

This information was obtained from Java, Eclipse, Ant, XML, JMX and other acronyms which provides a lot more useful information than my blog :(.

Thursday, April 27, 2006

Using sed to remove lines from multiple files

In addition to searching and replacing content in files, Sed can also remove specific characters from files. This can be done by specifying the '/d' option after the pattern you specific. Below is a script which deletes the DTD header for a list of files in a specific directory using the Sed command:


##########################################################
#
# Script used to delete the DOCTYPE header consisting of
# the dtd file path from each xml in a specified directory
#
##########################################################

INPUT_DIR=$1

for filename in $INPUT_DIR/*.xml
do
echo "removing dtd path for $filename ...."
sed -e '/DOCTYPE/d' $filename > $INPUT_DIR/temp_file
mv -f $INPUT_DIR/temp_file $filename
done

Labels:

Wednesday, April 12, 2006

Unix's find command

Introduction

The find command allows the Unix user to process a set of files and/or directories in a file subtree.

You can specify the following:

  • where to search (pathname)
  • what type of file to search for (-type: directories, data files, links)
  • how to process the files (-exec: run a process against a selected file)
  • the name of the file(s) (-name)
  • perform logical operations on selections (-o and -a)

Search for file with a specific name in a set of files (-name)

find . -name "temp.txt" -print

This command will search in the current directory and all sub directories for a file named temp.txt.

Note: The -print option will print out the path of any file that is found with that name. In general -print wil print out the path of any file that meets the find criteria.

How to apply a unix command to a set of file (-exec).

find . -name "temp.txt" -exec chmod o+r '{}' \;

This command will search in the current directory and all sub directories. All files named temp.txt will be processed by the chmod -o+r command. The argument '{}' inserts each found file into the chmod command line. The \; argument indicates the exec command line has ended.

The end results of this command is all rc.conf files have the other permissions set to read access (if the operator is the owner of the file).

How to apply a complex selection of files (-o and -a).

find /usr/tmp -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print

This command will search in the /usr/tmp directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:

  • -not means the negation of the expression that follows
  • \( means the start of a complex expression.
  • \) means the end of a complex expression.
  • -o means a logical or of a complex expression.
    In this case the complex expression is all files like '*,v' or '.*,v'

The above example is shows how to select all file that are not part of the RCS system. This is important when you want go through a source tree and modify all the source files... but ... you don't want to affect the RCS version control files.

How to search for a string in a selection of files (-exec grep ...).

find . -exec grep "foo" '{}' \; -print

This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "foo" '{}' \; -print

This command is very important for process a series of files that contain a specific string. You can then process each file appropriately.

Labels:

Thursday, April 06, 2006

Using lsof

The lsof (stands for list of open files) provides information on file and ports which are opened by any process currently running. Below is an example:

# lsof -i -nP | grep httpd
httpd 2318 apache 16u IPv4 0x019922bc 0t0 TCP 127.0.0.1:8000 (LISTEN)
httpd 2319 apache 16u IPv4 0x019922bc 0t0 TCP 127.0.0.1:8000 (LISTEN)
httpd 2322 apache 16u IPv4 0x019922bc 0t0 TCP 127.0.0.1:8000 (LISTEN)

Labels: