LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-27-2009, 04:40 PM   #1
SnagglezMaw
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
Post Using GAWK in Java


I'm writing a Java program and i need it to execute a gawk command. When I run my Java program, other command executions work just fine but it fails when it tries to do the gawk part.

The command I want to run is :
Code:
gawk '{if($4>0.9) print $3}' A261u_#.cat > tmp.dat
Where # = a number generated by other parts of the program.

My current code for running the gawk command is as follows:
Code:
Process process = 
	Runtime.getRuntime().exec("gawk '{if($4>0.9) print $3}' A261u_" 
				+ meCounter + ".cat > tmp.dat");
So basically...what am I doing wrong and how can I fix it?
 
Old 05-27-2009, 07:10 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by SnagglezMaw View Post
I'm writing a Java program and i need it to execute a gawk command. When I run my Java program, other command executions work just fine but it fails when it tries to do the gawk part.

The command I want to run is :
Code:
gawk '{if($4>0.9) print $3}' A261u_#.cat > tmp.dat
Where # = a number generated by other parts of the program.

My current code for running the gawk command is as follows:
Code:
Process process = 
	Runtime.getRuntime().exec("gawk '{if($4>0.9) print $3}' A261u_" 
				+ meCounter + ".cat > tmp.dat");
So basically...what am I doing wrong and how can I fix it?
if your primary tool is java, that gawk command can be implemented in Java. the gawk command basically says check the fourth field whether is it more than 0.9, if yes, print the third field. with java you can split a string too into fields. (String.split() ???) ... look up your Java documentation for exact usage.
 
Old 05-27-2009, 10:18 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You would also help yourself (if only for debugging purposes) to separate out a) building the command text, from b) actually running the command text.

Because the text you generate may - or may not be - the text you're expecting:
Code:
import java.io.*;

public class Test
{
  public static void main (String[] args)
  {
     int meCounter = 10;
     String sCmd = "gawk '{if($4>0.9) print $3}' A261u_" + meCounter + ".cat > tmp.dat";
     System.out.println ("sCmd=" + sCmd + "...");
     Process p = Runtime.getRuntime().exec (sCmd);
  }
}
Quote:
sCmd=gawk '{if($4>0.9) print $3}' A261u_10.cat > tmp.dat...
At this point, you'd probably:
a) Make sure "A261u_10.cat" is the file you want
b) Make sure column 4 contains a valid number
c) Make sure column 3 contains valid data
d) Make sure the previous invocation didn't overwrite your "tmp.dat"
e) etc etc

You might also want to verify that your JVM's $PATH contains "gawk".

'Hope that helps .. PSM

Last edited by paulsm4; 05-27-2009 at 10:22 PM.
 
Old 05-28-2009, 02:53 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

additionally:

1. Are you sure, that your current directory is the one, which contains the file you want to parse, when you call the gawk? You don't qualify the file name with a path.

2. Did you run other external commands successfully, which are using stdout redirection? I remember of some trouble when trying this in own programs.

Jan
 
  


Reply

Tags
gawk, java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
gawk help needed. Speedy2k Linux - Newbie 2 11-07-2008 10:10 AM
gawk help.... visitnag Linux - Newbie 1 04-12-2008 11:55 AM
where is curl and gawk ???????? bozart ROCK 11 10-23-2005 10:41 PM
gawk question luxpops Programming 1 09-12-2004 04:46 AM
FS=? in gawk realos Programming 2 05-28-2003 07:30 PM


All times are GMT -5. The time now is 01:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration