1

I want to remove newlines and tap, spaces in file by using java. after removing it to be formatted in single line.

input :

12545|Logoff|MAVENSI8-PC|NULL|4634|4634|4|(S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,3)|Security|An account was logged off.

Subject:
Security ID:        S-1-5-21-2692671718-3285579559-3264406991-1000
Account Name:       MAVENSI8
Account Domain:     MAVENSI8-PC
Logon ID:       0x2e9030

Logon Type:         3

This event is generated when a logon session is destroyed. It may be positively correlated with a logon event using the Logon ID value. Logon IDs are only unique between reboots on the same computer.|46032|Microsoft-Windows-Security-Auditing|20140605041853.569120-000|20140605041853.569120-000|Audit Success|(null)
12544|Logon|MAVENSI8-PC|NULL|4624|4624|4|(S-1-0-0,-,-,0x0,S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,3,NtLmSsp ,NTLM,SRV01,{00000000-0000-0000-0000-000000000000},-,NTLM V1,128,0x0,-,192.168.1.3,36878)|Security|An account was successfully logged on.

Subject:
Security ID:        S-1-0-0
Account Name:       -
Account Domain:     -
Logon ID:       0x0

Logon Type:         3

New Logon:
Security ID:        S-1-5-21-2692671718-3285579559-3264406991-1000
Account Name:       MAVENSI8
Account Domain:     MAVENSI8-PC
Logon ID:       0x2e9030
Logon GUID:     {00000000-0000-0000-0000-000000000000}

Process Information:
Process ID:     0x0
Process Name:       -

Network Information:
Workstation Name:   SRV01
Source Network Address: 192.168.1.3
Source Port:        36878

Detailed Authentication Information:
Logon Process:      NtLmSsp 
Authentication Package: NTLM
Transited Services: -
Package Name (NTLM only):   NTLM V1
Key Length:     128

This event is generated when a logon session is created. It is generated on the computer that was accessed.

The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).

The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.

The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.
- Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.
- Transited services indicate which intermediate services have participated in this logon request.
- Package name indicates which sub-protocol was used among the NTLM protocols.
- Key length indicates the length of the generated session key. This will be 0 if no session key was requested.|46031|Microsoft-Windows-Security-Auditing|20140605041823.673410-000|20140605041823.673410-000|Audit Success|(null)
12548|Special Logon|MAVENSI8-PC|NULL|4672|4672|4|(S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,SeImpersonatePrivilege

output:

12545|Logoff|MAVENSI8-PC|NULL|4634|4634|4|(S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,3)|Security|An account was logged off.Subject:Security ID:  S-1-5-21-2692671718-3285579559-3264406991-1000  Account Name:       MAVENSI8    Account Domain:     MAVENSI8-PC Logon ID:       0x2e9030Logon Type:         3This event is generated when a logon session is destroyed. It may be positively correlated with a logon event using the Logon ID value. Logon IDs are only unique between reboots on the same computer.|46032|Microsoft-Windows-Security-Auditing|20140605041853.569120-000|20140605041853.569120-000|Audit Success|(null)
12544|Logon|MAVENSI8-PC|NULL|4624|4624|4|(S-1-0-0,-,-,0x0,S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,3,NtLmSsp,NTLM,SRV01,{00000000-0000-0000-0000-000000000000},-,NTLM V1,128,0x0,-,192.168.1.3,36878)|Security|An account was successfully logged on.Subject:Security ID:       S-1-0-0 Account Name:       -   Account Domain:     -   Logon ID:       0x0Logon Type:          3New Logon: Security ID:        S-1-5-21-2692671718-3285579559-3264406991-1000  Account Name:       MAVENSI8    Account Domain:     MAVENSI8-PC Logon ID:       0x2e9030    Logon GUID:     {00000000-0000-0000-0000-000000000000}Process Information:  Process ID:     0x0 Process Name:       -Network Information:   Workstation Name:   SRV01   Source Network Address: 192.168.1.3 Source Port:        36878Detailed Authentication Information:   Logon Process:      NtLmSsp     Authentication Package: NTLM    Transited Services: -   Package Name (NTLM only):   NTLM V1 Key Length:     128This event is generated when a logon session is created. It is generated on the computer that was accessed.The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.The authentication information fields provide detailed information about this specific logon request.    - Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.  - Transited services indicate which intermediate services have participated in this logon request.  - Package name indicates which sub-protocol was used among the NTLM protocols.  - Key length indicates the length of the generated session key. This will be 0 if no session key was requested.|46031|Microsoft-Windows-Security-Auditing|20140605041823.673410-000|20140605041823.673410-000|Audit Success|(null)
12548|Special Logon|MAVENSI8-PC|NULL|4672|4672|4|(S-1-5-21-2692671718-3285579559-3264406991-1000,MAVENSI8,MAVENSI8-PC,0x2e9030,SeImpersonatePrivilege

i want this formatted output...

Please, any one help me.

Thanks in advance.

lefloh
  • 10,653
  • 3
  • 28
  • 50
Kannan Arumugam
  • 1,119
  • 2
  • 18
  • 27
  • One thing is still missing: show us what you have already tried. – PM 77-1 Jun 07 '14 at 04:11
  • I think one thing that the current answers are missing is that only *some* of the newlines need to be replaced... And some form of `System.getProperty("line.separator")` should be used instead of hardcoding `"\n"` – awksp Jun 07 '14 at 04:18

4 Answers4

0

Forget Regex, just iterate through it.

BufferedReader in = new BufferedReader(new FileReader("file.txt"));

String line;
List<String> lines = new ArrayList<String>();

while ((line = in.readLine()) != null) {
    line = line.replaceAll("\t", "");  
    line = line.replaceAll("\n", "");  
    line = line.replaceAll(" ", ""); 
    lines.add(line);
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
0

Try to use the replace function.

String str = " \n a b c \n 1 2 3 \n x y z ";
str = str.trim().replaceAll("\n ", "");

Hope this solves your problem.

Jay Nirgudkar
  • 426
  • 4
  • 18
0

If you really wanted to remove newlines, tabs and spaces, the regex would be terrifyingly simple:

\s+

But you actually want to leave spaces between the words.

To do this, while still removing spaces at the beginning and end of lines, you can use:

[ ]*[\r\n\t]+[ ]*

To replace in Java regex, something like:

String your_result_string = your_original_string.replaceAll("[ ]*[\r\n\t]+[ ]*", "");

Explain the Regex:

[ ]*                     # any character of: ' ' (0 or more times)
[\r\n\t]+                # any character of: '\r' (carriage return),
                         # '\n' (newline), '\t' (tab) (1 or more
                         # times)
[ ]*                     # any character of: ' ' (0 or more times)
zx81
  • 41,100
  • 9
  • 89
  • 105
0

try this

    String unformattedText = FileUtils.readFileToString(new File(
            "test.txt"));
    String[] splittedText = unformattedText.split("\\n\\d");
    for (String split : splittedText) {
        System.out.println(split.replaceAll("\\n","").replaceAll("\\s", ""));
    }

Use Apache Commons IO FileUtils for IO/File related stuffs

Output :Exactly what you need

Ashok_Pradhan
  • 1,159
  • 11
  • 13