234

sed -i -E "s/(<username>.+)name(.+<\/username>)/\1something\2/" file.xml This is, I think, what you're looking for. Explanation: parentheses in the first part define groups (strings in fact) that can be reused in the second part \1, \2, etc. in the second part are references to the i-th group captured in the first part (the numbering starts ...


126

I think, it's better to replace \n symbol with some other symbol, and then work as usual: e.g. not-worked source code: cat alpha.txt | sed -e 's/a test\nPlease do not/not a test\nBe/' can be changed to: cat alpha.txt | tr '\n' '\r' | sed -e 's/a test\rPlease do not/not a test\rBe/' | tr '\r' '\n' If anybody doesn't know, \n is UNIX line ending, \r\n - ...


120

Unfortunately, for historical reasons, different tools have slightly different regular expression syntax, and sometimes some implementations have extensions that are not supported by other tools. While there is a common ground, it seems like every tool writer made some different choices. The consequence is that if you have a regular expression that works in ...


90

agrep can do it with this syntax: agrep 'pattern1;pattern2' With GNU grep, when built with PCRE support, you can do: grep -P '^(?=.*pattern1)(?=.*pattern2)' With ast grep: grep -X '.*pattern1.*&.*pattern2.*' (adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's ...


79

Your regular expression doesn't mean what you think it does. It matches all lines starting (^) with one (1) repeated zero or more (*) times. All strings match that regular expression. grep '^1' does what you want.


76

The syntax \t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support \t for tab and instead treats \t as meaning backslash followed by t. There are ...


70

And if you want to search three folders named foo, bar, and baz for all *.py files, use this command: find foo bar baz -name "*.py" so if you want to display files from dir1 dir2 dir3 use find dir1 dir2 dir3 -type f try this find . \( -name "dir1" -o -name "dir2" \) -exec ls '{}' \;


59

sed -n "s/^.*foobar\s*\(\S*\).*$/\1/p" -n suppress printing s substitute ^.* anything before foobar foobar initial search match \s* any white space character (space) \( start capture group \S* capture any non-white space character (word) \) end capture group .*$ anything after the capture group \1 substitute everything with ...


58

All things considered, gobbling the entire file may be the fastest way to go. Basic syntax is as follows: sed -e '1h;2,$H;$!d;g' -e 's/__YOUR_REGEX_GOES_HERE__...' Mind you, gobbling the entire file may not be an option if the file is tremendously large. For such cases, other answers provided here offer customized solutions that are guaranteed to work on ...


56

Oddly enough, \n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (Ctrl+@). Historically, vi replaces ^M (Ctrl+M) as the line-ending, which is the newline. vim added an extension \r (like the C language) to mean the same as ^M, but the developers chose to make \n mean null when replacing text. This is inconsistent with its use in ...


47

This is more correct: find . -iregex '.*\.\(jpg\|gif\|png\|jpeg\)$'


47

Yes, it is [[:digit:]] ~ [0-9] ~ \d (where ~ means approximate). In most programming languages (where it is supported) \d ≡ `[[:digit:]]` # (is identical to, it is a short hand for). The \d exists in less instances than [[:digit:]] (available in grep -P but not in POSIX). Unicode digits There are [many digits in UNICODE](http://www.fileformat....


45

The reason lies in the way RegEx matches are processed (see here, e.g.): The string is evaluated from left to right, and - except for backreferences - every single symbol in the string must be matched by a token in the regular expression (which in the simplest case is the literal symbol itself), although the token can be implicit thanks to repetition ...


42

By default sed uses POSIX Basic Regular Expressions, which don't include the | alternation operator. You can switch it into using Extended Regular Expressions, which do include | alternation, with -E (or -r in some older versions of some implementations). You can use: echo 'cat dog pear banana cat dog' | sed -E -e 's/cat|dog/Bear/g' and it will work on ...


41

grep -Eow '\w{10}' | grep -v '\(.\).*\1' excludes words that have two identical characters. grep -Eow '\w{10}' | grep -v '\(.\)\1' excludes the ones that have repeating characters. POSIXly: tr -cs '[:alnum:]_' '[\n*]' | grep -xE '.{10}' | grep -v '\(.\).*\1' tr puts words on their own line by converting any sequence of non-word-characters (...


40

Remove quotes if ! [[ "$scale" =~ ^[0-9]+$ ]] then echo "Sorry integers only" fi


40

You can turn off regex mode by hitting Ctrl+R before typing the pattern: ^R Don't interpret regular expression metacharacters; that is, do a simple textual comparison.


40

You seem to have defined the right regex, but not set the sufficient flags in command-line for grep to understand it. Because by default grep supports BRE and with -E flag it does ERE. What you have (look-aheads) are available only in the PCRE regex flavor which is supported only in GNU grep with its -P flag. Assuming you need to extract only the matching ...


39

To show only the first match with grep, use -m parameter, e.g.: grep -m1 pattern file -m num, --max-count=num Stop reading the file after num matches.


39

Did you try the following? ls -1 | grep "^1" That is, remove the *, which basically tells grep, find zero or more occurances of the ^1 expression. In other words: match the lines that start with a 1, or not.


38

grep -xv '.\{8,63\}' <input >output grep's -x switch denotes a whole line match - which is to say that any pattern matched must define a line from head to tail. doing... grep -x pattern ...is generally equivalent to... grep ^pattern$ grep's -v switch negates a pattern's influence on line-selection. generally doing... grep pattern ...will only ...


38

Regular expressions come in many different flavours. What you are showing is a Perl-like regular expression (PCRE, "Perl Compatible Regular Expression"). grep does POSIX regular expressions. These are basic regular expressions (BRE) and extended regular expressions (ERE, if grep is used with the -E option). See the manual for re_format or regex or ...


35

There are different regular expression dialects; some (e.g. Perl's) do not require backslashes in the quantification modifier (\d{2}), some (e.g. sed) require two (\d\{2\}), and in Vim, only the opening curly needs it (\d\{2}). That's the sad state of incompatible regular expression dialects. Also note that for matching exact numbers, you have to anchor the ...


35

This is what the cut command is for. cut -d';' -f2-


34

ANSI escape codes start with ESC or character \033 Color codes (a subset of them), are \033[Xm where X is a semicolon-separated list of digits, possibly empty (this means a reset). m is a literal m. Since I keep forgetting these codes myself I "documented" them on https://github.com/seveas/hacks/blob/master/ansi.py. As for a sed expression: I'd go for s/\...


34

\< and \> match empty string at the begin and end of a word respectively and only word constituent characters are: [[:alnum:]_] From man grep: Word-constituent characters are letters, digits, and the underscore. So, your Regex is failing because / is not a valid word constituent character. Instead as you have spaces around, you can use -w option ...


33

Here's a sed one that will give you grep-like behavior across multiple lines: sed -n '/foo/{:start /bar/!{N;b start};/your_regex/p}' your_file How it works -n suppresses the default behavior of printing every line /foo/{} instructs it to match foo and do what comes inside the squigglies to the matching lines. Replace foo with the starting part of the ...


31

Here are a few options, all of which print the desired output: Using grep with the -o flag (only print matching part of line) and Perl compatible regular expressions (-P) that can do lookarounds: printf "this is (test.com)\n" | grep -Po '(?<=\().*(?=\))' That regex might need some explaining: (?<=\() : This is a positive lookbehind, the general ...


31

It's a shame that you can't do global matching in bash. You can do this: global_rematch() { local s=$1 regex=$2 while [[ $s =~ $regex ]]; do echo "${BASH_REMATCH[1]}" s=${s#*"${BASH_REMATCH[1]}"} done } global_rematch "$mystring1" "$regex" 1BBBBBB 2AAAAAAA This works by chopping the matched prefix off the string so the next ...


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