w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Failing to find files with regex in my directory with .tar.gz extension
Just remove the quotation marks: my $date_folder =~ /$mday-$mon-$year/; they are just literal characters in a regex. Oh, oh, oh, sorry, I overlooked that the code makes no sense at all! So, some basics: if you have something like $date_folder =~ /$mday-$mon-$year/;, your regex is matched against the content of $date_folder. If this is not set you get your "Use of uninitialized value $date_folder in pattern match (m//)". $date_folder =~ /$mday-$mon-$year/; returns true or false. For more details see: perlrequick perlretut In your case you need to loop over your files in the folder and inside the loop you need to check the current filename with your regex and if it is matching then do whatever you want with the file. For more details see: How can I loop through files in a direct

Categories : Regex

Grep/Awk part of directory name to variable iteratively
awk does not support PCRE so that positive lookahead is not going to work. Also, the bash variables cannot be called in to awk like that. What I am seeing from your requirement can be met with bash built in string substitution. $ var=01_Startup $ echo "${var%_*}" 01 You can use find to gather list of directories and use string substitution. find . -type d | while read -r dir; do echo "$dir ---> ${dir%_*}"; done

Categories : Shell

how to include grep in a regex in perl
The easiest way is to define a quoted regular expression as your constant instead of a list: use strict; use warnings; use autodie; # Will kill program on bad opens, closes, and writes use feature qw(say); # Better than "print" in most situations use constant { LIST => qr/keyword1|keyword2|keyword3/, # Now a regular expression. FILE_READ => 'file.txt', # You're defining constants, make this one too. }; open my $read_fh, "<", FILE_READ; # Use scalars for file handles # This isn't Java. You don't have to define "main" subroutine while ( my $line = <$read_fh> ) { chomp $line; if ( $line =~ LIST ) { #Now I can use the constant as a regex say $line; } } close $read_fh; By the way, if you don't use autodie, the standard way of opening a file

Categories : Regex

Different regex output for grep, ack and rubular
You need to use an extended regular expression for grep to work in this case. Add the -E flag. I get pretty much the same output for all three tools in that case:

Categories : Regex

shell variable in a grep regex
You need to use double quotes. Single quotes prevent the shell variable from being interpolated by the shell. You use single quotes to prevent the shell from doing interpolation which you may have to do if your regular expression used $ as part of the pattern. You can also use a backslash to quote a $ if you're using double quotes. Also, you may need to put your variable in curly braces ${var} in order to help separate it from the rest of the pattern. Example: $ string="test this" $ var="test" $ echo $string | grep "^$var"

Categories : Bash

Strange regex behavior with grep
echo 1 | grep '[:digit:]' #nothing.... grep uses a different syntax, you need [[:digit:]] or [0-9]. The {1,} syntax is not supported by basic grep, you can use other modes, like the extended one with -E... Note: Normally one would use + for matching one or more characters.... General note: always test regexes in small parts to see that each part really does what you thought it does. Once the expression gets complicated, it's really hard to tell what went wrong.

Categories : Regex

Grep regex to capture ip socket without port
Simply use awk and split in characters {, :, } and blanks. Then count fields: awk -F'[{:}[:blank:]]+' '{ print $9, $11 }' infile It yields: 10.20.21.106 10.20.21.27

Categories : Regex

bash and grep: passing of regex parameter
Change regex="^'$1$'" to regex="^$1$" and $($cmd) to $cmd Here is a fixed version: #!/bin/bash DICT=/usr/share/dict/ngerman usage () { progname=$(basename "$0") echo "usage: $progname regex" } if [ $# -le 0 ]; then usage exit 1 fi regex="^$1$" cmd="grep -i $regex $DICT" echo "$regex" echo "$cmd" $cmd | while read -r word; do echo "$word" done But this script has potential problems. For example try running it as ./script 'asdads * '. This will expand to all files in a directory and all of them are going to be passed to grep. Here is a bit improved version of your code with correct quoting and also with bonus input validation: #!/bin/bash DICT=/usr/share/dict/ngerman usage () { progname=$(basename "$0") echo "usage: $progname regex" } if [ $# -le 0 ]

Categories : Regex

How do I stop searching after the word (regex, grep)?
Use the $ for end of line: ls -al | grep " adh$" should make it. Test $ touch faaa $ touch faaa2 $ touch faaa3 $ touch afaaa $ ls -al | grep faaa -rw-rw-r-- 1 me me 0 Jul 25 14:24 afaaa -rw-rw-r-- 1 me me 0 Jul 25 14:24 faaa -rw-rw-r-- 1 me me 0 Jul 25 14:24 faaa2 -rw-rw-r-- 1 me me 0 Jul 25 14:24 faaa3 $ ls -al | grep faaa$ -rw-rw-r-- 1 me me 0 Jul 25 14:24 afaaa -rw-rw-r-- 1 me me 0 Jul 25 14:24 faaa $ ls -al | grep " faaa$" -rw-rw-r-- 1 me me 0 Jul 25 14:24 faaa

