Pattern Match Script

Hi,

I have a requirement in which I have to search the File name for pattern and if the pattern matches then I will have generate a value.

Example:

If 
$File_name='*NF' Then "NBC"
Else 
$File_Name='SC*' Then "CBS" 
Else "FB" 
fi

What is the best way to do this? I want to pass the File name as parameter.

Thanks.

case "$1" in
        *NF)
                echo "NBC"
                ;;
         SC*)
                echo "CBS"
                ;;
esac
./myscript filename

Thanks Corona688 I tried to execute the script you posted but it does not give me an output. also i want to capture the output into a parameter, how do I do it.

Thanks.

It works absolutely fine here. Show exactly what you did, word for word, letter for letter, keystroke for keystroke, and I'll tell you why it didn't work. I suspect your input isn't quite what I expected.

To save something in a variable, save something in a variable. It's just a different statement in the same place, you can put whatever you want there.

case "$1" in
        *NF)
                VAR="NBC"
                ;;
         SC*)
                VAR="CBS"
                ;;
esac

Note there's no spaces on either side of the equal sign. Shell code insists on that, putting spaces there changes the meaning.

##!/bin/ksh
File_Name=${1}
case "$File_Name" in
        *NF)
                VAR="NBC"
                ;;
         SC*)
                VAR="CBS"
                ;;
esac

Execution

testscript ANFCDED.txt

Since you had NC and SC, I figured you wanted files ending in NC or files beginning in SC, since that's what those globs mean.

If you want the string to be found anywhere, including the middle, that'd be a * before and a * after.

Your #! is wrong. It is #!/bin/ksh, not ##!/bin/ksh.

#!/bin/ksh
File_Name=${1}
case "$File_Name" in
        *NF*)
                VAR="NBC"
                ;;
         *SC*)
                VAR="CBS"
                ;;
esac
1 Like

Thanks it works, using ${VAR} to pass the captured output to a file.