Tag Info

Hot answers tagged

8

Inside [...], backslash is not special. [\[] matches both backslash and [1. If you want to include the ] character in the set, you have to make sure it's first: []X] matches ] or X while [X]] would match X followed by ] (and [X\]] would match X or \ followed by ]). To exclude it, it has to be right after ^: [^]X] is any character but ] or X. So, in your ...


6

You have three tools that can do regular expressions. These all assume that $in contains na-examplename-01. grep $ printf "%s\n" "$in" | ./grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' na-examplename-01 sed $ printf "%s\n" "$in" | ./sed -n '/^[a-z]\{2,3\}-[a-z]\+[0-9]*-[0-9]\+$/p' na-examplename-01 awk $ printf "%s\n" "$in" | ./awk ...


5

grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function. The ^ prepended to the argument is a standard regular expressions modifier that matches the beginning of a line -- this way you can ensure that ...


4

Some more choices. I have saved your example text in file for simplicity. grep and PCREs: $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' file purple yes violet affirmative To get them on the same line, just parse. For example $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' | paste -d" " - - – purple yes violet affirmative sed $ sed ...


4

Different tools and versions thereof support different variants of regular expressions. The documentation of each will tell you what they support. Standards exist so that one can rely on a minimum set of features that are available across all conforming applications. For instance, all modern implementations of sed and grep implement basic regular ...


4

awk sounds like a good candidate: input='whatever even spaces and newlines xxx-blah12-0' # should not match input='na-examplename-01' # should match if LC_ALL=C awk 'BEGIN{exit(!(ARGV[1] ~ /^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$/))}' "$input" then echo it matches else echo >&2 it does not match fi


4

You could use grep in extended regex mode like this: echo na-examplename-01 | grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' You should use the interval parameter to make this more easy to read. [a-z][a-z]|[a-z][a-z][a-z] would be [a-z]{2,3}. [a-z]+ is the same as [a-z][a-z]* For the grep snytax, take a look at ...


4

If you reverse the file, you can add a line the first time you see something: tac lists.txt | awk -v l1="list1" -v val1="something new" \ -v l2="list2" -v val2="another thing" ' index($0, l1"[i++]") && !found1 { printf "%s[i++] = \"%s\";\n", l1, val1 found1 = 1 } index($0, l2"[i++]") ...


3

Your problem comes from the .*. If you only match every character that is not a ' or a " it will work: sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\x27\x22]*(\x27|\x22)) \]/[\1]/g" file.php Even better (to take possible " or ' into account): sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\2]*(\2)) \]/[\1]/g" file.php


3

You need two things to match the line breaks (hence multiple lines) using grep : -z option of newer GNU grep, it will cause the lines to be separated by ASCII NUL rather than line breaks (?s) is called DOTALL modifier (with grep -P), it will cause the grep to match the line breaks (LF/CR) by . (dot) So in your case the following should work: grep -aPoz ...


3

You are correct - \w is part of PCRE - perl compatible regular expressions. It's not part of the 'standard' regex though. http://www.regular-expressions.info/posix.html Some versions of sed may support it, but I'd suggest the easiest way is to just use perl in sed mode by specifying the -p flag. (Along with the -e). (More detail in perlrun) But you don't ...


3

:%s/\${ARRAY1\[@\]}/$1/ worked for me. Apparently, you must escape [ and ] but not { and }. I always use / instead of : as seperation, but %s:\${ARRAY1\[@\]}:$1:g works as well.


3

sed uses regular expressions. These are different from patterns ("globs") that the shell uses. Notice that the following doesn't work: $ echo hostname=abc | sed "s/\<hostname=*\>/hostname=int1/" hostname=int1=abc But, the following does: $ echo hostname=abc | sed "s/\<hostname=.*\>/hostname=int1/" hostname=int1 You need a . before the *. ...


3

Since you're trying with sed ranges, here's one possible way to do it. The lines in your additional-values.txt follow the same pattern: KEY[i++] = 'VALUE'; //etc and as far as I can tell, each line should be inserted in a range that is always delimited by var KEY = new Array(); and an empty line so you could process additional-values.txt and turn it ...


2