Categories : Regex

Using bash grep -Po regex fails if string has an underscore
Your regexp uses , which matches the boundary between word and non-word characters. The problem is that _ is a word character, and so are digits, so there's no boundary between _ and 2. You can use [^0-9][0-9]{4}.{1}[0-9]{2}.{1}[0-9]{2}[^0-9] instead. If the date can be at the beginning or end of the filename, use: ([^0-9]|^)[0-9]{4}.{1}[0-9]{2}.{1}[0-9]{2}([^0-9]|$)

Categories : Regex

grep regex return substring but exclude comments
This grep should work: grep -Po '^[^!].*?(?<=/)K[^/.]*(?=.)' infile.txt OUTPUT: baseline Explanation: ^[^!] will make sure to match anything but ! at line start K will make sure to reset the start

Categories : Regex

grep & regex in terminal - stop at first occurence of character
.* matches greedily. Use .*? (and an extended regular expression) instead: grep -ohE 'translatedString:.*?]' ArticleV.m If you don't like the -E flag, egrep works the same way: egrep -oh 'translatedString:.*?]' ArticleV.m

Categories : Regex

Regex with grep - match unknown number of alphabetical characters?
Have a look at the manual. Without the -E flag, grep assumes "basic" regular expressions. In this case most meta-characters lose their special meaning, unless you escape them. So do grep -nr "STRINGONE_w+_w+_STRINGTWO" or grep -nrE "STRINGONE_w+_w+_STRINGTWO" Or, since you only want uppercase letters: grep -nr "STRINGONE_[A-Z]+_[A-Z]+_STRINGTWO" grep -nrE "STRINGONE_[A-Z]+_[A-Z]+_STRINGTWO" Otherwise, strings with three or more components in the middle would also be accepted.

Categories : Regex

Perl Regex to Grep a word not starting with special character _
You can use a negated character class: [^_]IPSd+$ Note that the second d in your original regex is redundant because d+ will be greedy.

Categories : Regex

Use regex with grep to filter data from the output of a verbose command
Let's go by parts. Best way I can think of is redirecting the output to a file, in unix-like environments you do it like: cat your-command > filename.txt Second, you need to read the file line by line, I would use a python script or a perl script if you know any of those, or whatever language fits you. Third, you can get values two different ways: Read columns by position, you can get colums with a regex like: [^s]+ Write regular expressions for every specific column, so for IP you could have something like this: ([0-9]{1,3}.){4} for monitoring monitoring-([^s]+) and so on.

Categories : Regex

Why does enclosing a single char in brackets in a regex exclude the grep itself when grepping ps?
This is because the expression ba[s]h (or [b]ash, or...) just matches bash, not ba[s]h (or [b]ash, or...). So the grep command is looking for all lines with bash: root 2286 1 0 Jun06 ? 00:03:15 /bin/bash /etc/init.d/zxy100wd wmiller 6436 6429 0 Jun06 pts/0 00:00:01 bash wmiller 10707 6429 0 Jun07 pts/1 00:00:00 bash wmiller 10795 6429 0 Jun07 pts/2 00:00:00 bash but wmiller 16220 6436 0 06:55 pts/0 00:00:00 grep --color=auto ba[s]h does not match because it is not exactly bash.

Categories : Regex

Using grep to find function
Try: grep -E "system("rm [a-zA-Z0-9 ]*")" file.txt Regexp [a-zA-Z0-9 ] builds a pattern for grep what it needs to find in x of system("rm x"). Unfortunately, grep don't supports groups for matching, so you will need to specify it directly what to search.

Categories : Linux

Grep - find and replace
You have a missing single quote in grep command, it should be: grep -irl '#e5f6fc' mydirectory | xargs sed -i.bak 's/#e5f6fc/$highlight-blue/Ig' PS: I made both grep and sed ignore case

Categories : Shell

Unix script to recursively search a directory and sub directories to grep and print content between 2 patterns in file
This might work for you: find . -type f -exec awk 'BEGING {print FILENAME ":"} /Pattern1/ { p=1 ; next } /Pattern2/ {p=0} p==1 {print $0} END {print ""}' {} ; Note, this prints the FILENAME, even if Pattern1 was not found!

Categories : Unix

