Pattern Match Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Match Script
# 1  
Old 12-14-2012
[SOLVED] 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:

Code:
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.

Last edited by joeyg; 12-14-2012 at 03:44 PM.. Reason: Please wrap commands and data with CodeTags
# 2  
Old 12-14-2012
Code:
case "$1" in
        *NF)
                echo "NBC"
                ;;
         SC*)
                echo "CBS"
                ;;
esac

Code:
./myscript filename

# 3  
Old 12-14-2012
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.
# 4  
Old 12-14-2012
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.

Code:
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.
# 5  
Old 12-14-2012
Code:
##!/bin/ksh
File_Name=${1}
case "$File_Name" in
        *NF)
                VAR="NBC"
                ;;
         SC*)
                VAR="CBS"
                ;;
esac

Execution
Code:
testscript ANFCDED.txt


Last edited by btt3165; 12-14-2012 at 04:19 PM.. Reason: Extra [
# 6  
Old 12-14-2012
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.

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

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 12-14-2012
Thanks it works, using ${VAR} to pass the captured output to a file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

2. UNIX for Beginners Questions & Answers

awk script for pattern match and line break

Hi, I have input which reads like 9089.00 ----- kl jkjjljk lkkk; (909099) 9097.00 ----- HGJJHHJ jcxkjlkjvhvlk jhdkjksdfkhfskd 898.00 ----- HHHH I am trying to do something like this - As soon as I found pattern match "XYZ.00-----" it will insert a line break to the input and will go to... (3 Replies)
Discussion started by: Indra2011
3 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

5. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

6. Shell Programming and Scripting

script that can do a pattern match

Hi, str1="Hello World" CheckPattern="drW" Need a search function that will do a character search. and return me which charater($CheckPattern) is not matched in $str1. Thanks in advance Madhu (1 Reply)
Discussion started by: madhuti
1 Replies

7. Shell Programming and Scripting

pattern match .com in awk script

guys ! I want to search .com,.html files ..... how do I match pattern...? here's wht I hv written if ( $i ~ /.com/ ) even escaping it doesn't help if ( $i ~ /\.com/ ) (2 Replies)
Discussion started by: shreeprabha
2 Replies

8. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

9. Shell Programming and Scripting

Awk script to match pattern till blank line

Hi, I need to match lines after a pattern, upto the first blank line. Searched in web and some forums but coulnt find the answer. where <restart_step> = 10 -- Execute query 20 -- Write the contents to the Oracle table 30 -- Writing Contents to OUTPUT... (7 Replies)
Discussion started by: justchill
7 Replies

10. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies
Login or Register to Ask a Question