# Tag Info

0

The two commands produce the same output only for your input but otherwise they are different. For understanding of what is going on we have to know how is the parameter interpreted first by bash and then by grep. Escaping in bash \ is a special character which cancels special meaning of the following character including \ itself. If the following ...

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 ...

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 <!-- ... 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 ... 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 ...

0

The error is telling you that your sed command is wrong, not your regexp. You need a newline or semicolon to separate the s command from the following { command. Equivalently, you can put them in separate -e arguments. sed -e 's/.SRC="/Repository([^"])".*/\1/p' -e '{' -e 'r File1' -e 'd' -e '}' File2 This won't do what you want, though. It removes the ...

0

I came up with a few ways to do this in sed, but most broke in corner cases. One, however, doesn't: sed 's/^/\n/;:b /\n\n/!s/$$\n[^,{}]*$$,/\1|/;tb s/$$\n\n*$$$$[^{}]*[{}]$$/\2\1/ s/{$$\n$$/&\1/;s/$$}\n$$\n/\1/;tb s/\n//g' <<\DATA (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

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 ... 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: ... 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 ...

0

Don't be afaid it's a hard sed statement. But, it should respect cascading. Here is it in one line: sed -e 's/,/|/g;:a;s/{$$[^{}]*$$|$$[^{}]*$$}/{\1,\2}/g;ta;s/{$$[^{}]*$$}/<\1>/g;ta;:b;s/<$$[^<>]*$$>/{\1}/g;tb' file Here is the commented version: sed -e ' s/,/|/g; #replaces all commas (,) with ...

0

replace ,{ with |{ and }, with }| echo "THING1,{THING2,{THING3,}},THING4" | sed -re "s/,\{/|{/gi" | sed -re "s/},/}|/gi" results in THING1|{THING2|{THING3,}}|THING4

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 ... 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, ... 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 ...

0

This might not be as general as what you really want, but here’s a starting point: sed -n '/SOMETHING/!q;p' This says: check for match to /SOMETHING/.  If the line doesn’t match (using ! to invert the result of the test), then quit.  Otherwise, print this line and continue to the next line. This is not immediately flexible enough to allow you to do what ...

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 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 ... 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 ... 0 If for some reason you cannot use single quotes as suggested in Mikel's answer, you can temporarily turn off history expansion using set +H (turn it back on with set -H), as suggested by glenn jackman in comments. 0 As @l0b0 said, I don't think this can be done in a single step in the shell but it's simple enough to do with external tools (this assumes the string is saved in the variable$s): $perl -pe 's#[.\-/]##g' <<<$s | grep -oP .{16} mysitesubdomainc

2

Why not use zsh? $echo${${mysite//[\.\/-]/}:0:16} mysitesubdomainc (I slightly modified you code) 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

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, ... 0 As easy as it might be to want to use vim (or sed for this), awk is actually entirely capable of doing this type of matching and substitution all on its own:$ awk '{ sub(/^.* - /, ""); sub(/,.*$/, ""); print$0 }' file 23499 The above matches everything (with awk's built-in sub() function) from the beginning of the line to the hyphen and space before ...

0

If you would look for <account> (something like <12345>) and have have < and > only in <account>, as it looked like in the original version of the question, then this could work: %s/\v.*(\<.*\>).*/\1/ It matches < and > with anything between - which is in a group, and anything before and after in the line. That is ...

0

To get only the <account> string awk '{print $6}' file| sed 's/,//' >> newfile For possible future usage of the main data, this could be useful 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) || ...

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 ...

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 ...

0

offtopic, but good laugh: two "grammar" edits by users :D

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 ...

Top 50 recent answers are included