New answers tagged

2

The word boundary has a similar effect to -w, but can be used as part of the expression. ‘\b’ Match the empty string at the edge of a word. [...] ‘\<’ Match the empty string at the beginning of word. ‘\>’ Match the empty string at the end of word. To match bar only when it's the whole word, but foo anywhere (including inside ...


5

dig can read in a file containing a list of hostnames and process them one by one. You can also tell dig to suppress all output except the answer section. This should give you the output you want: dig -f hostlist.txt +noall +answer +search | awk '{sub(/\.$/,"",$1); print $1","$5}' awk's sub() function is used to strip the literal period . from the ...


4

Change your invocation of gawk to the following: | gawk '{print substr($1,1,length($1)-1)","$NF}' >fqdn-ip.csv


16

The sed command, the awk command, and the removal of the trailing period can all be combined into a single awk command: while read -r host; do dig +search "$host" ALL; done <hostlist.txt | awk 'f{sub(/.$/,"",$1); print $1", "$NF; f=0} /ANSWER SECTION/{f=1}' Or, as spread out over multiple lines: while read -r host do dig +search "$host" ALL done ...


0

Given your sample input, this produced your desired output: grep -Eo '["'\'']?[/[:alnum:]]+\.epl' It makes some assumptions about the characters in your filenames (letters, numbers, slashes) -- feel free to throw more characters into that set of brackets.


0

I think this will do it for you: grep -oP "(?<=['\"]|qq{)[xp/c][^\`]+" ./index.epl This says: -o Only keep the matching part. -P Use Perl style regex (we need this for the lookbehind) "..." Use double quotes since you can't escape the single within single (?<=...) Start a positive lookbehind group. This means that whatever we match much also have ...


0

This would better done using the luhn algorithm a discussion of how to implement it in bash can be found here http://codereview.stackexchange.com/questions/95211/validating-a-credit-card-number-using-luhns-algorithm


1

There are variances and limitations in how regexs get evaluated based on the utility being provided the expression, the arguments used when calling the command, the shell the command is called in, among other issues. With that said, the last section of the regex is what is blocking the match when I use it with grep; it is not listed as optional so it is ...


0

In the first case you have to quote the vertical bar (|), since inside lftp it is a special symbol too (also use double quotes to interpolate src and dest variables, quote backslash and dollar sign to prevent their interpretation by the shell): /usr/local/bin/lftp -u user,pass -e "mirror -x '^(\\.mp4|\\.swf)\$' $src $dest" ftp.host In the second case you ...


4

The fact that you can do something in bash doesn't mean that you should. sh (and bash etc) scripts are best suited to be relatively simple wrappers to launch programs or around text-processing commands. For more complicated tasks, including parsing ini files and acting on them, other languages are more appropriate. Have you considered writing your script ...


0

I know it's an incomplete answer but the MySQL.lns in augeas seems to be able to parse most of that. In augtool: augtool> set /augeas/load/testini/incl "/root/test.ini" augtool> set /augeas/load/testini/lens "MySQL.lns" augtool> load augtool> ls /files/root/ .ssh/ test.ini/ augtool> ls /files/root/test.ini target/ = Section1 augtool> ...


1

Using bash's built-in substring expansion: for f in 2015* ; do mv "$f" "${f::4}-${f:4:2}-${f:6}" done


1

While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility: mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9' The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the ...


0

I'm not sure why Perl isn't acceptable here. On the inputs you provided, this line gives the output you asked for: perl -0777p -e 's/.* > (.*) joined the channel\.\n(((?!.* \1 (was kicked from channel\.|was banned from channel\.)\n).*\n)+?.*\1 disconnected)/\2/mg' irc.txt The -e argument is exactly the first argument to your magicregextool except that ...


1

Using the perl rename command (which is completely different to the rename command from util-linux): rename -v 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' 2015* (use -n rather than -v for a dry-run to test the command first). This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux ...


2

With sed: LC_ALL=C sed -e 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1_\2_\3/' <file


0

