Sponsored Content
Top Forums Shell Programming and Scripting Need help with sed and regexp Post 302691213 by elixir_sinari on Friday 24th of August 2012 04:00:24 AM
Old 08-24-2012
From man page(s) for gawk:
Code:
       match(s, r [, a])       Return the position in s where the regular expression r occurs, or 0 if r is not present, and set
                               the  values  of RSTART and RLENGTH.  Note that the argument order is the same as for the ~ opera‐
                               tor: str ~ re.  If array a is provided, a is cleared and then elements 1  through  n  are  filled
                               with  the  portions of s that match the corresponding parenthesized subexpression in r.  The 0'th
                               element of a contains the portion of s matched by the entire regular  expression  r.   Subscripts
                               a[n,  "start"],  and  a[n,  "length"] provide the starting index in the string and length respec‐
                               tively, of each matching substring.

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

GnuWin32 sed 4.1.4 regexp matching

I am using GnuWin32 sed and am having trouble with the regexp - i.e., they don't behave the same way as in UNIX (POSIX and and all that). I have a stream of data, e.g.: 11111'222?'22'33?'333'44444'55555' I want to insert a \n after those apostrophes that are *not* preceded by a ?. ... (2 Replies)
Discussion started by: Simerian
2 Replies

2. Shell Programming and Scripting

regexp with sed again!!!

please help: I want to add 1 space between string and numbers: input file: abcd12345 output file: abcd 1234 The following sed command does not work: sed 's/\(+\)\(+\)/\1 \2/' file Any ideas, please Andy (2 Replies)
Discussion started by: andy2000
2 Replies

3. Shell Programming and Scripting

Regexp and sed problem

Basically it should identify what ever is in between /*< >*/ (tags) and replace dbname ending with (.) with the words in between the tags i.e. DELETE FROM /*<workDB>*/epd_test./*<multi>*//*<version>*/epd_tbl1 ALL; into DELETE FROM... (4 Replies)
Discussion started by: sol_nov
4 Replies

4. Shell Programming and Scripting

Help regarding behavior sed regexp query

Hi all, I have one question regarding sed regexp (or any regexp in general), I have some path like this C:/Abc/def/ghi/jkl in a file file1 Now if i use following code cat file1 | sed 's#\(.*\)/.*#\1#' Now it give me following output C:/Abc/def/ghi, which is fine But i just... (2 Replies)
Discussion started by: sarbjit
2 Replies

5. Shell Programming and Scripting

sed regexp

Hi, I am not that good with reg exp and sed. But I was just looking at something the other day and came across a situation. When I ran the below command: echo "123 word" | sed 's/*/(&)/' the op was: (123) word But when I ran: echo "123 word" | sed 's/*/(&)/g' the o/p was: (123)... (4 Replies)
Discussion started by: King Nothing
4 Replies

6. UNIX for Dummies Questions & Answers

sed before and after regexp

Dear all i have the code which print 1 line of context before and after regexp, with line number sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h the code work well but any one can tell me what each letter mean {=;x;1!p;g;$!N;p;D;} also how i can print 2 line before and onle line after ... (2 Replies)
Discussion started by: soly
2 Replies

7. Shell Programming and Scripting

Help with Regexp replace in vim/sed

Hi! I have a file with multiple lines following this format: <a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a> The goal is to replace the title (not modifying the href) so the new lines looks like this: <a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a> The number of underscores in the... (2 Replies)
Discussion started by: ericjohansson
2 Replies

8. Shell Programming and Scripting

sed with multiple regexp

Dealing with Linux servers script would be in korn or bash shell syntax file is /etc/fstab I want to insert something if regex is matched to all matched lines in the /etc/fstab file and print out entire /etc/fstab file with the changes example 58.228.111.111:/my/file/system... (5 Replies)
Discussion started by: snoman1
5 Replies

9. Shell Programming and Scripting

sed regexp teaser

G'day, Here's a teaser for a sed guru, which I surely am not one, as even my basic sed skills are rusted from years of not practising ... lol Ok ... we have a string of digits such as: 632413741610252847552619172459483022433027602515212950543016701812771409213148672112 we want it split... (9 Replies)
Discussion started by: naderra
9 Replies

10. UNIX for Beginners Questions & Answers

sed with regexp in Linux

