Sponsored Content
Top Forums Shell Programming and Scripting expr command to extract words Post 302451119 by fpmurphy on Sunday 5th of September 2010 09:23:03 PM
Old 09-05-2010
Here is an example of how to do what you want to do:
Code:
$ A="foo=bar"
$ echo $A
foo=bar
$ expr "$A" : '\(.*\)=.*'
foo

From IEEE Std 1003.1:2008
Quote:
The ':' matching operator shall compare the string resulting from the evaluation of expr1 with the regular expression pattern resulting from the evaluation of expr2. Regular expression syntax shall be that defined in the Base Definitions volume of IEEE Std 1003.1-2001, Section 9.3, Basic Regular Expressions, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the first character of a string are matched by the regular expression) and, therefore, it is unspecified whether '^' is a special character in that context. Usually, the matching operator shall return a string representing the number of characters matched ( '0' on failure). Alternatively, if the pattern contains at least one regular expression subexpression "[\(...\)]", the string corresponding to "\1" shall be returned.
This User Gave Thanks to fpmurphy For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

expr command

I am looking for the correct syntax on the expr command in UNIX. I have a script that I am building at the moment. the script is creating file1 that is an actual .sql file that is going inside the oracle database to get some information in there. It take that information, puts it inside another... (2 Replies)
Discussion started by: wolf
2 Replies

2. UNIX for Advanced & Expert Users

How to extract two words at the same time.

Hi, Can anyone please let me know, how to extract two lines at the same time. In specific,I have a file containing list of devices, such as router1 and switch2 below. I want to get all the lines which has "#" and all the lines which has "down" router1#sh ip int br Interface ... (6 Replies)
Discussion started by: Aejaz
6 Replies

3. UNIX for Dummies Questions & Answers

using the expr command

Hi friends how can i execute expr $va1 * $var2 provided i m not supposed to use '/' also the nglob variable is turned off. (4 Replies)
Discussion started by: ashishj
4 Replies

4. Shell Programming and Scripting

expr command

Hi Can anyone explain me the usage of this command and the arguments used here and what will be the expected output : v_num=`expr nav_d_20100204_1759 : '*\(*\)'` what will be the value returned in v_num. Thanks in Advance!!! Regards Naveen Purbia (3 Replies)
Discussion started by: trying_myluck
3 Replies

5. Shell Programming and Scripting

Usage of grep command to extract only the words.

Dear Folks, I am a newbee to UNIX. I want to extract the SQLSTATE from a log file. For example the log file content is SQL0010N The string constant beginning with "' from table1 a, table 2" does not have an ending string delimiter. SQLSTATE=42603 when I give the the command as ... (4 Replies)
Discussion started by: dinesh1985
4 Replies

6. UNIX for Dummies Questions & Answers

Extract words to new file

Hi there, Unix Gurus Working with big listings of english sentences for my pupils, of the type: 1. If the boss's son had been , someone would have asked for money by now. 2. Look, I haven't a crime, so why can't you let me go? .... I wondered how to extract the words between brackets in... (7 Replies)
Discussion started by: eldeingles
7 Replies

7. Shell Programming and Scripting

expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is: $expr "${var}" : . # expr STRING : regrep where the "." is the grep wildcard for any single character. Whats wrong with my code here and is there a... (3 Replies)
Discussion started by: Tewg
3 Replies

8. Shell Programming and Scripting

Extract words from a pipe

Hello, Currently, I have this output from my application : ------------------------------------------------- Log viewer/Tmp1 (Jun 29 2011 09:48) ------------------------------------------------- BlalbalbaBlalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba..Blalbalba....... (3 Replies)
Discussion started by: acidoangel
3 Replies

9. Shell Programming and Scripting

Extract words before and after a certain word.

I have a sample text file with file name: sample.txt The text file has the following text. this is an example text where we have to extract certain words before and after certain word these words can be used later to get more information I want to extract n (a constant) words before and... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

