-2

I have a file that contains some data, I want to delete some lines from this file that contains specific substrings.

example of my file:

{1:F21FIEGEGCXAXXX4781478239}{4:{177:1603150825}{451:0}}
{1:F01FIEGEGCXAXXX4781478239}
{2:O1030924160315RJHISARIAXXX83068856911603150825N}
{4::20:PO/180797059/767
:23B:CRED
:32A:160315USD1405,19
:50K:/213000010006082015619
M A A H
TABOUK 71411 PO BOX 000001
:53B:/152700
:57A:FIEGEGCXMIN
:59:/3829
ESAM ABDELALIM ABDELRAHMAN HABIB
EGYPT
:70:Family Expenses
:71A:SHA-}
{5:{MAC:00000000}{CHK:C04D5471B9E9}}{S:{SAC:}{COP:P}}

I want to delete every line that ends with {451:0}} like the first line and {COP:P}} like the last line.

roeygol
  • 4,908
  • 9
  • 51
  • 88
  • 2
    have you tried anything? please provide [mcve] of what you've tried – ItamarG3 Jan 10 '17 at 09:59
  • See this question which gives MOST of what your looking for, needs modified to support End Of Line only: http://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it – GavinF Jan 10 '17 at 10:00
  • 1
    Possible duplicate of [Delete specific line from a text file?](http://stackoverflow.com/questions/1245243/delete-specific-line-from-a-text-file) – Andremoniy Jan 10 '17 at 10:00
  • @Andremoniy, that is very similar, but C# where this question is Java. – Ole V.V. Jan 10 '17 at 10:32

2 Answers2

1

If the input files are not huge. Following could be a solution starting point.

Path in = Paths.get("in.txt");
Path out = Paths.get("out.txt");
Files.write(out, Files.lines(in, StandardCharsets.UTF_8)
        .filter(s -> !s.endsWith("{451:0}}") && !s.endsWith("{COP:P}}"))
        .collect(Collectors.toList()));

edit As suggested by Klitos Kyriacou in the comment. For huge files we should skip the creation of a temporary list.

Stream<String> inStream = Files.lines(in, StandardCharsets.UTF_8)
        .filter(s -> !s.endsWith("{451:0}}") && !s.endsWith("{COP:P}}"));
Files.write(out, (Iterable<String>) inStream::iterator);
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Nice solution for small files, but why create the required `Iterable` from a `Stream` by creating a whole `List`? You can instead do `Files.write(out, (...)::iterator)` - the method reference syntax is shortcut for creating an `Iterable` interface implementation, as shown in http://stackoverflow.com/a/20130475/638028. With this, it can cope with huge files too! – Klitos Kyriacou Jan 10 '17 at 11:17
  • @KlitosKyriacou Simply I have not tought about it. I added your suggestion to the answer. Thanks for the hint. – SubOptimal Jan 10 '17 at 12:25
0

Something like this -

    try{
        Scanner scanner = new Scanner(new File("input.txt"));
        Writer writer = new PrintWriter(new File("output.txt"));
        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            if(!line.endsWith("{451:0}}") && !line.endsWith("{COP:P}}")){
                writer.write(line+System.getProperty("line.separator"));
            }
        }
        scanner.close();
        writer.flush();
        writer.close();
    }catch(Exception e){
        e.printStackTrace();
    }
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45