I want to delete a whole line in txt file where i found a string with DATE format.

#The date have specific format  mm/dd/yyyy
#I Need to find a line where is date older than one year and then erase it
$Date = (Get-Date).AddDays(-365)
write-host "-----------------------"
$a= '\d{2}\/\d{2}\/\d{4}'
Select-String -pattern "$a"  -Path C:\test\file.txt -AllMatches |
    ForEach-Object {
        $Info = $_ | #
            Add-Member -MemberType NoteProperty -Name Date -Value $null -PassThru |     
            Add-Member -MemberType NoteProperty -Name Captured -Value $null -PassThru   
        foreach ($Match in $_.Matches) {
            try {
                $Date = [DateTime]::ParseExact($Match.Value,"MM\/dd\/yyyy",$null)     
             if($Datum -lt $Date){
                 write-host "$Date is up to date"
                    else{
                        write-host "$Date is old" }  # here should be command which delete the line
        catch {
                $Date = 'NotValid'

Or you can use Set-Content like in mjolinor's example, or just pipe to out-file like I suggested, but in both cases you'd need to enclose the get-content in parentheses:
$Date = (Get-Date).AddDays(-365)
$a= '\d{2}\/\d{2}\/\d{4}'
(Get-Content C:\test\file.txt) |
Where-Object {$_ -notmatch $a -or $(Select-String -pattern "$a" -inputobject $_ -AllMatches |
Foreach-Object {$good = $true;$(Foreach ($match in $_.matches) {If([datetime]$match.value -lt $date){$good = $False}};$good)})} |
Set-Content C:\test\file.txt
Make sure it works as expected then change the filename to file.txt or test it without piping it to file by reviewing it in the console first.  Do you have multiple dates on any lines of your text file?  If not, the other answers are more appropriate.
I hope this post has helped!

Similar Messages

  • AppleScript to delete the whole line if ..

    Im using the following code to find and replace with "target word" but I would like to DELETE the whole line instead of replacing the actual word.
    so it would be
    if the line contains www.network -> Delete the whole line.
    Thanks in advance!
    set theTargetWords to {"www.network"}
    tell application "Pages" to activate
    tell application "System Events" to tell process "Pages"
      -- Open the "Find & Replace" window:
      keystroke "f" using command down
              repeat until exists window "Find & Replace"
              end repeat
              tell tab group 1 of window "Find & Replace"
      -- Adjust the settings:
      click radio button 2 -- Advanced
                        if value of checkbox 2 is 1 then click checkbox 2 -- Don't match case
                        if value of checkbox 3 is 1 then click checkbox 3 -- Whole words
                        if value of checkbox 1 is 0 then click checkbox 1 -- Search previous text (loop)
      click pop up button 1 -- find in
      click menu item 1 of menu 1 of pop up button 1 -- Entire document
      click pop up button 2 -- replace style
      click menu item 1 of menu 1 of pop up button 2 -- Any
      -- Delete every target word:
                        set value of text area 1 of scroll area 2 to ""
                        repeat with thisWord in theTargetWords
                                  set value of text area 1 of scroll area 1 to thisWord -- the target word
                                  click button "Replace All"
                        end repeat
              end tell
    end tell

    if you want to script an application, read and use the application scripting dictionary. using System Events to script the GUI is something you should only do as a last resort. It creates clunky, slow, overly complex, and breakable scripts.
    try this to start:
    tell application "Pages"
              delete (every paragraph of body text of document 1 whose contents contains "some key phrase")
    end tell

  • How to delete a whole line in cmd prompt in Linux

    Hi all,
    How to delete a whole line in cmd prompt in Linux using short key?
    ex: [oracle@host1 ~]$ cd /u01/app/oracle/product/11.2.0/dbhome_1/
    Thanks

    yes ctrl+ak or ctrl+eu both are better. if you look from that perspective, go end and delete will be the same job too. (ctrl+eu) (but for me in such a command line, for example cding in to the directories ,the cursor will be at the end of the line %99 of the time :) )
    about ctrl-t,
    Drag the character before the cursor forward over the character at the cursor, moving the cursor forward as well. If the insertion point is at the end of the line, then this transposes the last two characters of the line.
    for ex:
    misspelled command date -> daet -> ctrl+t -> date
    or
    misspelled command $APPPL_TO -> place the cursor on third P press ctrl+t repeateadly till the P will come to the end -> APPL_TOP

  • How do I delete duplicate songs in my iTunes? I have hundreds and I don't want to delete them one by one. They were all added on the same date, so sorting by the date won't help.

    How do I delete duplicate songs in my iTunes? I have hundreds and I don't want to delete them one by one. They were all added on the same date, so sorting by the date won't help.

    Hi, if this is in regards to your library simply open up itunes and do the following steps:
    Click File
    Scroll down to "show duplicates"
    A list will then appear of your duplicate song titles.
    Be sure to CAREFULLY review each song to make sure it is a duplicate ( as I have some music that is the same song but live, acoustic etc...)
    Proceed to manually delete each song from the list and leave alone any song that you wish to keep.
    Best of luck,
    Cait

  • If I want to delete other people's iCloud accounts from my phone will it interfere with the backups on their phones?

    If I want to delete other people's iCloud accounts from my phone will it interfere with the backups on their phones?

    Hello Stacey140
    You should not have an issues with removing accounts and messing with others users backup. As long as you are not removing the main account that is yours that you use for back up, everything will be fine.
    iCloud: iCloud storage and backup overview
    http://support.apple.com/kb/PH12519
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G.

  • I want convert string to date format in Oracle

    Dear All
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'

    >
    Hi Parwez,
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'SELECT TO_DATE('Friday, 02 March 2012 2:44 P.M.', 'DAY, DD MONTH YYYY HH:MI A.M.') from dual;
    As well as what the other poster suggested, look here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
    HTH,
    Paul...

  • Xcode - delete the whole line shortcut missing

    Hi,
    Is there any way to have a shortcut to delete the line? you know, like D from Vi, or CMD+D from Eclipse, o CTRL+D from JEdit, or ... you got it.
    please tell me I'm blind and there is a shortcut for that.
    just for the record, there is a shortcut option to SELECT LINE, so why not one for DELETE LINE ?
    please help,
    Thanks,

    Hi Grimmac
    Many of the applications that Apple themselves make include a sub-set of commands from the emacs editor. Xcode is no exception to this. One of the useful commands from emacs is control-K (or ^K). The first press deletes from the cursor to the end of the line, the second press deletes the carriage return and brings up the next line. Subsequent presses repeat this sequence of events.
    Other useful commands are ^A and ^E to move to the start/end of a line, so to delete a complete line from the beginning you can use ^A^K^K.
    What makes these commands really worth knowing is that you can use them in Safari to edit URLs, or in Terminal to edit Unix commands, and in many other Apple made applications.
    If you do a Google search for "emacs" then you can quickly find the full list of commands. Xcode only supports a few of them, mainly the control + single key ones.
    You can also make your own personal keyboard shortcuts from the Xcode Preferences menu option.
    Bob

  • Read at each new line from .txt file

    Hi guys
    I want to read from a file which I am saving using labview. What I am doing is reading 128 bytes fro VISA port and saving it in .txt file with new line constant. At some later time I want to read 128 bytes from that text file do some processing and display it. The "read from text file" vi has convert EOL but I am not able to configure it. Please help me..
    Thanks in advance
    Niladri

    Hi Newbee_3,
                                                  Do 1 thing, Read the txt file & convert this into string, Then you have all the flexibility to do whatever you want.
    Thanks
    Kudos are always welcome if you got solution to some extent.
    I need my difficulties because they are necessary to enjoy my success.
    --Ranjeet

  • How to remove balnk line on txt file??

    Anyone can help me please?
    Tks,
    Elber.

    may be this helps??
    How to Delete a line from a file?
    http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/removing-last-line-from-a-txt-file-798004

  • How to skip first TWO Lines of   .txt  file    using XSLT Mapping

    Hi Friends  ,
                              I have an .txt file in has the format as
                               <TEST>
                                4564564545
                                56456444566
                                56465
                                    How can i skip the first two Lines when i am writing the XSLT Mapping ?
                              That <TEST> and empty line shouldn't go the rfc  .
                             How can i skip and sent to rfc  using XSLT Mapping ?
    Best Regards .,
    V.Rangarajan

    you can avoid the empty lines in your File Content Conversion by defining offset.
    <i>Under Document Offset, specify the number of lines that are to be ignored at the beginning of the document.
    This enables you to skip comment lines or column names during processing. If you do not make an entry, the default value is zero lines.</i>
    ref: http://help.sap.com/saphelp_nw04/helpdata/en/2c/181077dd7d6b4ea6a8029b20bf7e55/content.htm
    then the generated XML after FCC will not have the empty lines.

  • Adding line to txt file and then rename

    Hello there. I am trying to create a class that will add a line to a txt file and then if the nuber of lines are greater than 5 it will rename the file.
    I think I have solved it with this class.
    My problem is that the file2.renameTo does not work does anyone have any suggestions why?
             public void CopyLines(String string, String location, String fileName) throws IOException {
                 BufferedReader inputStream = null;
                 PrintWriter outputStream = null;
                   GregorianCalendar calendar = new GregorianCalendar(Locale.UK);
                boolean b=false;
                 try {
                     int counter = 0;
                      File file2 = new File(location + fileName);
                      ArrayList arrays = new ArrayList();
                      if (file2.exists()){
                     inputStream =
                         new BufferedReader(new FileReader(file2));
                     String l;
                     while ((l = inputStream.readLine()) != null) {
                          arrays.add(l);
                         counter++;
                     outputStream =
                         new PrintWriter(new FileWriter(location + fileName));
                      if (!arrays.isEmpty()){
                           for(int i= 0; i<arrays.size();i++){
                              outputStream.println(arrays.get(i));
                     outputStream.println(string);
                     if (counter>5){
                          String newFileName = location + fileName + calendar.getTime().getTime();
                          b = file2.renameTo(new File(newFileName));
                 } finally {
                     if (inputStream != null) {
                         inputStream.close();
                     if (outputStream != null) {
                         outputStream.close();
             }

    What is ". . .does not work. . ."?

  • Issue with Date Format in line Graph X-AXIS

    Hi all,
    I have a line graph, with data in x-axis and integer values in y-axis.
    I am fetching data in Mon-dd-yyyy format but in graph (x-axis) it showing in yyyy-mm-dd format.
    How to change this format.
    Regards,
    Krishna Prasad

    Hi,
    Basically you are trying to convert a date into date which is not advisable. Hence convert it to char and then equate it against a character value. Also do not use trunc as it will trim out the time part of the date.
    select * from test
               where to_char(dob,'RRRR/MM/DD HH24:MI:SS')=:p1Then pass the parameter
    cheers
    VT
    Edited by: VT on Oct 26, 2010 4:16 PM

  • How to write data in differt line in .txt file

    Hi all,
    I am tring to write data on one file using utl_file,
    the first cursor loop write data on first line of the file using
    utl_file.put_line(..........................);
    end the second cusor loop write the data on secion line of the same file using
    utl_file.put_line(..........................);
    file format we are given as .txt;
    problem we are facing is when we open the file in notepad all the data is showing in one line...
    same file if we open another tools like edit plus, word pad etc it showing the write formate( line by line)
    Can any one help on this we need to set any setups in note pad????????????/

    As others have said it sounds like an operating system difference.
    If your Oracle is installed on a unix based server then the file will be written using Unix style newlines which are just Line Feed characters.
    In Windows environments a newline is considered to be made of the Carriage Return/Line Feed character pair, not just the Line Feed.
    Some applications such as MS Word or Wordpad can read Unix style linefeeds and interpret them as you expect as a Newline. However Notepad doesn't.
    When you transfer the file from Unix to Windows, usually this is done using an FTP based mechanism. If you transfer the files using FTP Binary mode, then every byte of the file is transferred as it is on Unix, and the file will essentially be a unix file on a windows system. If, however, you transfer the files (from Unix to Windows) using FTP ASCII mode, then whenever FTP encounters a LineFeed character it will convert it to a Carriage Return/LineFeed pair of characters, so that the file will appear correctly in Notepad. Likewise if you transfer files from Windows to Unix in ASCII mode, then Carriage Return/LineFeed pairs of characters are converted to just a LineFeed character so that the file becomes Unix compatible. Note, you shouldn't transfer binary files in ASCII mode as it can corrupt them, it's only intented for text based files.

  • Delete a line from txt file

    Is it possible to search for a string in a text file then
    delete the line where the string is found.
    For ex. if I have a list of names in a text file, I would
    like to be able to search for a name, based on user's input, then
    if the name is found, delete the line where the name was listed at.
    Thanks

    Is each name on a separate line? If so, each line is
    terminated with a <BR>, </P>, etc. (unless it is in its
    own table cell).
    Then one way to handle it would be to convert the line
    terminator to a pipe ( | ) character, giving you a pipe delimited
    list. The ListFind( ) function could be used to find the list
    element that has the name in it and the ListDeleteAt( ) function
    deletes the name.
    Sample code below. There are other ways to handle it if the
    lines are in their own individual table cell.

  • Problem with receiver file adapter printing data line by line in .txt file

    We are working on e-payments integration with one of the bank. As part of text file generation, we have a written a UDF which accumulates all Invoice details( 10 input fields information ) into one string and then sends output line by line with a delimiter separation for each invoice.
    H
    D
    E
    E
    E
    T
    Bank requires a file in above format separating invoice in separate E band.( one invoice collection in one E band)
    As per our scenario, multiple invoice details from an idoc(PAYEXT) have to be written to a text file line by line.  i.e one invoice details per one row.
    After the execution of the scenario, in SXMB_MONI, the message display clearly showing that the invoice details are printed line by line. A java function used in message mapping for the writing the invoice details line by line.
    But actual file that is written to a text file is showing all the invoice details in written in single row.
    When I enable the option Word wrap in the notepad, then only I can see the details in desired format.
    Please guide us where we are going wrong.
    Can notepad settings make any difference while displaying the text file???
    Is there any way to write the file as desired without enabling the word wrap in notepad.
    Thanks in advance.
    Nagendar

    Hi venkatnrj,
    i hope this UDF might be its helps
    public void CombineText(String[] var1, String[] var2, String[] var3, String[] var4, String[] var5, ResultList result, Container container) throws StreamTransformationException{
    String s = "";
    for( int i = 0 ; i < var1.length ; i++)
         if ( var1<i> != ResultList.CC)
         s = s +  var1<i> + "," + var2<i> + "," + var3<i> + "," + var4<i> + "," + var5<i>  + "\n" ;
    result.addValue( s );
    Output diplay like below the format by using above udf:
    aaa,bbb,cccc,dddd,eee,fff,
    aaa,bbb,cccc,dddd,eee,fff,
    aaa,bbb,cccc,dddd,eee,fff,
    aaa,bbb,cccc,dddd,eee,fff,

Maybe you are looking for

  • Upgrade 9.2.0.6 to 9.2.0.8 to 11.2.0.2

    We've got a 9.2.0.6 database on AIX 5.3. We need to get it to 11.2.0.2 on AIX 6.1 - I realise that there is an intermediate step to get to 11.2.0.2 I've been told that I only have to install the 9.2.0.8 binaries on the AIX 6.1 machine. That I could c

  • How do I add a "stroke" to an image in Motion 5?

    I have a rectangle image.  I want a border on it (a stroke). How do I add one of those?

  • Fingerprint windows 7-64 login problem

     I have new installation of windows 7 64. After applying the exchange profile the fingerprint stop working for windows login. It works for power on and for entering the software control panel. I remove it and re install without success. Appreciates

  • Can an ipad do everything a laptop does?

    Looking to buy a new tech item. should it be laptop or ipad?

  • Duplicate Posting - Asset Acquisition

    Hi Gurus We are on 4.7 in a Production Environment. In fiscal year 2007 we had acquired 150 assets. During that time, we had wrongly posted acquisition twice for all the 150 assets. We have already closed the fiscal years for 2007 & 2008. Our company