String search using Regular Expresion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String search using Regular Expresion
# 1  
Old 11-11-2011
MySQL String search using Regular Expresion

Hi,

I am getting a string in the file, I want to parse the srting and extract the percentage from the string. Sample string are -
Code:
ASAD112_sd12.34%adnmfk
ASAsds_1.34%adnmfk
ASAdf 2 sd12.34%adnmfk
ASAD112_sd 12.34% adnmfk
ASAD112_sd12.34% adnmfk

I want to extract the numeric value which end with %. Like
Code:
12.34%
1.34%
12.34%
12.34%

I need one liner regular expresion which I can use in VI editor to replace all this string by result (percentage amount).

Thanks in advance.
Vipin

Last edited by Scott; 11-11-2011 at 05:04 AM.. Reason: Code tags, please...
meetvipin
# 2  
Old 11-11-2011
Code:
:%s/[^_\s]*[^0-9]*\([0-9]*\.[0-9]*%\).*/\1/

# 3  
Old 11-11-2011
excellent!!!

Can you please explain this, how it's working?
meetvipin
# 4  
Old 11-11-2011
Quote:
Originally Posted by meetvipin
excellent!!!

Can you please explain this, how it's working?
Code:
%s -> every line

Code:
[^_\s]* -> any chars not specified in '_' and '\s' character set  ( \s-> it equals to these  [ \t\r\n\v\f]  ) 
equals to
ASAD112
ASA
ASAdf 2
ASAD112
ASAD112

Code:
[^0-9]* -> any chars not specified in digits
_sd
sds_
sd
_sd
_sd

Code:
\([0-9]*\.[0-9]*%\) -> groping chars (save that char groups inside the parentheses) 
12.34%
1.34%
12.34%
12.34%
12.34%

Code:
\(..\) -> escape the literal parantheses for use backslahs

Code:
.* -> any chars
adnmfk
adnmfk
adnmfk
 adnmfk
 adnmfk

Code:
\1 -> (write char groups inside the parentheses )

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

awk regular expression search

Hi All, I would like to search a regular expression by passing as an i/p variableto AWK. For Example :: 162.111.101.209.9516 162.111.101.209.41891 162.111.101.209.9516 162.111.101.209.9517 162.111.101.209.41918 162.111.101.209.9517 162.111.101.209.41937 162.111.101.209.41951... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

How can awk search a string without using regular expression?

Hello, Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. just represent for "", not "A" or "a" . I don't want to add backslash . (2 Replies)
Discussion started by: 915086731
2 Replies

6. Shell Programming and Scripting

Help on the regular expresion =~

my $hw_plf_desc = `grep hw_platform $NODE_CFG_FILE`; if($hw_plf_desc =~ /Netra X4270 X4446A M2 /) Could someone explain the use of =~ .... this works only for perl . What is the alternate for the same in shell . Could any one convert this to shell script (7 Replies)
Discussion started by: frintocf
7 Replies

7. Shell Programming and Scripting

KShell regular expresion

Hi, I would like to know if the parameter i am passing to a shell script is contain the following charachter : ASM. I belive that i should use regular expresion here. Can one help ? Bellow is the "if statment" i need to fix with the reg exp: if ; then #echo "IT IS AN RDBMS... (4 Replies)
Discussion started by: yoavbe
4 Replies

8. Shell Programming and Scripting

Regular expresion

Here's my script read number if echo $number | grep "" I want this "if" statement to return true only when numbers without letters is matched. For example 45 - true, 923 - true, r5 - false, tg/f - false and so on. In this script even a single digit number like "3" returns false. Thanks. (1 Reply)
Discussion started by: eXPlosion
1 Replies

9. Shell Programming and Scripting

Variable regular expresion in awk.

:confused: Is there any way to use in awk a regular exprexion with a format not previusly known? I mean something like /VAR/ ,obviously VAR is the variable exprexion. Thak you all in advance. (4 Replies)
Discussion started by: Klashxx
4 Replies

10. UNIX for Dummies Questions & Answers

regular expresion question

I receive windows files via the internet on my solaris server. Since unix doesn't handle blanks well I change the blanks to ? which works just fine. I take these files and ftp them to windows so our analysts can work with them. Recently I received a file with the following structure: ... (3 Replies)
Discussion started by: gillbates
3 Replies
Login or Register to Ask a Question