quick and dirty not full solution #!/usr/bin/env bash str=$1 yyyy=$(echo "$str" | awk -F '_' '{print $1}' | awk '{print substr($0, 1, 4)}') mm=$(echo "$str" | awk -F '_' '{print $1}' | awk '{print substr($0, 5, 2)}') dd=$(echo "$str" | awk -F '_' '{print $1}' | awk '{print substr($0, 7, 2)}') new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' '{print $2}'` echo ...


0

To rename all such files (with 8-digit dates at the beginning) in the current directory: #!/usr/bin/perl my @files = glob "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_*"; foreach my $file ( @files ) { my ( $y, $m, $d, $rest ) = $file =~ m/^(\d\d\d\d)(\d\d)(\d\d)(_.*)$/ or next; rename $file, "$y-$m-$d$rest"; }


0

On my distribution I have the perl-rename command, which can use a perl-style regex to bulk-rename files. The rename command only accepts a pair of fixed strings for the rename.


1

Replacing with Grep You can do (most of) this with regular expression search/replace. Use the Replace dialog, making sure that "Use Regular Expressions" is selected and "Use multi-line matching" is not. Search for: \\stylea{(.*)} And replace with: \1 This is a regular expression "back reference" to the "captured" text in the search expression (the ...


2

Plain grep uses basic regular expressions. Your regex uses -d and ? and {m,n} that aren't recognized. You want grep -E, but that may not recognize \d. If not change it to [0-9] grep -Erle '[3-6][0-9]{3}([ -]?)[0-9]{4}([ -]?)[0-9]{4}([ -]?)[0-9]{3,4}' * Or use grep -P to use perl-compatible regexes Ref: ...


2

The first regex searches for any line containing the following: '^ - start of line, followed by [^,]* - 0 or more non-comma characters, followed by , - a comma, followed by [^0] - any single character other than a zero, followed by [^,]* - 0 or more non-comma characters, followed by ,' - a comma grep -c counts the number ...


0

You can do it with find: find . -regextype posix-extended \ -type f ! -regex '.*/[0-9A-Z]{1,2}_[[:digit:]]{4}_[[:alnum:]_]+?\.dat' -delete Of course you can put it all on one line (removing the \ at the end of the first line). -regextype posix-egrep seems to work exactly as well as -regextype posix-extended. If your version of find doesn't ...


0

Here's another way with sed: sed -n '/<Car>/{x;/.\{2\}/{x;$!{n;p};q};s/.*/&./;x}' infile This is using the hold space to count. Each time it encounters a line matching <Car> it exchanges buffers and checks if there are exactly N-1 occurrences of a character in the hold buffer. If the check is successful it exchanges again, and if not on ...


5

Using extended globs: shopt -s extglob printf '%s\n' !([[:digit:][:upper:]]?([[:digit:][:upper:]])_[[:digit:]][[:digit:]][[:digit:]][[:digit:]]_+([[:alnum:]]).dat) this will print all file/directory names that do not (!) match [[:digit:][:upper:]] followed by zero or one [[:digit:][:upper:]] followed by 4 [[:digit:]] in between _s and then one or more ...


3

There are many ways of doing this. You could use a scripting language that understands regular expressions. For example, in Perl: perl -le 'unlink(grep(!/[0-9A-Z]{1,2}_\d{4}_\w+?.dat/,@ARGV))' * That will look for all files (not subdirectories) in the current directory, collect those that don't match the regex and delete them. You could also do a ...


1

You don't need to get the database name, there's no point in replacing something with itself. Just leave it unchanged: beg="mysqli_connect(" new="'localhost','root','pass'," sed "s/$beg\([^,]\+,\)\{3\}/$beg$new/" file If I save the example you gave as file, that returns: $ sed "s/$beg\([^,]\+,\)\{3\}/$beg$new/" file sadnkjnadsjknfaskdjfnlasdnfkdsa ...


0

A few more choices: Perl $ bt-device --list | perl -lne 'print $1 if /Mouse.*\((.*)\)/' file 34:88:5D:3F:1B:88 In many tools that deal with regular expressions, parentheses (()) are used to capture the matched pattern in a way that can later be used. In Perl, the 1st such captured pattern is $1, the second $2 etc. Here, we are looking for a line that ...


7

Use awk to match the line and split it into words separated by ( or ). Take the 2nd word $2, or preferably the next-to-last $(NF-1) if you might have parentheses in the device name: awk -F '[()]' '/Mouse/{print $(NF-1)}'


2

Using grep with PCRE (-P): bt-device --list | grep -Po 'Mouse\s.*?\(\K[^)]+' Mouse\s.*?\( will match Mouse in the line and then upto first (, \K will discard the match [^)]+ will get us the desired portion i.e. characters upto the next ) Example: $ cat file.txt Added devices: Logitech K811 (00:1F:20:EB:06:E0) Plattan ADV Wireless (5C:EB:68:1F:D1:62) ...


0

Use grep + awk : grep "^ID" your_file | awk {'print $2'} Bonus : easy to read :)