OFF 00280456 - 2014|1|2020_STATUS|GROUP_NAME|SUBGROUP_NAME|CLASS_NAME|GROUP_ID|SUBGROUP_ID I have above header in file. I need to replace 2020_STATUS with STATUS. 2020_STATUS is not always same but the column name will have STATUS all of the time. For instance column name might be 2019_STATUS... (1 Reply)
Discussion started by: jmadhams
1 Replies
regexp(n)						       Tcl Built-In Commands							 regexp(n)

__________________________________________________________________________________________________________________________________________________

NAME
regexp - Match a regular expression against a string SYNOPSIS
regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...? _________________________________________________________________ DESCRIPTION
Determines whether the regular expression exp matches part or all of string and returns 1 if it does, 0 if it does not, unless -inline is specified (see below). (Regular expression matching is described in the re_syntax reference page.) If additional arguments are specified after string then they are treated as the names of variables in which to return information about which part(s) of string matched exp. MatchVar will be set to the range of string that matched all of exp. The first subMatchVar will con- tain the characters in string that matched the leftmost parenthesized subexpression within exp, the next subMatchVar will contain the char- acters that matched the next parenthesized subexpression to the right in exp, and so on. If the initial arguments to regexp start with - then they are treated as switches. The following switches are currently supported: -about Instead of attempting to match the regular expression, returns a list containing information about the regular expression. The first element of the list is a subexpression count. The second element is a list of property names that describe vari- ous attributes of the regular expression. This switch is primarily intended for debugging purposes. -expanded Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as speci- fying the (?x) embedded option (see the re_syntax manual page). -indices Changes what is stored in the subMatchVars. Instead of storing the matching characters from string, each variable will con- tain a list of two decimal strings giving the indices in string of the first and last characters in the matching range of characters. -line Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, "[^" bracket expressions and "." never match newline, "^" matches an empty string after any newline in addition to its normal function, and "$" matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both -linestop and -lineanchor, or the (?n) embedded option (see the re_syntax manual page). -linestop Changes the behavior of "[^" bracket expressions and "." so that they stop at newlines. This is the same as specifying the (?p) embedded option (see the re_syntax manual page). -lineanchor Changes the behavior of "^" and "$" (the "anchors") so they match the beginning and end of a line respectively. This is the same as specifying the (?w) embedded option (see the re_syntax manual page). -nocase Causes upper-case characters in string to be treated as lower case during the matching process. -all Causes the regular expression to be matched as many times as possible in the string, returning the total number of matches found. If this is specified with match variables, they will contain information for the last match only. -inline Causes the command to return, as a list, the data that would otherwise be placed in match variables. When using -inline, match variables may not be specified. If used with -all, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression. Examples are: regexp -inline -- {w(w)} " inlined " -> in n regexp -all -inline -- {w(w)} " inlined " -> in n li i ne e -start index Specifies a character index offset into the string to start matching the regular expression at. The index value is inter- | preted in the same manner as the index argument to string index. When using this switch, "^" will not match the beginning of the line, and A will still match the start of the string at index. If -indices is specified, the indices will be indexed starting from the absolute beginning of the input string. index will be constrained to the bounds of the input string. -- Marks the end of switches. The argument following this one will be treated as exp even if it starts with a -. If there are more subMatchVars than parenthesized subexpressions within exp, or if a particular subexpression in exp does not match the string (e.g. because it was in a portion of the expression that was not matched), then the corresponding subMatchVar will be set to "-1 -1" if -indices has been specified or to an empty string otherwise. EXAMPLES
Find the first occurrence of a word starting with foo in a string that is not actually an instance of foobar, and get the letters following it up to the end of the word into a variable: regexp {mfoo(?!barM)(w*)} $string -> restOfWord Note that the whole matched substring has been placed in the variable "->", which is a name chosen to look nice given that we are not actu- ally interested in its contents. Find the index of the word badger (in any case) within a string and store that in the variable location: regexp -indices {(?i)mbadgerM} $string location This could also be written as a basic regular expression (as opposed to using the default syntax of advanced regular expressions) match by prefixing the expression with a suitable flag: regexp -indices {(?ib)<badger>} $string location This counts the number of octal digits in a string: regexp -all {[0-7]} $string This lists all words (consisting of all sequences of non-whitespace characters) in a string, and is useful as a more powerful version of the split command: regexp -all -inline {S+} $string SEE ALSO
re_syntax(n), regsub(n), string(n) | KEYWORDS
match, parsing, pattern, regular expression, splitting, string Tcl 8.3 regexp(n)
All times are GMT -4. The time now is 07:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy