Delete all text after string...

I have a series of text files that I am attempting to parse with PowerShell. I haven't done much with text file manipulation and would love some advice. Thanks in advance to anyone willing to give this a go!
Example File: (file name = "report_20131231.txt")
**** SAMPLE1 ****
user1 username1
user2 username2
user3 username3
**** SAMPLE2 ****
user4 username4
user5 username5
user6 username6
I want to import the text file and reformat it to the following:
Date | SAMPLE # | Username
<file name date> | <sample #> | <username>
Example Output:
Date | SAMPLE # | Username
20131231 | SAMPLE 1 | username1
20131231 | SAMPLE 1 | username2
20131231 | SAMPLE 1 | username3
20131231 | SAMPLE 2 | username4
20131231 | SAMPLE 2 | username5
20131231 | SAMPLE 2 | username6

This code seems to work for parsing your original file format:
function Import-YourSampleFile
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName', 'PSPath')]
[string[]]
$Path
process
foreach ($string in $Path)
if ($string -match '(\d{4})(\d{2})(\d{2})(?:\.[^\.]*)?$')
$date = Get-Date -Year $matches[1] -Month $matches[2] -Day $matches[3] -Format yyyyMMdd
else
$date = 'Unknown'
$sampleNumber = $null
Get-Content -Path $string |
ForEach-Object {
$line = $_
switch -Regex ($line)
'^\s*\*{4}\s*SAMPLE(\d+)\s*\*{4}\s*$'
$sampleNumber = $matches[1]
break
'^\s*\S+\s+(\S+)\s*$'
if ($null -ne $sampleNumber)
New-Object psobject -Property @{
Date = $date
Sample = "SAMPLE $sampleNumber"
UserName = $matches[1]
break
} # ForEach-Object
} # foreach ($string in $Path)
} # process
} # function Import-YourSampleFile
The rest is just using this function in conjunction with Export-Csv (or something very like it) to produce the pipe-separated format you wanted.  Something like this:
Get-ChildItem -Path .\report_*.txt |
Import-YourSampleFile |
Export-Csv .\output.txt -NoTypeInformation -Delimiter '|'

