Grab first segment of URL in Perl [duplicate] - perl

This question already has answers here:
How do I extract the domain out of an URL?
(2 answers)
Closed 9 years ago.
I have a URL saved to a variable in Perl and I'd like to strip off everything after the domain name. I think grabbing everything to the left of the third slash should do it. I do want to keep the protocol.

($url) = $url =~ m! (.+?\w) (?: /|\z) !x;

Related

Count number of occurrence of pipe ("|") in a file with PowerShell [duplicate]

This question already has answers here:
Counting characters in a specific text file
(4 answers)
Closed 4 years ago.
I have data in a file like below.
text1|text2
text3|text4|
I'm looking for a Power Shell command to count number of pipes present in thisfile.
Normally I would want you to show some effort but this is trivial enough I won't bother:
$pipeCount = (get-content file.txt -Raw).Split('|').count - 1
$pipeCount

How can I trim the process path from output? [duplicate]

This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name

I want to split a csv file with , and " with the latter hashigher precendence [duplicate]

This question already has answers here:
How do I split a string into an array by comma but ignore commas inside double quotes?
(7 answers)
Closed 8 years ago.
I want to split a csv file. ex a row contain name,id,address,pin etc..... if the adress contains again commas then how to parse? ex: Abc,123,"xyz,asd,pqr",123456 "xyz,asd,pqr" is one element and kept in double quotes only
Use a CSV parser. If you don't want to install new modules for some reason then look at Text::ParseWords instead.

Need help understanding perl code [duplicate]

This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.

using wildcard in Perl in dir name matching [duplicate]

This question already has answers here:
using wildcard in the dir locations and find all files with an extension
(2 answers)
Closed 9 years ago.
my #hex_locations = ("$FindBin::Bin/../../../project/platform-aa-full/bb",
"$FindBin::Bin/../../../project/platform-aa-base/bb");
how do I use wild card to match any directory with project/platform-aa-* in above code?
Use glob
my #hex_locations = glob("$FindBin::Bin/../../../project/platform-aa-*/bb");