Tag Info

Hot answers tagged

5

try ls -1 | grep -E '^[A-Z]?([a-z][A-Z])*[a-z]?$' EDIT: as pointed very correctly by @mikeserv, this won't work for non-ASCII chars. And indeed they do happen quite often (e.g. music files with foreign titles for filename). So the more robust way is: ls -1 | grep -E '^[[:upper:]]?([[:lower:]][[:upper:]])*[[:lower:]]?$' In the following, I retain just ...


3

I'd like to find a regular pattern to include in a shell script so that n variables will contain the n arguments The following creates a shell array arglist that contains each of the arguments: $ readarray -t arglist < <(echo "\command{arg1, arg2 , arg3 }" | sed -n '/\\command/{ :a;/}/!{N;b a}; s/\\command{//; s/[ \n}]//g; s/,/\n/g; ...


3

Type this command: ls |grep -E '^([[:upper:]][[:lower:]])+[[:upper:]]?$|^([[:lower:]][[:upper:]])+[[:lower:]]?$' To break this down. ls is the main command It is piped to grep The switch -P or -E are telling grep to search using regular expressions (regex) Within the single quote ' and the parentheses () the pattern is layed out. Basically it reads ...


3

sed '/P1/,/P2/!d;/P2/q' ...would do the job portably by deleting all lines which do !not fall within the range, then quitting the first time it encounters the end of the range. It does not fail for P2 preceding P1, and it does not require GNU specific syntax to write simply.


3

In sed: sed -n '/P1/,/P2/p; /P2/q' -n suppresses the default printing, and you print lines between the matching address ranges using the p command. Normally this would match both the sections, so you quit (q) when the first P2 matches. This will fail if a P2 comes before P1. To handle that case, try: sed -n '/P1/,/P2/{p; /P2/q}'


3

with awk awk '/P1/{a=1};a;/P2/{exit}' file something P1 something content1 content2 something P2 something


3

sed -e 's/& PID=\$\!;$//' The $ toward the end anchors it to the end of the string.


2

A BRE doing what you're trying to in sed might look like: sed 's/ *\(\([^ ]*\) *\)\{[num]\}.*/\2/' ...or as an ERE for those seds which support it such as GNU and BSD versions: sed -E 's/ *(([^ ]*) *){[num]}.*/\2/p' ...either expression will begin its match at the first character of the [num]th group (where [num] is a positive integer) of [^ ...


2

In short: \K causes grep to keep everything prior to the \K and not include it in the match. It does not affect what comes after the \K(). This might be enough: " \K(.+)(?= )" Where (?= ) is a non capturing group. or perhaps better: " \K([^ ]+)(?= )" " \K(\w+)(?= )" or similar.


2

Try: $ find -type d -name '*dir*' -exec sh -c ' for d do for f in "$d"/*; do [ -f "$f" ] && mv -- "$f" /path/to/test done done ' sh {} +


2

mv "$dir_path"/* ... will not only move files but everything in "$dir_path". At least everything whose name does not start with a dot (hidden files). In bash you can change this with the option dotglob. But if the * expands nicely (matches everything but not too much for a command line) then you can use a shell for indirection: find . -type d -name "*dir*" ...


2

You misunderstand what ack does. It is a grep-analog, not a find-analog. It searches through the contents of files for the given pattern and not for file names matching the pattern. You should instead use find -regex '.*group.*producer.*' Or find -name '*group*producer*' If you insist on using ack (though I see no point in doing so), you can parse ...


2

set aAbBaBbA bAbBaA bbAb AAaBbAa BBBaaa aBaB l= u=; printf %s\\n "$@" | grep -E "^([${l:=[:lower:]}][${u:=[:upper:]}])+[$l]?$|^([$u][$l])+[$u]?$" OUTPUT: aAbBaBbA bAbBaA aBaB But see Pierre's answer for the (obviously) better version of this. Still, there is another way: grep -vE '[^[:alpha:]]|[[:lower:]]{2}|[[:upper:]]{2}|^$' ...though ...


2

In your original statement the problem is that you're mixing syntactic and literal quotes: the single quotes within the double quotes will be matched literally. You'll want to either simply remove them or mix quotes (not nest them). Untested examples: sed 's/foo/{bar}/' sed "s/foo/"'{bar}'"/" There are many flavours of regular expressions, and they all ...


2

With perl: perl -l -0777 -ne ' $n = 0; for (/\\command\{\s*(.*?)\s*\}/sg) { $n++; $i = 0; for $arg (split /\s*,\s*/, $_) { $arg =~ s/'\''/$&\\$&$&/g; print "arg${n}[$i]='\''$arg'\''"; $i++; } } print "n=$n"' the-file Would output something like: arg1[0]='arg1' arg1[1]='arg2' arg1[2]='arg3' n=1 Which ...


1

You can do this by listing both sets of packages and determining the difference between the two results: grep -Fxv -f <(apt list | grep unstable | cut -d/ -f1) <(apt list | grep experimental | cut -d/ -f1) This produces “experimental minus unstable”, and can be adapted to calculate any difference by changing unstable and/or experimental. Strictly ...


1

If you use egrep that mean grep with extended regexp syntax so to be able to transfer your pattern into sed you have to add parametr -r(--regexp-extended) or -E in some versions. Regarding to your expression you have extra \ after c so even with egrep it does not match Additionally your better use \1 instead $1 for reverse-matching. So final command could ...


1

IMHO Perl offers the easiest and the most flexible solution: perl -nE 'say $1 if m{/(\w+\d+\w+\d+)/};' input_file Please note that input_file is optional: STDIN will be filtered if/when input file name is not given.


1

Try this : perl -0ne 'print $& if /^Host.*?IdentityFile\s+\K[^\n]+/ms' file


1

If grepping over filenames in the current directory is all you need ack matches the contents of files, not their name. To find files whose name matches a regex, run ack over the list of filenames: ls | ack '.*group.*producer.*' What you did, namely ack -n '.*group.*producer.*' will instead look for files in the current directory containing something ...


1

All answers are good so far, but for this concrete example you can save your time and write a few characters less: sed 's/ & [^ ]*$//' or even better sed 's/ & \S*$//'


1

To remove the last occurrence of a pattern on a line, you can just squeeze it. sed 's/\(.*\)& PID=$!;/\1/' That will match as much as possible in \1 before matching the string so it will always strip only the last occurrence - regardless of whether or not the string you strip is actually at the end of the line. Your attempt: sed 's/\(\& ...



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