shell to match a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell to match a pattern
# 1  
Old 02-05-2009
shell to match a pattern

/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE

the idea is to match a env varialbe name, so it could be between ${ } or a single $
Perl can do it with: perl -ne '/\$\{?(\w+)\}?/;print $1'
What shell can do?

Last edited by honglus; 02-05-2009 at 08:33 PM..
# 2  
Old 02-05-2009
Quote:
Originally Posted by honglus
/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE

the idea is to match a env varialbe name, so it could be between ${ } or a single $
Perl can do it with: perl -ne '/\$\{?(\w+)\}?/;print $1'
What shell can do?

Where is the string you wnat to match? Is it in a file or is it already in a variable?

If it's in a variable:

Code:
case $var in
     *\${INSTANCE}*| *\$INSTANCE* )
        echo MATCH;;
     *) echo NO MATCH ;;
esac

If it's in a file or the output of a command, use grep:

Code:
grep -e '${INSTANCE}' -e '$INSTANCE'

# 3  
Old 02-05-2009
Quote:
Originally Posted by cfajohnson
I want to return the word between ${} or after $, but I don't know the word name.

example of Perl result


Code:
 
$ echo '/data}/{bla/${INSTANCE}' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE
 
$ echo '/data}/{bla/$INSTANCE' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE

# 4  
Old 02-05-2009
Quote:
Originally Posted by honglus
I want to return the word between ${} or after $, but I don't know the word name.

example of Perl result

Code:
 
$ echo '/data}/{bla/${INSTANCE}' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE
 
$ echo '/data}/{bla/$INSTANCE' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE


You haven't answered the question:
Where is the string you want to match?
Is it in a file?
Is it already in a variable?
Is it the output of a command?
Are you looking for it in a single line? Or in a file containing more than one line?
# 5  
Old 02-05-2009
It's better if you provide more details but hope this helps:

Code:
echo '/data}/{bla/${INSTANCE}' | sed 's/.*${*\([A-Za-z0-9]\{1,\}\)}*.*/\1/'

# 6  
Old 02-05-2009
Quote:
Originally Posted by cfajohnson
You haven't answered the question:
Where is the string you want to match?
Is it in a file?
Is it already in a variable?
Is it the output of a command?
Are you looking for it in a single line? Or in a file containing more than one line?
supposing it is a file has 1 line only
/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE
# 7  
Old 02-05-2009
Quote:
Originally Posted by angheloko
It's better if you provide more details but hope this helps:

Code:
echo '/data}/{bla/${INSTANCE}' | sed 's/.*${*\([A-Za-z0-9]\{1,\}\)}*.*/\1/'

Smilie
Thanks the expression worked for both. Can you expain how it worked?
i don't understand usage of "\1"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. 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

3. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

4. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 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

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

10. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies
Login or Register to Ask a Question