• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Get filename from output and assign to variable

I need to be able to take the following output (which changes dynamically, and issues several thousands of records), and get the filename, and assign it to a variable $fn:

TYPE:
filename2.tif:
TIFF Directory at offset 0x4aac
  Bits/Sample: 1
  FillOrder: lsb-to-msb
  Document Name: "Standard Input"
  Image Description: "converted PBM file"
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 2376
  Planar Configuration: single image plane

I need to get "filename2.tif:" and assign it to a variable $fn.  The filename will always be different, so that's why I need it in a variable, and I'm sure a regex will need to be written to satisfy this.  The script is in a while loop, however, it is outputting multiple times.  Also, I want the above output to remain the same, I just want to take the filename and ALSO store in a variable (so no output will change).

Thanks in advanced.
if (/^\s*filename3.tif:$/) {
                $fn = $1;
                next;
}

## What I have so far, but doesn't work, obviously, since I need it to match all forms of filename.  Basically, it will be any form of letters and numbers followed by .tif: (yes, that's a colon at the end)

Open in new window

0
drewrockshard
Asked:
drewrockshard
1 Solution
 
deiaccordCommented:
You should be able to change the regex to the below and get it work


/^\s*(.+)\.tif:\s+$/i

if (/^\s*(.+)\.tif:\s*$/i) {
                $fn = "$1.tif";
                next;
}

Open in new window

0
 
drewrockshardAuthor Commented:
This worked beautifully. :) Thanks.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now