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

Finding and deleting rows in text file programatically

I have many text files with consisting of a records (rows) beginning with specific segment ids.
I have the need to find these rows containing specific segment id's at the beginning of the row.  Once found the line/row needs to be deleted, i.e., not leaving a blank line.

Example:

AAA*HI*AC~
BBB*R*MI*10~
CCC*999*Y*6~
DDD*999*CD*2393939~
EEE*384*DC*340384~
FFF*823*YY*222222~

I want to open each respective text file in a  loop (like VB macro in word) and delete all rows beginning with CCC, DDD and EEE leaving no blank lines.
0
66chawger
Asked:
66chawger
  • 4
  • 3
1 Solution
 
tdlewisCommented:
Do you want a VBA macro to be run from Microsoft Word? If not, what kind of solution did you have in mind?

Is the script supposed to change all *.txt files in a directory? How do you want to specify the directory?
0
 
66chawgerAuthor Commented:
I have a word macro(s) where I can specify the data path and file types to open and save/close at the end.  I just need the code in the  middle once the file is open to do the deletions.
0
 
tdlewisCommented:
As long as the strings "CCC", "DDD", and "EEE" followed by an asterisk do not appear in the middle of a row, the following will work:
    Selection.Find.ClearFormatting
    With Selection.Find
        .text = "<[CDE]{3}>\*[!^l-^13]@[^l-^13]"
        .MatchWildcards = True
    End With
    Selection.Find.Execute
    Do While Selection.Find.found
        Selection.Delete
        Selection.Find.Execute
    Loop

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
66chawgerAuthor Commented:
tdlewis, thanks for the example.  Yes, the segment codes are always at the beginning followed by *.  Let me give this a try on a couple of the data files.
0
 
66chawgerAuthor Commented:
Ok, this definitely deleted the data on that line, however, there is a blank line left.  I need to be able to remove the blank line after the data has been deleted, or incorporate that into the delete.  I was playing around with it some, but have not found this command yet.
Please let me know if you have a solution... thanks.
0
 
66chawgerAuthor Commented:
Good info, and concise responses..thanks!
0
 
tdlewisCommented:
This code will replace all instances of two paragraph marks with one paragraph mark:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^p^p"
        .Replacement.Text = "^p"
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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