13

I have seen the below pattern is used in several places (even on sof) ... Why is it so popular? Because people are copy-pasting the first google search result in their answers, blogs and code, which are in turn picked up by search engines, which brings even more people to copy-paste it, generating an infernal vortex which finishes by driving off the ...


12

It should not be used in production. For example "email me"@contoso.com is a syntactically valid email address but will not be matched by that naïve RE. See RFC5322 section 3.4.1 for the definitive grammar. Annoyingly perhaps, there is no BRE or ERE that can match that grammar definition, but you can get very close. However, a PCRE will do the ...


5

Without looking too much at the other commands in the pipeline, you can make find avoid entering any directory that has a hidden name: find . ! -path . -name '.*' -prune -o -name '* *' -print0 | ... This will remove all directories that have . at the start of their names from the search tree, except for the . directory (the current directory). It will also ...


3

If the point is to remove comments from POSIX sh scripts, note that only the ones marked as YES in the code below are comments: echo 1 # YES echo 2 $# NO foo# NO echo 3;#YES # YES cat << E # NO E echo 4 " # NO \" # NO" \" # YES echo "5 # NO $(echo 6 # YES ) `echo 7 \" # NO \"` " eval 'echo 8 # NO, then YES' (and ...


3

Just use zsh's zmv: #! /bin/zsh - autoload -Uz zmv zmv '(**/)(* *)' '$1${2// /_}' zmv skips hidden files and files in hidden dirs by default (unless you pass a (#qD) qualifier to the first argument), it processes the files depth first (your sort -rz is not guaranteed to work for that and -prune is not compatible with -depth) we don't need to call dirname/...


3

The \s does not works inside brackets. For example, echo " screenshot" | grep -E "^\sscreenshot" works, but echo " screenshot" | grep -E "^[\s]screenshot" doesn't work. So, instead of ^[\sa-z0-6\.]+screenshot, you can use either echo "await page.screenshot" | grep -E "^([a-z0-6.]|\s)+screenshot" or ...


3

awk 'NR==FNR{a[$1];next}{n=split($NF,b,";");for(i=1;i<=n;i++){if(b[i] in a){print;break}}}' file2 file1 Unwinded version: awk ' NR==FNR{a[$1];next} { n=split($NF,b,";") for(i=1;i<=n;i++){ if(b[i] in a){print;break} } } ' file2 file1 split($NF,b,";") splits the last field on the semicolon and ...


2

I think this is cleaner: [ -n "$(df --output=ipcent | awk -F '%' 'NR>1 && $1>80')" ] && echo "80% hit." df --output=ipcent outputs only the inodes percentage column. awk -F '%' 'NR>1 && $1>80' skips the header (with NR>1) and checks if the percentage is greater than 80%, printing the line if yes. ...


2

Several grep implementations have a -P option that allows you to use Perl-like regular expression and in particular perl's negative look ahead (?!...) operator. So with those, to look for screenshot occurrences that are not found after //, you can do: grep -P '^(?:(?!//).)*screenshot' There's no \s in standard basic regular expressions. Inside a bracket ...


2

You seem to be assuming a formal and precise definition of "word" in relation to regular expressions, while its meaning is actually implementation-dependent. Indeed, in the paragraph "Regular Expression Nomenclature", "Flavor", the book you are quoting states that Even if two programs both support ⌈\<···\>⌋, they might ...


2

According to the most recent Java API documentation, the dot (.) character matches "Any character (may or may not match line terminators)". It does not say it will match zero occurrences. After reading your linked article, it also does not state that the dot will match zero occurrences. To get your regular expressions to match zero occurrences, ...


2

regex=/^[a-zA-Z]+\S+/ means "compare $0 to /^[a-zA-Z]+\S+/ and save the result in the variable regex" so regex will be 1 or 0 as a result of that assignment and since we're in the BEGIN section where no lines have been read and so $0 is still empty, it's equivalent to regex=0. \S is a GNU awk extension which means [^[:space:]] so if you were using ...


1

That's not an awk pattern (looks like perl): regex=/^[a-zA-Z]+\S+/ something like this would work: regex="^[a-zA-Z]+[^[:space:]]+" Also, your pattern should match against $0 (not $1). $0 is the whole line. $1 is the first field (think of it as the first word on each line: that may not have a # to match against in the first column). With those ...


1

try with sed -r 's/(=)([^=]*)(&)/\1\3/g;s/(=)([^=]*)$/\1/' where s/(=)([^=]*)(&)/\1\3/g perform substitution on firsts param=value pattern, but stop on = (to avoid greedy match) s/(=)([^=]*)$/\1/ substitute last pattern


1

If you are using the common GNU version of df, I suggest the following #!/bin/bash df -P | { full=() read header # discard the first line while read fs blocks used free cap mount do cap=${cap%?} # remove the last character [ $cap -ge 80 ] && full+=("$mount%$cap") done [ ${#full[@]} -ne 0 ] && ...


1

That part from the GNU grep manual says: \< Match the empty string at the beginning of word. \> Match the empty string at the end of word. They match at the start and end of a "word", so \<bar matches the string foo bar, or just bar, but not foobar. The matches are described as matching an empty string, since when matching \<bar ...


1

See if this shell script works for you: #!/bin/bash if [ $# -eq 0 ]; then echo "You have to specify a file. Exiting..." exit 1 fi if [ ! -r $1 ]; then echo "File '$1' doesn't exist or is not readable. Exiting..." exit fi # count every line lines=$(wc -l $1 | awk '{print $1}') echo "$lines total lines." # ...


1

Based on the rules specified we distinguish 5 kinds of words: double quoted words (they can include escaped double quotes as well) "... \"... " single quoted words '...' they will not have an included single quote. backslash quoted word \.basically any escaped char. non comment starting char [^'#"] what remains is a comment. #! /...


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