KSH Expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH Expression
# 1  
Old 09-28-2005
KSH Expression

Quick question related to KSH expressions (not unix regular expressions).

I am trying to craft a pattern that will correctly identify lines that match the following CSV text in a case statement:
Code:
filename.txt, filename.txt, alpha, nnnn, nnnn, nnnn, Free form text

Originally I simply used an expression like *,*,*,*,*,*,* in the following case statement:
Code:
case ${LINE} in
    # Expression 1..n are informational and specific enough that the
    # expressions work well
    expression 1..n)
             ... match expressions 1..n logic ... ;;

    # CSV lines contain 7 fields and 6 commas
    *,*,*,*,*,*,*)
         ... match valid CSV line logic ... ;;

    # Malformed CSV lines or any other not matching my list of expressions
    *)
         ... malformed CSV line or other mismatch ... ;;
esac

Problem:
I found that the *,*,*,*,*,*,* CSV expression matches cases such as these:
Code:
field1, field2, field3, field4, field5, field6, field7, field8, field9
field1, field2, field3, field4, field5, field6
field1, field2, field3, field4, field5, field6, field7,,,,,,,
,field1, field2, field3, field4, field5, field6, field7

I have tried numerous variations and have ended up with this expression:
Code:
case ...
...
    @(*)@(,)@(*) ) ...
...
esac

I can match more precisely and this nails the smallest CSV list of "text, text" but I still have to incorporate some comma counting logic that I don't want to include.

The commas and/or asterisks are causing me complications with various expressions that I have tried (essentially * matches commas). Production code is very hard to change where I work once implemented so I'd like to nail down a very precise expression now and let the final *) expression trap all malformed lines. What am I doing wrong?

By the way, I have no control of the data file provided me so changes to my data source won't happen.
# 2  
Old 09-28-2005
Post some sample valid data.
# 3  
Old 09-28-2005
Code:
file1.txt, file1_original_name.txt, control1, 1001, 100001, 10000, Data Sample 1
file2.txt, file2_original_name.txt, control5, 2001, 100002, 10000, Data Sample 2
file3.txt, file3_original_name.txt, control7, 3001, 100003, 20000, Data Sample 3

# 4  
Old 09-28-2005
Sheesh....
Code:
#! /usr/bin/ksh

exec < data
while read line ; do
        case $line in
        +([_a-zA-Z0-9]).txt,+( )+([_a-zA-Z0-9]).txt,+( )+([a-zA-Z0-9]),+( )+([0-9]),+( )+([0-9]),+( )+([0-9]),* ) echo OK: $line
                ;;
        *)   echo XX: $line
                ;;
        esac
done
exit 0

# 5  
Old 09-28-2005
Excellent. This gets me very close.

Lines such as this still get through due to the final "*":
Code:
file1.txt, file1_original_name.txt, control1, 1001, 100001, 10000, Data Sample 1,,,,,

Altering the expression helps narrow it down:
Code:
+([_a-zA-Z0-9]).txt,+( )+([_a-zA-Z0-9]).txt,+( )+([a-zA-Z0-9]),+( )+([0-9]),+( )+([0-9]),+( )+([0-9]),+( )+([ a-zA-Z0-9]))

and I think this allows for anything but a comma in the final field:
Code:
+([_a-zA-Z0-9]).txt,+( )+([_a-zA-Z0-9]).txt,+( )+([a-zA-Z0-9]),+( )+([0-9]),+( )+([0-9]),+( )+([0-9]),+( )+([!,])

I need to tweak the filenames a bit but I believe I have the basis for nailing down a very precise expression.

Thanks for the help!

Thomas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking numeric expression in .ksh script

Gurus, Please need your help. I'm saving a filetimestamp into $filetimestamp and say....echo $filetimestamp gives 2015021612 I'm saving a cutoff_time into $cutoff_time say....echo $cutoff_time gives 2015021514 now my requirement is to check if $filetimestamp is greater than... (4 Replies)
Discussion started by: thummi9090
4 Replies

2. Shell Programming and Scripting

Why Relational Expression is Writing to a Expression?

Hello All, Not sure why this is happening... When the following If Statement is evaluated for some reason it is creating a file in the CWD called '0'. I've seen this happen before, just not in an If Statement... CODE: if then DIR_NAME="$1" DIR_SIZE=0 STATUS="" else... (3 Replies)
Discussion started by: mrm5102
3 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Shell Programming and Scripting

Regular Expression in Find command [KSH]

Hello, I am trying to use regex wtih find command in KSH. For some reason it is not working as expected. Input: comm_000_abc_0102.c comm_000_abc.c 456_000_abc_1212.cpp 456_000_abc_.cpp Expected Output: comm_000_abc_0102.c kkm_000_abc_8888.cpp (Basically I want to find all... (6 Replies)
Discussion started by: vinay4889
6 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

regular expression in ksh

I am trying to test to see if the hostname of the computer I'm on starts with ne1dxdb - it can contain any characters after that. I have the following code, but it's not working - it's not falling into the if statement. Any ideas? boxname="unknown" function get_hosttype { ... (4 Replies)
Discussion started by: kittsi
4 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question