Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 723617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:07:42+00:00 2026-05-14T06:07:42+00:00

I’ve got a problem where calling grep from inside java gives incorrect results, as

  • 0

I’ve got a problem where calling grep from inside java gives incorrect results, as compared to the results from calling grep on the same file in the shell.

My grep command (called both in Java and in bash. I escaped the slash in Java accordingly):

/bin/grep -vP --regexp='^[0-9]+\t.*' /usr/local/apache-tomcat-6.0.18/work/Catalina/localhost/saccitic/237482319867147879_1271411421

Java Code:

String filepath = "/path/to/file";
String options = "P";
String grepparams = "^[0-9]+\\t.*";
String greppath = "/bin/";

String[] localeArray = new String[] {
    "LANG=",
    "LC_COLLATE=C",
    "LC_CTYPE=UTF-8",
    "LC_MESSAGES=C",
    "LC_MONETARY=C",
    "LC_NUMERIC=C",
    "LC_TIME=C",
    "LC_ALL="
};

options = "v"+options; //Assign optional params

if (options.contains("P")) {
    grepparams = "\'"+grepparams+"\'"; //Quote the regex expression if -P flag is used
} else {
    options = "E"+options; //equivalent to calling egrep
}

proc = sysRuntime.exec(greppath+"/grep -"+options+" --regexp="+grepparams+" "+filepath, localeArray);
System.out.println(greppath+"/grep -"+options+" --regexp="+grepparams+" "+filepath);
inStream = proc.getInputStream();

The command is supposed to match and discard strings like these:

85295371616 Hi Mr Lee, please be informed that...

My input file is this:

85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
8~!95371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
852&^*&1616 Hi Mr Lee, please be informed that...
8529537Ax16 Hi Mr Lee, please be informed that...
85====ppq16 Hi Mr Lee, please be informed that...
85291234783 a3283784428349247233834728482984723333
85219299222

The commands works when I call it from inside bash (Results below):

85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
8~!95371616 Hi Mr Lee, please be informed that...
852&^*&1616 Hi Mr Lee, please be informed that...
8529537Ax16 Hi Mr Lee, please be informed that...
85====ppq16 Hi Mr Lee, please be informed that...
85219299222

However, when I call grep again inside java, I get the entire file (Results below):

85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85aaa234567 Hi Ms Chan, please be informed that...
85292vx5678 Hi Mrs Ng, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
8~!95371616 Hi Mr Lee, please be informed that...
85295371616 Hi Mr Lee, please be informed that...
852&^*&1616 Hi Mr Lee, please be informed that...
8529537Ax16 Hi Mr Lee, please be informed that...
85====ppq16 Hi Mr Lee, please be informed that...
85291234783 a3283784428349247233834728482984723333
85219299222

What could be the problem that will cause the grep called by Java to return incorrect results? I tried passing local information via the environment string array in runtime.exec, but nothing seems to change. Am I passing in the locale information incorrectly, or is the problem something else entirely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:07:43+00:00Added an answer on May 14, 2026 at 6:07 am

    The single quotes are used by the shell, and not seen by grep. You should not add them in java.

    Also, you should use the exec(String[], String[]) method instead of concatenating the parameters, so you have control over the separation of the parameters.

    And i agree with the other comments saying you should do this in pure java instead of starting grep in a separate process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, when you add the button's listener to a function… May 14, 2026 at 9:08 pm
  • Editorial Team
    Editorial Team added an answer Also, Be aware that when the list is empty, the… May 14, 2026 at 9:08 pm
  • Editorial Team
    Editorial Team added an answer I found an article describing it here. And a few… May 14, 2026 at 9:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.