Using grep with perl to find files
How about ls *keyword* If you trying to do this within perl @files = glob("*keyword*"); for $file (@files) { print "$file "; } Note that grep in perl is a core function, but it has nothing to do with regular expressions. It is a more like SQL where; it filters an array to a subarray by applying a function (which may or may not be a regex) to each element. If glob expressions are not good enough, you can do @files = grep /(fun[kK]y_)keyword?/ glob("*");

Categories : Regex

How can I use jquery .grep() to find an object then update it?
grep return an array var country = $.grep(contentArray, function (e) { return e.key == countrykey; }); if(country && country.length == 1) country[0].value = 'Malta';

Categories : Jquery

EMACS find-grep command switching windows
There might be some options of grep behavior, that you might find if you dig in the lisp/progmodes/grep.el from the source, but I really think it might be better and easier to have a look of GrepPlus library which bring many enhancement of emacs grep. Otherwise you could also use occur and see how you could customize it. In occur, when you are in the match buffer, you can it C-o instead of Ret, which will show in the other buffer the match you selected, keeping your cursor in the match buffer. Difference with grep, is that it only works with opened buffer. I'm rather sure grep+ might have the equivalent. You should have a look

Categories : Function

Find file within directory and delete the directory its in
%~dp1 can be used to convert a file param to a drive and path. Quote the output as it doesn't convert to 'safe' path names. SS64 is a great resource for learning command scripting.

Categories : Batch File

grep find when string has changed / create time bars
grep isn't going to be enough for you. Here's the start of a Perl program that should help. use warnings; use strict; my $current_datetime = ''; while (my $line = <>) { chomp $line; my $datetime = substr( $line, 0, 13 ); if ( $datetime ne $current_datetime ) { print $line, " "; $current_datetime = $datetime; } } Save that as whatever.pl, and execute it as perl whatever.pl yourinputfile And you should get the output 20110103T000204000 20110103T000330000 20110103T000523000 Does that give you a good place to start?

Categories : Bash

How to find, filter and copy directory and contents to a location dictated by part of find result ) in Powershell
This should work: $specialdirectory = "xyz" $Destination = "zyx" $1 = "\fileserverprofiles" | Get-ChildItem $User = $1.Name | where {$_ -Match "User"} $User | foreach { $check = test-path -pathtype Any "\fileserverprofiles$_appdata$Specialdirectory" If ($check -eq $true) {Copy-Item \fileserverprofiles$_appdata$specialdirectory -Destination C:$Destination -Recurse} }

Categories : File

Is it better to use git grep than plain grep if we want to search in versioned source code?
git grep only searches in the tracked files in the repo. With grep you have to pass the list of files to search through and you would have filter out any untracked files yourself. So if you are searching for something that you know is in the repo, git grep saves you time as all you have to do is provide the pattern. It also is useful for not having to search through anything that is untracked in the repo.

Categories : Linux

How to ssh to a certain directory and then be able to use the FIND command to find certain files and gunzip them
NB: don't confuse "zip" and "gzip", two different animals This should work: cd <local_directory> # collect files names as $1 $2 ... $N set -- *.gz # or use your own filter like "dumps*.gz" # put source file a tar archive and send it as input to ssh # then, on the other side, untar the file then decompress tar cf - $* | ssh <user>@<remote_host> "cd <remote dir> && tar xf - && gunzip $* Note: using "&&" instead of ";" to prevent "tar" command to be executed if "cd" fails for any reason

Categories : Unix

How to find with lookahead in Visual studio 2012 Regex Find and Replace
You need to add a (?s), which enables multiline matching, and also escape the period in using Example.Foo. The regex should be something along the lines of: (?s)using Example.Foo;(?=.*BaseClass<SomeClass>)

Categories : Regex

grep command on linux ( especially grep --exclude)
you can find grep on man pages here. what exclude does: --exclude=PATTERN: Recurse in directories skip file matching PATTERN. your command will search, recursively, in all directories, skipping file pattern "*.svn*" and searching for file pattern "1.0.0.8/*1.0.0.8.config > 1.0.0.8-REVISION.txt"

Categories : Linux

Regex - Find all groups where you can find a given number
You can try this regex [^:]*(?<=[-:])2(?=[-:])[^:]* [^:] means match any character except : [^:]* would match 0 to many characters except : 2(?=[-:]) would match 2 only if it is followed by - or : (?<=[-:])2 would match 2 only it is preceded by - or : OR [^:]*2[^:]*

Categories : Regex

Ant read files in a directory and regex
Try using an embedded scripting language, like groovy, to do this kind of complex logic. <target name="process-files"> <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/> <groovy> def list = [] new File('build').eachFile() { def matcher = it.name =~ /(build_leveld_Dd).properties/ list.add matcher[0][1] } properties."build.levels" = list.join(",") </groovy> </target> <target name="doSomething" depends="process-files"> <echo>${build.levels}</echo> </target> Just like ant-contrib, groovy needs an additional jar. I normally include a "bootstrap" target to install this: <target name="bootstrap"> <mkdir dir

Categories : Ant

Regex to match S3 file and a directory
There's only the final slash missing in the regex. You might try this: ^s3://([^/]+)/(.*?([^/]+)/?)$ ^^ regex101 demo.

Categories : Java

Inconsistency between perl grep and cli grep
I suposse your perl script is named "myscriptname". When you run this script, you have a new process (perl myscriptname.pl), and it's showed by the ps -ef command. The other one is related to the grep command (it has the text you are looking for)

Categories : Linux

regex: How to match a directory (but not a 'normal' file)?
Asssuming you are refering to the Windows OS: Using regex to determine that would not be the right way. Because a file can have no extension and a folder can have a period and an extension as part of its name. The best approach is to use FileAttributes in the code like this (.NET C#): // get the file attributes for file or directory FileAttributes attributeUnknown = File.GetAttributes(@"C:MariusHofert"); //detect whether its a directory or file if((attributeUnknown & FileAttributes.Directory) == FileAttributes.Directory) MessageBox.Show("Its a directory"); else MessageBox.Show("Its a file"); Reference Link

Categories : Regex

Regex to match directory path and ignore filepaths
First of all, directories can be named with extensions, so checking for the presence or absence of an extension in a path is not a reliable way to do this to determine if something really is a file or directory. In fact, the only way you can determine if a path is a directory or file name would be to use the appropriate OS API, e.g. GetFileAttributes on Windows or stat on Linux. If this is your requirement, then you should split on the semicolon and iterate over each path that results, feeding each one in turn into the appropriate API to determine if it is a file or directory. If a textual match is all you need, I would still suggest you split on the semicolon and then match each individual path against an appropriate regular expression. A Ruby function that would extract the extension m

Categories : Regex

Regex for replacing dashes with underscores in directory names and not the filenames
No, not really. It's much better to use Path.GetDirectoryName and friends: var s = "/my/test-is/for-this/to/work-right.txt"; var result = Path.Combine( Path.GetDirectoryName(s).Replace("-", "_"), Path.GetFileName(s) ); If your path uses / as the directory separator, you are running on Windows and you don't want it to be replaced by you can simply undo the replacement afterwards: // What I 'm really doing here is showing that these constants exist; // read the manual to see what you can expect from them. result = result.Replace( Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar );

Categories : C#

Regex to check special characters in the filename in an input directory/path
I think something like the following provide a decent match for a Windows filename/directory structure: ^[A-Z]:(\[^\/:*?"<>|]+)+.w+$ (Note that blacklist came from Windows Explorer after trying to enter in a bad character.) Example matches: $ grep -P '^[A-Z]:(\[^\/:*?"<>|]+)+.w+$' file_list.txt C:Sample File5.txt C:Program Files (x86)Windowsfile.exe C:Program FilesJon's folderfile.pdf C:my-file.txt C:5@^&[]{}!#i.txt.exe If you want to change that to a whitelist, modify accordingly: $ grep -P '^[A-Z]:(\[ws@-]+)+.w+$' file_list.txt C:Sample File5.txt C:my-file.txt

Categories : Asp Net

Creating a script to delete old log files, that allows me to pass directory/age/regex arguments
If you insist on using Perl, look into File::Find & friends. Though if you're on a *nix box you should probably be aware of find(1) for tasks this common. try: find /release/logs -name test.log-* -mtime +7 -delete If you want to test it out 1st, leave off the -delete flag & it will just print a list of the files it would have otherwise deleted.

Categories : Perl

Better Way to find all Files in Directory
There's an overload that will search for a particular pattern. Instead of searching for *.* search for the filename you are interested in with the search pattern parameter. Directory.GetFiles(dirPath, "*" + searchName + "*", SearchOption.AllDirectories);

Categories : C#

How can I find my working directory?
In short, you can't rely on the files being on disk at all - they might be just contained in a *.war file, containing the whole application. Or they might be generated on-the-fly, despite the name sounding like an actual file. Also, you should not arbitrarily write within a directory even if you find it (my closest guess would be tomcat's webapps/websitereport/ directory if it exists) because nobody will know that something changed during the time since last deployment. So, on the next update of the application, you'll end up overwriting all of your changes again. You typically change the underlying application and redeploy. You might also find a few references in tomcat's conf/localhost/ directory or even in conf/server.xml, but it all depends on how your server was administered

Categories : HTML



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.