1

While executing a below command in linux

root@11.222.33.38.11:~#  cat /etct/user.conf

I will get following data.

[General]
ui_language=US_en

From this i need to fetch the value of ui_language (US_en) only using regular expression.

divz
  • 7,847
  • 23
  • 55
  • 78

2 Answers2

1

You can pipe to grep and sed:

cat foo.txt | grep 'ui_language=' | sed 's/ui_language=\(.*\)/\1/g'

Replace foo.txt with your file name.

Example:

host$ cat foo.txt 
[General]
ui_language=US_en
host$ cat foo.txt | grep 'ui_language=' | sed 's/ui_language=\(.*\)/\1/g'
US_en
host$ 

EDIT: I didn't realize you wanted to use java. This was not originally tagged with java. You can do that with the following:

MatchExample.java

import java.util.regex.*;
import java.io.File;
import java.nio.file.Files;

public class MatchExample {

    public static void main(String[] args) throws Exception {

        byte[] bytes = Files.readAllBytes((new File(args[0])).toPath());
        String s = new String(bytes,"UTF-8");

        Pattern pattern = Pattern.compile("ui_language=(.*)");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()){
            System.out.println(matcher.group(1));
        }
    }

}

This takes filename as the first parameter.

Compile:

host$ javac MatchExample.java 

Run:

host$ java MatchExample foo.txt 
US_en
host$ 
jason120
  • 94
  • 3
0
Pattern pattern = Pattern.compile("US_en");
Matcher matcher = pattern.matcher(mydata);
    if (matcher.find()){
        System.out.println(matcher.group(1));
    }

here,mydata is a string from which you want to fetch data

sandip bharadva
  • 629
  • 1
  • 6
  • 19