Bash grep complicated search [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I write a searching command using grep that will look for a line with a strict requirements. For example it should start with a name, which consist only letters and "-", then follows an ":", then a year or "xxxx", then again an ":", and then a line of letters, digits and "-" of some length. Or may be there is a link where I can read this... I'm trying to find some solution in the Internet for a long time, but can't...

What you need here is to pass the grep command a regular expression that describes your pattern of interest, on the basis of which grep will match only valid lines.
Taking into account your indications, the following regular expression could do the job:
^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$
The expression begins and ends with the ^ and $ anchors, that indicate the beginning and the end of a line. Then, you basically have three token blocks, separated by :, the first matching letters and dashes, the second years or xxxx, and the third letters, digits and dashes. + is a quantifier, indicating that the preceding token can appear one or more times.
You can use it with grep like so:
grep -P "^([A-z]|-)+:([0-9]|xxxx)+:([A-z]|[0-9]|-)+$"
The -P option is to indicate to interpret it as a Perl regex and correctly handle hyphens matching.

Related

Regex How to match ONLY File name prefix and extension ignoring the numbers in the middle in Ruby? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've been trying to match only specific prefix and it's extension in a filename but with no luck.
Example:
BX-ST123456.mxf ==> I need to match only BX-ST and .mxf
BX-SR123456.mxf ==> this shouldn't be a match
BXL-ST123456.mxf ==> this shouldn't be a match
so basically the match should happen only if it finds the string of BX-ST and .mxf without restricting the length of numbers in the middle (ignore them) they could be 0-9 in any random.
Example:
BX-ST0.mxf ==> should be a match
BX-ST01.mxf ==> should also be a match and so on.
appreciate your help everyone
Try
BX-ST\d+.mxf
You could also add ^ in the beginning and $ at the end like this
^BX-ST\d+.mxf$
A good site to try your Regex is Rubular
There is a nice class called File that can help you. Such that:
File.fnmatch?('**BX-ST*.mxf','path/to/file/BX-ST123456.mxf')
#=> true
Here the pattern is a glob pattern not a regex and simply says match directory recursively '**', filename starts with "BX-ST" and ends with ".mxf".
Additionally you could go with
File.extname(YOUR_FILE) == '.mxf' && File.basename(YOUR_FILE)[0,5] == 'BX-ST'
Here we check that the extension is ".mxf" and that the basename's first 5 characters are "BX-ST"

How to match strings, ignoring certain characters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have to match pairs of strings, ignoring spaces " " and hyphens "-". I want to regard the following pairs as identical.
"2,3 chloro benzene" and "2,3 chlorobenzene"
"4'3',2-dinitrotoluene" and "4'3',2-di nitro toluene"
Due to the spaces, I cannot match them. How can I do that? I am not sure how to do it in Ruby.
Use String#delete to delete unwanted chars and normalize the two strings before comparing them, as shown below:
s1 = "2,3 chloro-benzene"
s2 = "2,3 chlorobenzene"
s1.delete(" -") == s2.delete(" -")
#=> true

Regex that matches a pattern but does not match ":" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a regex that looks for certain types of hostnames like: .*-.*(nmtg)*|.*(\.nms). How do I modify this so it does not match: 11.22:33:44:55-66?
Should match:
cs25-admin.nmtg.company.com
cs25-admin
but should not match:
11.22:33:44:55-66
Two basic ways:
You can replace your "match anything" . with "match anything except for colon" [^:] everywhere
You can prepend your expression with "no colons from here to the end of string" (?!.*:)
EDIT As Signus said, your regexp is really non-specific and open-ended; it will match much more than what you think. For example, "----THRICEnmtgnmtgnmtg" is a full match, and so is "(-_-)". It is a better policy and easier to carefully specify what you want, rather than go listing exceptions. The regexps suggested by Signus are a good example.
They will still match within strings: "dont match this: example.com" will still match the "example.com" part. If that is what you want, cool. If not, you want to anchor the start and end of the string, by surrounding your regexp with /^.....$/.
You're using the * quantifier that matches 0 or more of the preceding token, in which case you supplied . which is a token that matches any character except line breaks.
To match domain names with subdomain names you can do the following:
(\w+\.)?\w+\.(com|org)
And to really match any domain with a TLD I like to do this:
([a-zA-Z0-9]+\.){1,2}[a-zA-Z]{2,4}
Where the latter will match any domain with a single subdomain using the numeric quantifer {num} which allows you to specify a range of matches, as shown in the above regex.
This allows you to match a group of alphanumeric characters followed by a period 1 to 2 times (i.e. subdomain.domain.topleveldomain, where subdomain. is the first match and domain. is the second match of the first group).

Adding \n at every # characters in string Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to start a new line after every 66 characters for any file that is input into a Ruby script.
some_string.insert( 66, "\n" )
puts some_string
shows that a new line starts after the 66th character but I need it to happen after each 66th character. In other words, each line should be 66 characters long (except possibly the last).
I'm sure it involves a regex but I've tried various with insert, scan, gsub and cannot get it to work.
I'm new to Ruby and programming and this is the first thing I've tried outside of a tutorial. Thanks for the information, all.
You could do something like this:
<your_string>.scan(/.{1,66}/).join("\n")
It will basically split <your_string> at every 66th character and then re-join it by adding the \n between each part.
Or this variation to not split words in half:
<your_string>.scan(/.{1,66} /).join("\n")
some_string.gsub(/.{66}/, "\n")
If you're interested in exploring an answer that doesn't use RegEx, try something like:
a = "Your string goes here"
d = 66
Array(0..a.length/d).collect {|j| a[j*d..(j+1)*d-1]}.join("\n")
The RegEx is likely faster, but this uses the Array Constructor, .collect and .join so it might be an interesting learning exercise. The first part generates an array of numbers based on the number of chunks (a.length/d). The collect gathers the substrings in to an array. The body of the collect generates substrings by ranges on the original string, and the join puts it back together with '\n' separators.
Use the following to split the string into an array of strings of length 66 and join those strings with a newline character.
some_string.scan(/.{1,66}/).join("\n")

Regex issue with a string and all numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Ruby, I would like to create a regular expression that matches the following:
building/liberty-green/6d
(the word building and some number somewhere after it)
Currently, I have /building/ and need to add \d (any digit) to it, but I don't know how.
You need /building\/[\w-]+\/\w+/. For example:
irb(main):001:0> /building\/[\w-]+\/\w+/.match("building/liberty-green/6d")
=> #<MatchData "building/liberty-green/6d">
That expression will match any string that:
Starts with /building/
Then follows with one or more word characters or dashes (eg. foo-bar, foo, bar-1)
Then follows with a /
Finally ends with one or more word characters (eg. foo, 6d, 12345)
Note that \w includes digits.

Resources