LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2011, 02:25 AM   #1
shik28
LQ Newbie
 
Registered: Sep 2011
Location: India
Posts: 25

Rep: Reputation: Disabled
Selective retrieval of numbers in a text file


I have a text file as follows
Code:
             total       used       free     shared    buffers     cached
Mem:          7923       7875         47          0        564       3237
-/+ buffers/cache:       4074       3848
Swap:        12287       1067      11220
Total:       20211       8943      11267
I have to retrieve only those values which fall in the mem, swap and total under total, used and free i.e. I have to ignore 0, 564, 3237, 4074, 3848. How do I do it?

I have written the following code to display the all the numbers.
Code:
package my.scan;  
import java.util.*; //importing some java classes  
import java.lang.*;  
import java.io.*;  
  
class ReadFile {
	void getFile () {
		int i = 0;
		
	       try {  
	               Scanner myFile = new Scanner(new FileReader("C:/Documents and Settings/shikhau/workspace/SanNumber/freelast.txt"));  
	               while (myFile.hasNext()) {  
	                   if (myFile.hasNextInt()) {
	                	   i = myFile.nextInt();
	                	   System.out.println(i);
	                   }
	                   else {
	                   		myFile.next();
	                   }              
	               }  
	       }  
	       catch (FileNotFoundException e){  
	           System.out.println("Sorry! This file is not found");  
	       }
	}
}

public class ScanNum {  
    public static void main(String[] args) {  
        ReadFile rf = new ReadFile();
    	rf.getFile();
          
    }  
  
}

Last edited by shik28; 11-23-2011 at 02:26 AM. Reason: BB error
 
Old 11-23-2011, 04:40 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
Well.. I know it is not what you are looking for, but here is how to do this in awk:
Code:
$ awk '$1~/Swap|Total/{print $2,$3,$4}' test.dat
12287 1067 11220
20211 8943 11267
where
Code:
$ cat test.dat 
             total       used       free     shared    buffers     cached
Mem:          7923       7875         47          0        564       3237
-/+ buffers/cache:       4074       3848
Swap:        12287       1067      11220
Total:       20211       8943      11267
Maybe this will give you idea how to implement this in java.

Last edited by firstfire; 11-23-2011 at 04:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 09:51 AM
[SOLVED] Reading numbers from text file and storing in array idaham Linux - General 3 05-27-2010 03:36 AM
Shell script:- Reading numbers embedded in brackets from a text file rsan Linux - Newbie 6 07-05-2009 06:01 AM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Printing numbers from a text file dynamically mrobertson Programming 1 06-28-2005 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration