Pages

Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Thursday, January 7, 2010

Regular expressions patterns with grep

I know there is a lot of documentation on the topic of regular expression (like the Regexp bible, Mastering Regular Expressions, by Jeffrey Friedl ) so just some short examples I have used:
  1. ls -l |egrep '.*\.[ch]p{0,2}$'
    Matches  strings with a character repeated any number of times (0+), followed by the '.' character which is escaped(to not be interpreted as the '.' which represents any character), then 'c' or 'h' followed by  0, 1, or 2 'p's. '$' means 'end of line' (the caret ('^') could be used similarly to mark the begging of line).
    But this would also match files like  "main.cp" which is not what I want so:
  2. ls -l |egrep '.*\.(c|cpp|h|hpp)$'
    Matches just specific extensions (enclosed in "(...)").
  3. ls -l |egrep -v '.*\.[ch]p{0,2}$'
    The '-v' parameter excludes the lines matching this pattern
  4. ls -l |egrep -w "file123"
    Matches word "file123", but not "file1234" or "file12345". Full description from man grep :
    -w, --word-regexp
        Select  only  those  lines  containing  matches  that form whole words.  The test is that the matching substring must  either  be at  the  beginning  of  the line, or preceded by a non-word constituent character.  Similarly, it must be either at the end  of the line or followed by a non-word constituent character.  Word-constituent characters are letters, digits, and the  underscor
    e
  5. grep -A n -B m 'pattern'
    Shows the lines matching the pattern, and also n lines after, and m lines before the match.
Good references:
Enjoy! :)

Tuesday, September 29, 2009

Script to find diff between labels in ClearCase

     A simple bash script, using cleartool,  to show files that have the new label, but not the old one. Supposedly modified files are label correctly with new label, the script will show the files modified between two labels, in ClearCase. It will also show the creator of the new version. There is also a grep with regular expression filter on the output, to show only the files with .c, .cpp, .h, ... extensions.

#! /bin/bash
# Script to show diffs between 2 ClearCase labels

#cleartool location
export CT=/usr/atria/bin/cleartool

function usage() {
 echo "Usage: cc_dif.sh <new_label> <old_label>"
 exit
}

if [ $# -ne 2 ] ; then
 usage
 exit
else
 echo -e "\t-- New label: $1 --"
 echo -e "\t-- Old label: $2 --\n"
fi

${CT} find . -version "{(lbtype($1) && ! lbtype($2))}" -exec 'cleartool describe -fmt "%Xn \t\tBy: %Fu (%Lu) \n" $CLEARCASE_PN' | grep -E '.*\.(c|cpp|h|hpp|xml)@' 


References:
  • cleartool find man page (cand also be accessed with cleartool man find )
  • cleartool describe man page
  • format to apply to describecommand:  fmt_ccase  
  • Useful regexp filters, this post
Hope it will be useful for others too. If you used other methods to do this, leave a comment:)