Similar Messages

  • Siri 8.2 Bug: siri deletes all text after pressing "Done"

    After the upgrade to 8.2, when dictating text through Safari into an open field, once you press "Done" to end Siri dictation, all text entered is deleted.
    We have tested this multiple times with both 8.2 based iPhones (5S, 6 and 6+).   All 8.1 based units work fine.  All 8.2 units fail.
    How does one go about reporting such an issues to Apple?

    HI,
    I am having the same issue in IOS 8.2. The dictation feature is useless because it erases everything i have written, or certain characters. Now i cant use it in MS Word. This is a big problem for ppl that heavily relies on it for writing messages, articles, etc. Please we need a soon fix Apple!!

  • I deleted a contact out of my iPhone 4S but when I go to send a message the contact appears although that individual is no longer in my address book. I deleted all text and call history pertaining to the deleted contact but still shows up.

    I deleted a contact out of my iPhone 4S but when I go to send a message the contact appears although that individual is no longer in my address book. I deleted all text and call history pertaining to the deleted contact but still shows up. Does anyone have the same issue and how do I fix it?

    dam122577 wrote:
    but wont restoring as new delete all my info on my phone
    Yes. That is why I added...
    it will disappear over time due to non-use.

  • Deleting all text boxes and images on one layer

    I am designing a 240 page book where nearly every page contains at least one image and at least one text box.  The book will be printed with a varnish (a fifth plate) on every image.
    The printer has instructed me to set up the varnish in the following way in InDesign:
    With the finished book, make a duplicate layer.
    Delete all text boxes on the new layer (including page numbers)
    Delete all images (but not their containers) on the new layer
    Fill all the empty image containers with 100% black
    I have done this previously with an even bigger book, and it did of course take several hours, and it's also easy to make mistakes. So I thought it might be possible to do this with a script - only problem is I know nothing about scripting - so I wonder if someone would be willing to help me out?
    Thanks in advance! Using ID CS5.5, win xp pro sp3.

    Hi Both,
    I am having a similar problem.
    I have to extract all images to a new layer, without modifying the size of the container.
    As long as I am not an expert in scripting I have tried to do a simple code to obtain all links and go to its parent to change its itemlayer property to the new layer. But it is not what I am looking for and it has not work.
    The layout of the current layer is based in tables, so images are anchored objects inside table cells, so when I run the simple script I mentioned I also get errors becuase it says the items are grouped. And even if I first run a script to ungroup everything I get the error.
    I would appreciate help.
    Thanks in advance.

  • How can I empty my trash email. There is no delete or delete all option after the emails are highlighted from the edit option. This is on an i phone 4 running on ios 7.1.2

    How can I empty my trash email. There is no delete or delete all option after the emails are highlighted from the edit option. This is on an i phone 4 running on ios 7.1.2

    I have 7 email accounts on my phone. If I tap EDIT while viewing the Trash folder in any of them I see "Mark All" on the left and "Delete All" on the right. If you tap any message, however, the legends go away.

  • How to delete all text messages in iphone5

    How do you delete all text messages on iphone5?

    ... or click the Edit button in the upper left, check the ones you want to delete and delete them that way.

  • IPhone 6 plus- how to delete some of the texts feeds in a conversation from a sender without deleting all texts from sender?

    How can you delete some of the text messages  from a sender without deleting the entire conversation from that sender? After selecting the message or messages...the top left option is "delete all" and the top right says "cancel". I simply want to save some of the history of the texts and delete those that are no longer needed.

    Hey Mari7Can,
    Thanks for the follow up. Hmmm…the options should have shown at the bottom of the screen. Let’s try forcing the Messages application to close and restarting your phone:
    Force an app to close in iOS - Apple Support
    https://support.apple.com/en-us/ht5137
    Restart or reset your iPhone, iPad, or iPod touch - Apple Support
    https://support.apple.com/en-us/HT201559
    Thanks,
    Matt M.

  • Reader deletes all URLs after signing

    Hello,
    if I've got an pdf-file with URLs in it and I sign the document and save it as an separate file, adobe deletes all URLs.
    The URLs still appear underlined and are highlited in blue, but I can't click them anymore.
    Normaly I create the pdf.files directly with Word 2010 but the same issue appears when I create them with OpenOffice Writer.
    I sign the documents withe the "Sign" option on the right sidebar and my Wacom TouchPad
    Thank You!

    Hello Sabian, thanks for reply.
    Yes, I sign with EchoSign, but I don't upload the file enywhere (adobe asks every time me to do it).
    What do you mean with "Digital Signature"?
    I now tried another signature: right sidebar -> Sign -> Work with Certificates -> Sign with Certificate
    That works fine and the URLs are avaible after saving the dicument, but I get this red triangle Watermark on the Background:

  • Iphone deleted all text messages on restart

    Hi all,
    Yesterday my phone was hanging so I gave it a restart. Once it came back up it had deleted all of my messages. Is there any way to get these back without "restore"? I had an issue before where it would not back up, which was solved by removing and reinstalling iTunes. However, the backup was very old.
    I have been told that the iphone puts all old messages in a database, even when they have been deleted. I backed up straight away.
    Please please can someone help me restore the text messages, or at the very least, tell me how I can go about viewing them on my computer (csv for example).
    Beer to who ever solves this!
    Thanks.

    Your MacBook shoul NOT be too old to run Leopard. (10.5) Snow Leopard, maybe, but not Leopard.
    When you go to the Genius Bar, MAKE SURE the genius knows you are NOT up to date with your  OS. If they try to update your phone, you could find yourself unable to sync at all...
    That said, if you're nice to them, they might just give you a copy of Leopard so you can get current with iTunes and iOS. It's been known to happen.

  • How to delete ALL text messages in Inbox at once? ...

    Hi All,  I'm a newbie here so be gentle.... 
    I've noticed that opening new texts has become increasingly slow over the last few months - ie I go to open it and it takes about 8-10 seconds before the message actually appears.  I am assuming that this may be because I have allowed my inbox to accumulate no fewer than 1650 text messages.
    I would like to delete ALL of them at once, if possible, rather than having to delete them one-by-one, which will inevitably take about 382 hours to do so at this rate
    Many thanks in advance for any suggestions on this matter 
    Solved!
    Go to Solution.

    I am afraid I have not tried the 6110 but if it works the same as other Symbian phones you can choose the Mark/Unmark > Mark All option to select all SMS and then simply select Delete. Should do the trick.

  • Ability to globally delete all text/topics that have a certain conditional tag?

    When deleting a conditional tag in FrameMaker, there is an option to either delete all tagged text, or make the text unconditional. But in Robohelp, it seems the only option is to make the text unconditional. Is there any way to delete all the text with a specific conditional tag, other than deleting it manually? I am using Robo 7. Is there an option to do this in the newer versions? Thanks!

    If you are bringing in content from FrameMaker and want to delete (not bring) certain content having a conditional text applied in the FM source, then while conversion there is a simple way to achieve it, apply "FrameMaker condition text build Expression" during Import/Link FM source.
    If you use this option while you import or Link any FM source with conditions, then all the text/content having condition set as hidden in FrameMaker will get remove from the topics generated
    but inside RoboHelp you dont have this option to directly remove content from topic, but while generating any output you have the option to exclude any content with a condition applied over it!
    Hope this will help
    Ashish

  • AppleScript Needed...How to tell Pages to delete all texts with a specific color?

    How to tell Pages to remove all texts with the font size: 13 and color " disabledControlTextColor" from the developer palette?

    Sorry John,
    Now My question has changed,
    Now I want to fill the whole cell with a color , not only the text background
    Now I have the Solution. As from the previos post itself I got the Answer. I excuse all of u to put the related question in a new thread.
    Please leave it. I won't repeat it.
    <af:column sortProperty="Salary" headerText="#{bindings.EmployeesVO1.hints.Salary.label}"
    inlineStyle="#{row.Salary>'20000'?'background-color:Red;':'background-color:Orange;'}"
    id="c6">
    <af:outputText value="#{row.Salary}" id="ot4">
    <af:convertNumber groupingUsed="false"
    pattern="#{bindings.EmployeesVO1.hints.Salary.format}"/>
    </af:outputText>
    fixes my issue.
    Ranjith C
    Edited by: Ranjith C on Jun 30, 2011 4:46 AM

  • Getting All text after a character

    I have a file name which I wish to get all of the characters after an underscore. Im sure this is easy but haven't had any luck finding the solution
    eg filename_45b to return 45b
    thanks

    It's actually fairly straightforward.
    The key is the part:
    <pre class=command>offset of "_" in t</pre>
    This returns an integer indicating the position of the specified character (in this case "_") in the string (in this case t which represents the filename).
    So if the _ was the 9th character, offset of "_" in t would return 9
    The line set o to (offset of "_" in t) + 1 would then equate to: set o to (9) + 1, or 10. You want to add one to the position of the underscore since you want to know the next character in the string.
    So now the line text o thru -1 of t equates to text 10 thru -1 of t which returns the 10th through last characters of the string - negative numbers are counted from the end of the string, so 'character -1' is the same as 'last character', etc.

  • Time Machine Deleted All Backups After Restore

    Had a very strange thing happen...I had to do a clean reinstall of Mountain Lion and manually add back everything from various backups. After the reinstall (including a completely new computer name), I was able to access all the old Time Machine backups to restore folders from. However, once I restored one folder, TM wrote over the entire old set of backups (over 6 months worth) with a new backup of the current state, deleting everything and renaming the containing folder to the new computer name. Is there any way I can get back what it deleted? How could this have happened?
    No doubt Im done using Time Machine...no matter how anal I am about backing up, I still end up losing tons of irreplacable data (I had much of the TM backup also backed up on another external drive but, go figure that drive would fail literally simultaneously as this incident).
    Any suggestions on how to move forward, if possible at all?
    Thanks!

    Depending on how you restored your data, Time Machine may not recognize your previous backups as belonging to the same machine. It will then delete as many snapshots as necessary to make room for a full backup of all files. Once the snapshots are deleted, they can't be recovered.
    Another time, when you erase the boot volume, restore from Time Machine in Setup Assistant so that your backup history will be preserved.
    As this experience shows, you need more than one backup to be safe. I suggest you start a new backup set on another, larger, external drive.

  • ITunes deletes all media after sync.

    Every now and then iTunes will remove all media on my iPhone. All movies, music and podcasts are deleted from the phone after I sync. I have to then uncheck the sync media apply, go back select sync media and re-sync the phone. Then it will copy all the media back, which takes VERY long time and is frustrating. If I don't add or remove media, why does it delete everything?

    One thing to check, look at your podcast default options and make sure they are set to keep all the episodes.
    http://img14.imageshack.us/img14/6654/podcastsettings.png
    You could also try control-A, then right-click and choose "Do Not Auto Delete"

Maybe you are looking for