I've got a script that searches through a logfile for a specific trigger, and then pulls out the line when it's found. For example, this is a line I'm looking for:
7/2/10 9:24:12 AM puppetmasterd[63092] Could not resolve 10.13.1.190: no name for 10.13.1.190
It saves this line into a variable "line", but I'd like to be able to extract only the IP Address.
Our IP Addresses all start with 10.13 - is there an easy way to search for that in this variable and then isolate ONLY the IP address into a variable?
matches = line.scan(/10\.13\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]/);
You want to read about regular expressions on Ruby. Your specific problem is fairly easy, so it would be a good learning exercise. (Try the tutorial if the first link is too advanced!)
Related
I'm searching a file for a repeated string using grep on windows. I've got it working more or less.
I'm updating a large batch file with some IP addresses. When I add a new location into the file sometimes the T1 IP address isn't available so that location gets a T1 IP of null. So periodically I go back through the list to update them.
I search for lines with SET T1=null. That returns those specific lines so I added the -BX to go back X lines where the site number is. This gives me a block of text which includes both the site number and IP of null. Hooray progress.
However I would like to filter it down further and preferably just get the site numbers.
So instead of getting this:
:S001
Text I want to skip here
More text here
SET T1=null
--
:S010
I could get this:
:S001
--
:S010
To summarize, I would like to find a match, then go back up X lines, output that line to a file and nothing else, move onto the next match, repeat.
I suspect I could achieve this with gawk, but alas my awk fu has long since left me so I'm at a loss.
Your extension requirements are heading in the direction of sed or awk (or Perl, Python, …, though awk is fine here); grep is no longer appropriate.
awk '/^:S.*/ { site = $1; next } /SET T1=null/ { print site }'
Capture the lines starting :S (make that more rigorous as you need — for example, maybe /^:S[0-9]{3}$/ looks for a line containing :S and 3 digits only), keeping the name in the variable site. When you encounter a SET T1=null line, print the current value of site — the most recently encountered site name. You can add bells and whistles as needed to make it more robust if your input data is flakier than this expects. You could omit the next in this example; it is a minor optimization.
#Jonathan Leffler
OK thank you Jonathan for getting me on the right path.
I was able to get exactly what I was looking for with a wee bit of tweaking. Mostly due to me being on Windows using the GNUwin32 packages as opposed to on linux/unix.
C:\Program Files\GnuWin32\bin>gawk "/^:S.*/ { site = $1; next } /SET T1=null/ { print site }" c:\file.txt >c:\match.txt
Would anyone be able to give me an example of how this Apache directive, VirtualDocumentRoot "/sites/%-2.0.%-1/www" would resolve a request. I have read the docs and searched for examples but it still does not make any sense. Any help would be greatly appreciated thank you.
It seems like this is a roundabout way of saying take the last two segments of the hostname.
The interpreted part is %-2.0.%-1
%: begins the interpolation, that's easy.
-: means go right-to-left counting which segment of the Hostname the next digit refers to
2: 2nd component of the Hostname
The ".0" is kind of a hack to allow you to include a literal "." immediately after a %X
.: literal "."
%-1: The last component of the Hostname, e.g. com/net/org/biz/uk
So this entire expression just grabs the "last two" components of the hostname separated by a dot.
e.g. foo.example.com, www.example.com and example.com go to the same place on-disk.
I'm new to regex's and Sublime's and am having issues trying to do a find/replace on all email addresses in a csv file.
I thought it would be reasonably straightforward but seem to be heading down the rabbit hole at a great rate of knots.
Data looks like;
data,data,email#address.com,data,data etc NB: there are about 100 fields per record and about 300 records
My thought was to look for the # symbol, then go left and right until I get to the comma and then replace with my new email address but I just can't get a win.
Any thoughts or am I using the wrong tool for the job?
(Also tagging with Ruby as if I need to do some scripting then I'll try to get figure it out in Ruby)
Thanks,
Liam
user2141046's expression won't find an email address like- "a.b#c.com"
I would suggest using:
[a-zA-Z0-9.!#$%&'+-/=?\^_`{|}~-]+#[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)
Source
I'm not familiar with the ruby language, but a regex that finds what you want is:
\w+\#\w+\.\w+
with the \. maybe unneeded (depending on language).
a perl one-liner that does the exact thing:
perl -pi -e 's/\w+\#\w+\.\w+/<your new email here>/g' <csv file here>
note
make sure you use \# in the enw email in the one liner i wrote, meaning new_email\#server.com
Try this:
[a-zA-Z0-9.!#$%&'*+-/=?\^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*
It worked perfectly on a very long csv file filled with emails and all other kinds of stuff.
[a-zA-Z0-9.!#$%&'+-/=?\^_`{|}~-]+#[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)
will not work fine, because some domains have 2 or more levels (like com.br)
Use:
[a-zA-Z0-9.!#$%&'+-/=?\^_`{|}~-]+#[a-zA-Z0-9-]+(?:.[\.a-zA-Z0-9-]+)
How to create VB script Irregular expression syntax to check the VPparam (IP address validity) When the last octatat of the IP address is a range between ip's (x-y) and between each IP we can put the "," separator in order to add another IP
example of VBparam
VBparam=172.17.202.1-20
VBparam=172.17.202.1-10,192.9.200.1-100
VBparam=172.17.202.1-10,192.9.200.1-100,180.1.1.1-20
THX yael
I believe the term you're looking for is "regular expression", not "irregular" - might help when google searching. I don't know enough VB to provide a complete script, but the pattern you're looking for is:
(\d{1,3}\.){3}\d{1,3}(\-\d{1,3})?(,(\d{1,3}\.){3}\d{1,3}(\-\d{1,3})?)*
This will not validate that X < Y, or that each octet is in a proper range, i.e. 999.999.999.999 would be valid. You can't validate X < Y in regex (abbrev. for regular expression), so you'll need to use pattern captures to validate those yourself in the script. If you wish to validate that octets are in the proper range, replace \d{1,3} with ((1\d{2})|(2[0-4]\d)|(25[0-5])|\d{1,2}) each time it appears in the above script.
Is there an api in windows that retrieves the server name from a UNC path ? (\\server\share)
Or do i need to make my own ?
I found PathStripToRoot but it doesn't do the trick.
I don't know of a Win32 API for parsing a UNC path; however you should check for:
\\computername\share
\\?\UNC\computername\share (people use this to access long paths > 260 chars)
You can optionally also handle this case: smb://computername/share and this case hostname:/directorypath/resource
Read here for more information
This is untested, but maybe a combination of PathIsUNC() and PathFindNextComponent() would do the trick.
I don't know if there is a specific API for this, I would just implement the simple string handling on my own (skip past "\\" or return null, look for next \ or end of string and return that substring) possibly calling PathIsUNC() first
If you'll be receiving the data as plain text you should be able to parse it with a simple regex, not sure what language you use but I tend to use perk for quick searches like this. Supposing you have a large document containing multiple lines containing one path per line you can search on \\'s I.e
m/\\\\([0-9][0-9][0-9]\.(repeat 3 times, of course not recalling ip address requirements you might need to modify the first one for sure) then\\)? To make it optional and include the trailing slash, and finally (.*)\\/ig it's rough but should do the trick, and the path name should be in $2 for use!
I hope that was clear enough!