1

The man page description for that option is sort of misleading... It's pattern as in globs not pattern as in regex. Per the info page: --exclude-dir=GLOB Skip any command-line directory with a name suffix that matches the pattern GLOB. When searching recursively, skip any subdirectory whose base name matches GLOB. Ignore any redundant trailing ...


2

--exclude-dir does not understand |. You can, however, get the same effect by specifying --exclude-dir multiple times, one for each directory that you want to exclude: grep -inRw -E --exclude-dir 'asset' --exclude-dir 'git' --exclude-dir 'log' 'direct'


-1

Regarding point 2: gnu sed uses GNU BRE by default. To use POSIX BRE you should specify --posix


6

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


1

Implementations of find vary, but they should all handle character classes in wildcards (POSIX.2, section 3.13): find . -name '*[~*]*' If newline is among your "special" characters, you may need to work out how to get your shell to pass it to find. In Bash, you can use find . -name $'*[\t \n]*' to show files containing whitespace, for example. A ...


2

If you want something more general than matching a specific character, you would have to use regular expressions. Since the question is not tagged "linux", the proper answer would use POSIX: find . | grep '[*~]' If you want to make it Linux-specific, you can use the GNU find option -regex (also supported by FreeBSD). If the pathname has an embedded ...


0

find dir -name just supports shell file name glob characters as documented by man fnmatch. Some find implementations support non-standard extensions for regular expressions. Check your find man page.


1

-name takes wildcard patterns, not regexps and matches on the file name, not its full path. Use -regex (or -iregex) for regexp matching but beware it matches against the full path. Here, you could do: LC_ALL=C find -E . -iregex '.*s[0-9]{1,2}\.?e[0-9]{1,2}[^/]*\.mkv' Here, we're replacing the second .* with [^/]*, that is a sequence of non-/ characters to ...


1

Unfolding that one-liner and rearranging a bit, plus a few tweaks, gets: cat /dev/urandom | \ tr -dc 'a-zA-Z0-9' | \ fold -w 16 | \ tr -d '[A-z]' | \ grep '....' | \ head -n 16 Outputs: 7405935 60722 11225 96954 3966 8774 539418 1964 59150 5994 1086 7470 2751 8534 21501 14927 Note: the n-digit numbers are probably random if taken ...


0

To get the familiar regexps working you need to enable "extended regular expressions" with the flag '-E'. With that, your regexp should work: ... | grep -E -o '[0-9]{4,16}' The -P flag (Perl-compatible regular expressions), which some distributions support, is not necessary in this case.


5

ShellCheck is a good start for bash programming. It gives quite useful hints: Line 6: if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]] ^-- SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex. Regex can't be quoted like this. Working example with escaped special characters (basically ...


2

As is often the case on Solaris, /usr/bin/egrep is a legacy implementation that isn't POSIX-compliant, while /usr/xpg4/bin/egrep is a POSIX-compliant implementation and has little if anything beyond POSIX. Unless you're running legacy Solaris applications from the pre-POSIX days, make sure that /usr/xpg4/bin is before /usr/bin in your $PATH. GNU tools ...


29

There are two problems with your example. The primary one is that you're assuming that regular expressions work the same as glob patterns in that * is a wildcard meaning "any sequence of characters." In regular expressions, * means "any number of the previous atom" instead, so fil* means f followed by i followed by zero or more l characters. You need to say ...



Top 50 recent answers are included