Excluding patterns from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Excluding patterns from a list
# 1  
Old 03-06-2012
Excluding patterns from a list

I have the following code that takes the command line arguments.
However I want to remove from the command line list the user options.

For example, removing

Code:
--quiet --shift=3 sort=4/5/6

I have written the following code to take care of this situation.

Code:
set strLst = `echo $argv[$firstArg-$lastArg] | tr ' ' '\n' \
  | grep -ivE '[-]-((quiet)|(check)|(shift=[0-9]+)|(sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$' | tr '\n' ' '`

As I include more options, the command is getting longer and I want a way to simplify this, so that I can write the options I want excluded separately. In addition, I want to disregard if options are a combination of upper or lower case.

To try to do this I have tried the following. I try to include the exclusion pattern
one at a time, building the command. However this is not working very well due to unix expansion mechanism. I think I have tackled the upper and lower case combination, by using grep -i.

Code:
set excludeStr = "[-]-(quiet)"
set excludeStr = "${excludeStr}|(check)"
set excludeStr = "${excludeStr}|(shift=[0-9]+)"
set excludeStr = "${excludeStr}|((sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$)"

set strLst = `echo $argv[$firstArg-$lastArg] | tr ' ' '\n' \
  | grep -ivE '$excludeStr' | tr '\n' ' '`


Last edited by kristinu; 03-06-2012 at 08:32 PM..
# 2  
Old 03-07-2012
Did you consider using getopts ?
# 3  
Old 03-07-2012
I think kristinu is wrestling with (t)csh, no?
# 4  
Old 03-07-2012
I did it like this.

Code:
set strLst = `echo $argv[$firstArg-$lastArg] | tr ' ' '\n'  \
  | grep -ivE --   '(--check|--check-colpos)'  \
  | grep -ivE --   '(--sort|--sort-fields)'    \
  | grep -ivE --   '(--group|--group-table)'   \
  | grep -ivE --   '(--shift|--shift-table)'   \
  | grep -ivE --   '(-v|--vrb-level)'  \
  | grep -ivE --   '(-q|--quiet)'      \
  | grep -ivE --   '(-u|--usage)'      \
  | grep -ivE --   '(-e|--examples)'   \
  | grep -ivE --   '(-h|--help)'       \
  | grep -ivE --   '( (--shift=[0-9]+) | (--shift-table=[0-9]+) )'  \
  | grep -ivE --   '--((sort|group)=[0-9]+/[0-9]+(/[0-9]+)*)$' | tr '\n' ' '`

I would need a bit of assistance in using getopts. I had considered it long time ago but not been very good of using it in a way I want.

---------- Post updated at 08:33 AM ---------- Previous update was at 08:30 AM ----------

Ok, in my code I have he following. How would you tackle the procedure using getopts using ksh. My plan is to convert my scripts to ksh eventually.

Code:
# --Read Command Line Arguments---------------------------------------------------------------------

# Set echo to recognize both the -n flag and backslashed escape sequences
set echo_style = "both"

# Perform numeric calculations using bc.
alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'

# --Read Command Line Arguments---------------------------------------------------------------------

set ierr = 0
set iarg = 0
set opt_chkColPos = 0
set opt_sortFlds = 0
set opt_groupTbl = 0
set opt_shiftTbl = 0
set opt_verbose = 0
set opt_quiet = 0
set opt_usage = 0
set opt_examples = 0
set opt_help = 0

set vrb_usrInputFlag = 0
set Def_vrbLevel = 1
set Def_shiftLen = 3

set arg_fullNamesLst = ""
set narg = $#argv
while ($iarg < $narg)

  MATH iarg = $iarg + 1
  set arg = $argv[$iarg]
  set usrInputFlag = `echo $arg | awk '/=/ {print 1}; ! /=/ {print 0}'`
  set opt = `echo $arg | awk 'BEGIN {FS="="} {print $1}' | tr '[:lower:]' '[:upper:]'`
  set par = `echo $arg | awk 'BEGIN {FS="="} {print $2}'`

  switch ($opt)

    # -- Optional Arguments ------------------------------------------------------------------------

    case "--CHECK":                         # Separates numbers from characters.
    case "--CHECK-COLPOS":
      set opt_chkColPos = 1
      breaksw

    case "--SORT":
    case "--SORT-FIELDS":
      set arg_sortPtn = $par
      set opt_sortFlds = 1
      breaksw

    case "--GROUP":
    case "--GROUP-TABLE":
      set arg_groupPtn = $par
      set opt_groupTbl = 1
      breaksw

    case "--SHIFT":
    case "--SHIFT-TABLE":
      set arg_shiftLen = $par
      set opt_shiftTbl = 1
      breaksw

    case "-V":
    case "--VRB-LEVEL":
      set arg_vrbLevel = $par
      set opt_verbose = 1
      set vrb_usrInputFlag = $usrInputFlag
      breaksw

    case "-Q":
    case "--QUIET":
      set opt_quiet = 1
      breaksw

    case "-U":
    case "--USAGE":
      set opt_usage = 1
      breaksw

    case "-E":
    case "--EXAMPLES":
      set opt_examples = 1
      breaksw

    case "-H":
    case "--HELP":
      set opt_help = 1
      breaksw

    default:
      set arg_fullNamesLst = "$arg_fullNamesLst $arg"
      breaksw

  endsw

end   # while

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Help needed in excluding certain word patterns

Hi, I need help with following. I need to exclude words that match following patterns a. more than length 4 (example SBRAP) b. contains mixture uppercase and lower case regardless of the length (example GSpD) File contains COFpC MCHX SP SNFCA GEH SBRAP DGICA JPMpE WFCpP GSpD AXL... (5 Replies)
Discussion started by: jakSun8
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] List Files With Multiple Name Patterns

Hi, We have created a script that would accept the an indicator as a parameter and archive files present in a directory. The indicator would drive what the name pattern of the files to be archived should be. If the indicator is 1, then the pattern to look out for is FACT*. If the indicator is... (2 Replies)
Discussion started by: jerome_rajan
2 Replies

4. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

5. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

6. UNIX for Dummies Questions & Answers

List files older that 7 days in a dir, excluding all subdirs

Hi, I would like to list all files, older than 7 days, in a directory, but exclude all subdirectories in the find command. If I use find . -type f -mtime +7 all files in the subdirs are also included. How can I exclude them? Regards, JW (6 Replies)
Discussion started by: jwbijl
6 Replies

7. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

8. UNIX for Dummies Questions & Answers

script to search patterns inside list of files

>testfile while read x do if then echo $x >> testfile else fi if then echo $x >> testfile else fi done < list_of_files is there any efficient way to search abc.dml and xyz.dml ? (2 Replies)
Discussion started by: dr46014
2 Replies

9. Shell Programming and Scripting

finding and removing patterns in a large list of urls

I have a list of urls for example: Google Google Base Yahoo! Yahoo! Yahoo! Video - It's On Google The problem is that Google and Google are duplicates as are Yahoo! and Yahoo!. I'm needing to find these conical www duplicates and append the text "DUP#" in from of both Google and... (3 Replies)
Discussion started by: totus
3 Replies

10. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question