0

I have a Java program that concatenates two text files. So, I have file1.txt and file2.txt and all the records from file1.txt are merged with file2.txt ON file2.txt. After concatenation, file2.txt is sorted numerically. However, file2.txt has a header and I need to find a way to ignore that header(not delete, ignore!) when merging the files because it gets messed up with the records after being sorted.

Please help me find a way to ignore the first line of file2.txt

Here is the code:

 public static void sortRecords(String fileName, String overflow) {

    String s = null;
    String main_file = fileName;
    String overflow_file = overflow;

    String command = "sort -nk1 " + fileName + " -o " + fileName;

    try {

        //cat
        Process merge = new ProcessBuilder("/bin/bash", "-c", "cat " + overflow_file + " >> " + main_file).start();


        //sort
        Process sort = Runtime.getRuntime().exec(command);
      } catch (IOException e) {

    }
}

Example:

file1.txt

2  ABC 

3  ABC

file2.txt (initially blank, except the header)

ID Name

file2.txt (after concatenation and sort)

ID Name

2  ABC

3  ABC

After concatenation, I empty file1.txt. When user adds records, they are initially stored in file1.txt and after a certain number of records, they are concatenated with file2.txt. So, the problem appears when adding records(besides the first time). They somehow get messed up, something like this:

file2.txt

3  ABC

ID NAME

1  ABC

2  ABC
useruser123
  • 89
  • 1
  • 11
  • Could you add examples of `file1.txt`, `file2.txt` and the expected result? – acm Sep 11 '16 at 22:12
  • @acm Yes, just did :) – useruser123 Sep 11 '16 at 22:31
  • I'm not able to reproduce your problem. After I run the `sort -nk1` for your `file2.txt` I obtain the expected output with the header on top if there are not blank lines in your file. Which makes sense since ordering numerical values when string is not a number is treated as zero: [Strings not of this form are treated as zero](https://github.com/wertarbyte/coreutils/blob/master/lib/strnumcmp-in.h#L44). I tested it using `GNU bash, version 4.3.46` and `sort (GNU coreutils) 8.25`. – acm Sep 12 '16 at 06:38

0 Answers0