10. UNIX for Beginners Questions & Answers

Extract Bracketed Words

Hi there, Unixers I need to extract ALL the words from a text which aresurrounded by square brackets. I am using this piece of code sed 's/.*\.*/\1/g' inputfile > outputfile but I only get one word for every paragraph, why? Please use CODE tags as required by forum rules! (7 Replies)
Discussion started by: eldeingles
7 Replies
EXPR(1P)						     POSIX Programmer's Manual							  EXPR(1P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
expr -- evaluate arguments as an expression SYNOPSIS
expr operand... DESCRIPTION
The expr utility shall evaluate an expression and write the result to standard output. OPTIONS
None. OPERANDS
The single expression evaluated by expr shall be formed from the operand operands, as described in the EXTENDED DESCRIPTION section. The application shall ensure that each of the expression operator symbols: ( ) | & = > >= < <= != + - * / % : and the symbols integer and string in the table are provided as separate arguments to expr. STDIN
Not used. INPUT FILES
None. ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of expr: LANG Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments) and the behavior of character classes within regular expressions. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. ASYNCHRONOUS EVENTS
Default. STDOUT
The expr utility shall evaluate the expression and write the result, followed by a <newline>, to standard output. STDERR
The standard error shall be used only for diagnostic messages. OUTPUT FILES
None. EXTENDED DESCRIPTION
The formation of the expression to be evaluated is shown in the following table. The symbols expr, expr1, and expr2 represent expressions formed from integer and string symbols and the expression operator symbols (all separate arguments) by recursive application of the con- structs described in the table. The expressions are listed in order of increasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be left-associative. +---------------+------------------------------------------+ | Expression | Description | +---------------+------------------------------------------+ |expr1 | expr2 | Returns the evaluation of expr1 if it is | | | neither null nor zero; otherwise, | | | returns the evaluation of expr2 if it is | | | not null; otherwise, zero. | +---------------+------------------------------------------+ |expr1 & expr2 | Returns the evaluation of expr1 if nei- | | | ther expression evaluates to null or | | | zero; otherwise, returns zero. | +---------------+------------------------------------------+ | | Returns the result of a decimal integer | | | comparison if both arguments are inte- | | | gers; otherwise, returns the result of a | | | string comparison using the locale-spe- | | | cific collation sequence. The result of | | | each comparison is 1 if the specified | | | relationship is true, or 0 if the rela- | | | tionship is false. | |expr1 = expr2 | Equal. | |expr1 > expr2 | Greater than. | |expr1 >= expr2 | Greater than or equal. | |expr1 < expr2 | Less than. | |expr1 <= expr2 | Less than or equal. | |expr1 != expr2 | Not equal. | +---------------+------------------------------------------+ |expr1 + expr2 | Addition of decimal integer-valued argu- | | | ments. | |expr1 - expr2 | Subtraction of decimal integer-valued | | | arguments. | +---------------+------------------------------------------+ |expr1 * expr2 | Multiplication of decimal integer-valued | | | arguments. | |expr1 / expr2 | Integer division of decimal integer-val- | | | ued arguments, producing an integer | | | result. | |expr1 % expr2 | Remainder of integer division of decimal | | | integer-valued arguments. | +---------------+------------------------------------------+ |expr1 : expr2 | Matching expression; see below. | +---------------+------------------------------------------+ |( expr ) | Grouping symbols. Any expression can be | | | placed within parentheses. Parentheses | | | can be nested to a depth of | | | {EXPR_NEST_MAX}. | +---------------+------------------------------------------+ |integer | An argument consisting only of an | | | (optional) unary minus followed by dig- | | | its. | |string | A string argument; see below. | +---------------+------------------------------------------+ Matching Expression The ':' matching operator shall compare the string resulting from the evaluation of expr1 with the regular expression pattern resulting from the evaluation of expr2. Regular expression syntax shall be that defined in the Base Definitions volume of POSIX.1-2008, Section 9.3, Basic Regular Expressions, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the first character of a string are matched by the regular expression) and, therefore, it is unspecified whether '^' is a special character in that context. Usually, the matching operator shall return a string representing the number of characters matched ('0' on failure). Alterna- tively, if the pattern contains at least one regular expression subexpression "[(...)]", the string matched by the back-reference expres- sion "1" shall be returned. If the back-reference expression "1" does not match, then the null string shall be returned. String Operand A string argument is an argument that cannot be identified as an integer argument or as one of the expression operator symbols shown in the OPERANDS section. The use of string arguments length, substr, index, or match produces unspecified results. EXIT STATUS
The following exit values shall be returned: 0 The expression evaluates to neither null nor zero. 1 The expression evaluates to null or zero. 2 Invalid expression. >2 An error occurred. CONSEQUENCES OF ERRORS
Default. The following sections are informative. APPLICATION USAGE
After argument processing by the shell, expr is not required to be able to tell the difference between an operator and an operand except by the value. If "$a" is '=', the command: expr $a = '=' looks like: expr = = = as the arguments are passed to expr (and they all may be taken as the '=' operator). The following works reliably: expr X$a = X= Also note that this volume of POSIX.1-2008 permits implementations to extend utilities. The expr utility permits the integer arguments to be preceded with a unary minus. This means that an integer argument could look like an option. Therefore, the conforming application must employ the "--" construct of Guideline 10 of the Base Definitions volume of POSIX.1-2008, Section 12.2, Utility Syntax Guidelines to pro- tect its operands if there is any chance the first operand might be a negative integer (or any string with a leading minus). EXAMPLES
The expr utility has a rather difficult syntax: * Many of the operators are also shell control operators or reserved words, so they have to be escaped on the command line. * Each part of the expression is composed of separate arguments, so liberal usage of <blank> characters is required. For example: +-----------------+-----------------------+ | Invalid | Valid | +-----------------+-----------------------+ |expr 1+2 | expr 1 + 2 | |expr "1 + 2" | expr 1 + 2 | |expr 1 + (2 * 3) | expr 1 + ( 2 * 3 ) | +-----------------+-----------------------+ In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. Newly written scripts should avoid expr in favor of the new features within the shell; see Section 2.5, Parameters and Variables and Section 2.6.4, Arithmetic Expansion. The following command: a=$(expr $a + 1) adds 1 to the variable a. The following command, for "$a" equal to either /usr/abc/file or just file: expr $a : '.*/(.*)' | $a returns the last segment of a pathname (that is, file). Applications should avoid the character '/' used alone as an argument; expr may interpret it as the division operator. The following command: expr "//$a" : '.*/(.*)' is a better representation of the previous example. The addition of the "//" characters eliminates any ambiguity about the division opera- tor and simplifies the whole expression. Also note that pathnames may contain characters contained in the IFS variable and should be quoted to avoid having "$a" expand into multiple arguments. The following command: expr "$VAR" : '.*' returns the number of characters in VAR. RATIONALE
In an early proposal, EREs were used in the matching expression syntax. This was changed to BREs to avoid breaking historical applica- tions. The use of a leading <circumflex> in the BRE is unspecified because many historical implementations have treated it as a special character, despite their system documentation. For example: expr foo : ^foo expr ^foo : ^foo return 3 and 0, respectively, on those systems; their documentation would imply the reverse. Thus, the anchoring condition is left unspeci- fied to avoid breaking historical scripts relying on this undocumented feature. FUTURE DIRECTIONS
None. SEE ALSO
Section 2.5, Parameters and Variables, Section 2.6.4, Arithmetic Expansion The Base Definitions volume of POSIX.1-2008, Chapter 8, Environment Variables, Section 9.3, Basic Regular Expressions, Section 12.2, Util- ity Syntax Guidelines COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 EXPR(1P)
All times are GMT -4. The time now is 06:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy