Tag Info

Hot answers tagged

7

First, note that the single slash matches too much: $ echo $'eegg \n e.g.' | grep e\.g\. eegg e.g. As far as Bash is concerned, an escaped period is the same as a period. Bash passes on the period to grep. For grep, a period matches anything. Now, consider: $ echo $'eegg \n e.g.' | grep e\\.g\\. e.g. $ echo $'eegg \n e.g.' | grep e\\\.g\\\. e.g. $ ...


5

This regular expression will match HTML comments In particular: <!-- matches literal string "<!--" . matches any character * is a quantifier, it means "0 or more" of the previous character ? makes the regex non-greedy, so it matches as few times as possible --> matches literal "-->" So, your regexp against this text: blah <!-- ...


4

Anchor the expression to the end of the line: grep -E "\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$" If you add the PCRE option of -o to return only the captured group, you will see that your example matches and returns the desired match: grep -oE "(\-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}$)" -146.17516 Too, to catch matches for lines in a file with optional, ...


4

The output is the same only for your string, but in general those regular expressions do different things. Let's modify your example a little by adding second pattern e,g, (with comas), third e\.g\. (dots), fourth e\,g\, (comas), and -o option to grep to print only matched parts. In the following case . match any char (notice '' around e.g., I will come to ...


3

When you do a grep e\.g\., the shell is consuming the backslash, thus you are doing a grep e.g., which matches. When you do a grep e\\.g\\., the shell is again consuming a slash, and now you are doing a grep e\.\g., which again matches. Now, a backslash to the shell looks like \\. So, when you have \\, the first one is an escape sequence, the second is a ...


3

If you are trying to replace in File1 everything inside double quotes with new image names taken from File2 then I would use awk: awk -F'"' 'NR==FNR{a[i++]=$1;next}{print $1 FS a[j++] FS $3}' File2 File1 The output is the following: <IMG SRC="/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif" /> <IMG ...


3

You could do this simply through Perl+regex combination. perl -pe 's/(\{(?:[^{}]|(?1))*\})(*SKIP)(*F)|,/|/g' file Example: $ perl -pe 's/(\{(?:[^{}]|(?1))*\})(*SKIP)(*F)|,/|/g' file (999969|2500|"777777888"|0|"45265"|"65522"|NULL|10001|2014-09-15 10:27:07.287|2014-09-15 10:28:49.085|2014-09-15 ...


3

Try this: pcregrep -M '\\begin{question}(.|\n)*?\\end{question}' Explanation: pcregrep: grep with Perl-compatible regular expressions -M: Allow patterns to match more than one line (.|\n)*?: any normal character . or new line \n matched zero or more times ., in non-greedy mode ?. Result: \begin{question} {You get a patient. What do you notice ...


3

<!-- matches the characters <!-- literally . matches any character (except newline) *? is a quantifier that matches the preceding regex between zero and unlimited times, as few times as possible, expanding as needed [lazy] --> matches the characters --> literally Answer from the regex tester http://regex101.com/r/lA1bH5/2 Note that * matches ...


3

There's no way to chain the Bash built-in parameter expansion, but of course this can be done in a single line with external tools like sed: $ sed 's/[\.\/-]//g;s/^\(.\{16\}\).*/\1/' <<< "./my-site.sub.domain.com" mysitesubdomainc Unfortunately this very quickly turns into unmaintainable code, and is probably less efficient than using Bash ...


2

From the man page of useradd (8): It is usually recommended to only use usernames that begin with a lower case letter or an underscore, followed by lower case letters, digits, underscores, or dashes. They can end with a dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]? On Debian, the only constraints are that usernames must neither ...


2

You can use following sequence to only retain the account numbers (cudo's to J.D.Mohr) note the space after the r in the command :%norm $F,d$Bhv0r This assumes that there's only one , after the number you want to retain Breakdown : -> Enter command mode %norm -> Applies a normal command to the entire file $ -> Jump to end of line F, ...


2

To replace first 10 digits with * if, and only if, the number has exactly 14 digits: sed 's/\([^0-9]\)[0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g' Example: $ echo 'foo bar 38012345678901 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2, 28012345678901,,,,, 3801234567890123456789 stuff' | \ sed ...


2

If I understand correctly, you're looking for something like this (in bash): #!/usr/bin/env bash ## avoid errors if a directory has no *tex files shopt -s nullglob directories=("Cardiology" "Rheumatology" "Surgery"); ## Change this to set whichever options you want. printf "%s\n%s\n" "\documentclass{YOURCLASS}" "\begin{document}" for directory in ...


2

Why not use zsh? $ echo ${${mysite//[\.\/-]/}:0:16} mysitesubdomainc (I slightly modified you code)


1

I have no idea what you're trying to do there but then my sed-fu is not that strong so I guess you're using some arcane syntax I am unaware of. Since I can't tell you what's wrong with your sed (but an educated guess is that the special characters contained in your replacement strings (/,? etc) are causing problems), I will instead offer a perl alternative: ...


1

Preceding the first regexp with a space: grep -E "\s+-?[[:digit:]]{1,3}\.[[:digit:]]{0,5}" should do the trick, since it excludes the match at the beginning of the line. If you want just the 4th column, you can easily achieve that with either GNU sed: sed -r 's/^\S+\s+\S+\s+\S+\s+(\S+)(\s.*|)$/\1/' where \s is any whitespace character (space, tab and ...


1

You can try a loop in sed: $ sed -n '{:loop p; n; /SOMETHING/b loop; q}' test.txt blah blah SOMETHING blah blah blah blah SOMETHINGblahblahblah SOMETHING blah blah This: :loop creates a label named loop p prints the current line n fetches the next line /SOMETHING/b loop branches to loop if line matches /SOMETHING/ q if the branch doesn't happen. This ...


1

In double quotes, you need to backslash backslashes, i.e. double the backslash before the dot. system("grep '^.*\\.[a-zA-Z0-9][a-zA-Z0-9]*\$' file.txt > file2.txt"); # ^ # | # Here.


1

Do you need it to be a pure regex solution, or just perlish? perl -lne 'print if(/^\\begin{question}/ .. /^\\end{question}/)' file


1

The general rule for username is its length must less than 32 characters. It depend on your distribution to make what is valid username. In Debian, shadow-utils 4.1, there is a is_valid_name function in chkname.c: static bool is_valid_name (const char *name) { /* * User/group names must match [a-z_][a-z0-9_-]*[$] */ if (('\0' == *name) || ...


1

I think the main difference between using regular expression is whether they require to match the whole string or not. In case, find and some other bash commands you have to match the whole string, while in sed, awk, grep and so on you have to match any part of the string. Other than that they are similar, but, of course, not identical. For example, when ...


1

case uses globs, which is a very simple pattern matching system similar to regular expressions. Some tools, like find, actually support both (via -name and -regex in this case). But the case is even more complicated: There are different flavours of regular expressions. Some tools support one, some several. You just have to check per tool and version ...



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