sed '/match_string1/{ :1 N /\n.*match_string2/s/\n/; / t1 P D }' When script met line with match_string1 it add next line to pattern and check if in that added line there is match_string2 if so they substitute newline sign by ; and add next line to check. If there is not match_string2 (so substitution ...


2

Base on your output, I offer some points to improve your regex: You can match explicit at beginning of line, using ^ If you don't use the match groups, then you don't need parentheses or using non-capturing group ?: \s includes \t, so you only need \s. The final regex can be m/^(?:[+-]?\d+\.\d+)\s+(?:[+-]?\d+\.\d+)/


2

With perl regular expressions, you can do non-greedy matches: perl -pe 's/===.*?<!!!>//g' file Edit 1: If you want to insert some text use that: perl -pe 's/===.*?<!!!>/Text you want to insert/g' file Edit 2: If it has to handle multiline comments as well use that: perl -pe 'BEGIN{undef $/;} s|===.*?</!!!>|insert|gs' file We have ...


2

Try this way: :%s:\${ARRAY1\[@\]}:$1:g


2

Using Awk: awk -v RS="," -F= '/GRAPE/||/FRUIT/ {printf "%s ", $2}' Changes the record separator from a new line to , and the field separator from a space to a =, then match lines that contain the pattern GRAPE or FRUIT and print the second matching field on the same line separated by a space. Result: purple yes


2

If the lists in your input file is separated by a blank line, you can use a tool that lets you set the record separator (what defines a "line") to consecutive newlines. For example, in Perl (assuming your replacements are in a file called additions): perl -ne 'BEGIN{## Open the additions file open($fh,"additions"); ...


2

As @cuonglm said, * means 0 or more. Since you;ve tagged this with Perl, I assume you're using Perl's regex engine which means you can use +: 1 or more. So, the substitution you're after would be one of: Match alphanumeric characters or underscores: s/\w+&\w+/\$$&\$/g Match letters only (note the i which makes the match case insensitive): ...


2

sed -n '/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc If you wants newline after each table: sed -n '/^\s*Table:/G;/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc or sed '/-\{10,777\}/,/^\s*Table:/! d;/^\s*Table:/G' LaTeX.doc


2

I'll edit this if you update the question but I think you're looking for something like this: perl -007lne '@F=(/-{7,}.*?Table:.*?\n(?=\n)/gsm); print join "\n", @F' file.tex Explanation -007 : slurp the entire file -lne : add a new line to each print call, process the input file, and run the script given by -e. @F=(/pattern/gsm) : save all matches of ...


1

Given that your lists are separated by new lines such as this var list1 = new Array(); i = 0; list1[i++] = 'a'; list1[i++] = 'b'; list1[i++] = 'z'; var list2 = new Array(); i = 0; list2[i++] = 'a'; list2[i++] = 'b'; list2[i++] = 'z';\n And if additional-lists.txt looks like: list1[i++] = 'something new'; list2[i++] = 'another thing'; Then this ...


1

Try grep -o 'myregex.*stuff' file and for the second question grep -o 'myregex.*stuff' file | sort | uniq. The -o grep switch will print only the matches instead of printing the whole line that matches the regex.


1

You are referring to regex back-references. Please check these two references: http://stackoverflow.com/questions/4609949/what-does-1-in-sed-do http://www.gnu.org/software/grep/manual/html_node/Back_002dreferences-and-Subexpressions.html And see the output of grep '\([0-9]\)\1' /etc/services which will give you a resultset of lines where a digit is ...


1

I suspect that grep and sed are deciding differently when to apply the [] and when to expand the \w. In perl regex \w means any word character, and [] define a group to apply any of the characters within as a match. If you "expand" the \w before the [] it will be a character class of all the word characters. If, instead you do [] first you will have a ...


1

With sed which doesn't support non-greedy *, you'd need to use tricks like: sed 's/_/_u/g;s/|/_p/g;s/<!!!>/|/g s/===[^|]*|//g s/|/<!!!>/g;s/_p/|/g;s/_u/_/g' Or with some sed implementations: sed 's/<!!!>/\ /g; s/===[^\n]*\n//g; s/\n/<!!!>/g' To support multi-line matching (as per your edit), with recent versions of GNU ...


1

What you have done wrong is you're trying to parse XML with a regular expression. This is a bad idea - it sometimes works, but it makes brittle code. XML has a specification that allows for linefeeds, whitespace and tag nesting - all things that regex handle very badly. The real answer is 'use a parser'. I would suggest perl and XML::Twig as options - ...


1

You have to backslash the slashes that don't work as separators, too, or use a different separator: sed "7 s%\<<Filevalue=.*/>\>%<Filevalue="true"/>%" Also, you can't include double quotes in double quotes without backslashing them, or switch to single quotes. Moreover, a word can't start with a <, so \<< never matches.



Only top voted, non community-wiki answers of a minimum length are eligible