Operating System - Linux
1828793 Members
2627 Online
109985 Solutions
New Discussion

PERL - pattern extraction help

 
Anand_30
Regular Advisor

PERL - pattern extraction help

I have got a file that has patterns similar to the sample provided below

C\WINDOWS\system\hpsysdrv.exe
C\hp\KBD\kbd.exe
C\Program Files\iTunes\iTunesHelper.exe
C\WINDOWS\system32\VTTimer.exe
C\WINDOWS\ALCXMNTR.EXE
C\Program Files\EarthLink TotalAccess\TaskPanl.exe
C\Program Files\iPod\bin\iPodService.exe
C\WINDOWS\system32\ctfmon.exe
C\Program Files\Compaq Connections\1940576\Program\BackWeb-1940576.exe
C\Program Files\NETGEAR\WPN311\wlancfg5.exe
D\OCSetup.exe
C\Program Files\Microsoft Windows OneCare Live\Staging\SEA3\ocsetup.exe

I am trying to write a PERL script that would extract only the .exe content from the file like ocsetup.exe, ctfmon.exe etc.

Can anyone please help.


Thanks,
Anand
3 REPLIES 3
H.Merijn Brand (procura
Honored Contributor

Re: PERL - pattern extraction help

> perl -nle'm{([^/\\]+\.exe)\b}i and print $1' xx.dta
hpsysdrv.exe
kbd.exe
iTunesHelper.exe
VTTimer.exe
ALCXMNTR.EXE
TaskPanl.exe
iPodService.exe
ctfmon.exe
BackWeb-1940576.exe
wlancfg5.exe
OCSetup.exe
ocsetup.exe

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: PERL - pattern extraction help

Merijn knows perl. Heed his advice!

His code matches and remenbers a piece of string on the line startign with a backslash and ending in .exe.
If there is seperate text beyond the .exe, then it is ignored, whcih you probably want.
Is every image ganartueed to be called .exe or could there be a .exe_v3 or such?
Are there arguments beyond the image?

Will every line have an image, and a \ before the image name?

If the last is true, and other simple approach could be:

perl -pe "s/^.*\\//" test.txt

Here we replace be beginning of every line up the the last backslash with nothing (the .* is 'greedy').

Or if there are other lines in the mix:

$ perl -ne "s/^.*\\//; print if /.exe/" test.txt

Enjoy!

Hein.



H.Merijn Brand (procura
Honored Contributor

Re: PERL - pattern extraction help

> His code matches and remenbers a piece of string on the line startign with a backslash and ending in .exe.

No, it does not. It matches any sequence of characters that do NOT match a forward or backward slash, followed by a .exe extension and a (zero-width) word boundary, all case-insensitive.
Essentially, this drops of the path part on both dos and unix.

The weakness in this match is files like D:foo.exe, which will match including the D:, as there is no slash at all and colon's are valid in file names.
I wrote the regex like that to allow whitespace in file names

If there is seperate text beyond the .exe, then it is ignored, whcih you probably want.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn