neoasr neoasr - 1 year ago 106
PowerShell Question

need to extract set of 4 digit numbers from epub or text file in Batch

I have hundreds of epub files. I need to extract dates from text (only years like 1947, 1987 etc) with file names
i mean, output should be like,this filename contains this this dates and so on
for example epub01 contains 1995 1945 1986.
epub02 contains 1926 1946 1948.
if anyone can provide me a PowerShell script or script that can run in ubuntu terminal, that would be great.

Although I have epub files but I can extract to text file myself. if you have script for text files.

Answer Source

I can only provide a script for text files. You can read them using the Get-Content cmdlet and use a regex to grab your values containg a negative lookahead and negative lookbehind to ensure there are exactly four digits:

$content = Get-Content 'your_file' -Raw
$matches = [regex]::Matches($content, '(?<!\d)(\d{4})(?!\d)')
$matches | ForEach-Object {

Regex explanation:


Regular expression visualization

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download