-2

I am writing a file from shell script and reading that file from java and prints % in jsp page.. Here's the file :

0K .......... .......... .......... .......... ..........  0%    6.93 MB/s
   50K .......... .......... .......... .......... ..........  0%    1.75 MB/s
  100K .......... .......... .......... .......... ..........  0%    3.78 MB/s
  150K .......... .......... .......... .......... ..........  1%    1.93 MB/s
  200K .......... .......... .......... .......... ..........  1%    3.66 MB/s
  250K .......... .......... .......... .......... ..........  2%    7.84 MB/s
  300K .......... .......... .......... .......... ..........  2%    3.65 MB/s
  350K .......... .......... .......... .......... ..........  3%    2.45 MB/s

Here's the code to get pertucular value from file :

while((line = file.readLine()) != null) {

                    int li = line.lastIndexOf("%");
                    if(li > 0) 
                        progress = Integer.parseInt(line.substring(li - 3, li).trim());
                }

Now the file format has been changed to below format :

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0  164M    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0  164M    0  272k    0     0   117k      0  0:23:54  0:00:02  0:23:52  303k
  0  164M    0  624k    0     0   186k      0  0:15:04  0:00:03  0:15:01  322k
  0  164M    0  960k    0     0   221k      0  0:12:41  0:00:04  0:12:37  328k
  0  164M    0 1296k    0     0   242k      0  0:11:33  0:00:05  0:11:28  331k
  0  164M    0 1632k    0     0   258k      0  0:10:52  0:00:06  0:10:46  333k
  1  164M    1 1968k    0     0   267k      0  0:10:28  0:00:07  0:10:21  337k
  1  164M    1 2304k    0     0   276k      0  0:10:08  0:00:08  0:10:00  337k
  1  164M    1 2656k    0     0   283k      0  0:09:52  0:00:09  0:09:43  338k
  1  164M    1 2992k    0     0   289k      0  0:09:41  0:00:10  0:09:31  339k
  1  164M    1 3328k    0     0   293k      0  0:09:32  0:00:11  0:09:21  339k
  2  164M    2 3680k    0     0   297k      0  0:09:25  0:00:12  0:09:13  341k
  2  164M    2 4016k    0     0   300k      0  0:09:19  0:00:13  0:09:06  341k
  2  164M    2 4352k    0     0   303k      0  0:09:14  0:00:14  0:09:00  340k

Without changing much I just want to perform same operation (see code) to get the value of column 1 i.e : % value.

How to modify the above code or is there any method (like lastIndexOf) that will have the first's columns value???

S. Das
  • 75
  • 3
  • 9
  • 1
    [remove multiple whitespaces](http://stackoverflow.com/questions/2932392/java-how-to-replace-2-or-more-spaces-with-single-space-in-string-and-delete-lead). Afterwards split on whitespace. This should be enough – SomeJavaGuy Sep 29 '15 at 11:08

2 Answers2

1

Assuming the first column starts at 0 and takes 3 chars

 progress = Integer.parseInt(line.substring(0, 3).trim());
Daniel Rosano
  • 695
  • 5
  • 16
  • This code will also capture %__ .. I need to make sure first line should be ignored. % Total % Received % Xferd Average Speed Time Time Time Current – S. Das Sep 29 '15 at 14:06
0
String line = "  101  164M    0  624k    0     0   186k      0  0:15:04  0:00:03  0:15:01  322k";
// replacing the whole string with percentage, if exists
String num = line.replaceAll("^\\s*([0-9]+).*", "$1");
// now, if the 'num' is really a number. for column header it will have the text
if(num.matches("[0-9]+")){
    // do whatever
    System.out.println("num >> "+num);
}

I have used regex here. First capturing the matched group, and then replaced by the capture ($1).

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85