Find the same String in different Strings - java

i want to extract these Strings (XXXXX,GGGGG,PPPPP) from this Strings:
COPY XXXXX,PFX='PPPPP';
COPY XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';
COPY XXXXX;
COPY XXXXX,'PPPPP';
COPY 'XXXXX','PPPPP','GGGGG';
COPY 'XXXXX','PPPPP',SUPPR='YES';
COPY XXXXX,PPPPP,GGGGG;
My problem is, that all these strings are different and i can't extract them. For every singel string i can do a regex, but not for all in one method.
xxxx can be e.g. TWT000
PPPP can be e.g. TWS000
GGGG can be e.g. TWSOOO
Any chance of getting all string types in one method to extract XXXX,PPPP,GGGG?

Split every string by space, then split by coma(,). For example(COPY XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';):
first step:
COPY
XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';
next step:
XXXXX
PFX='PPPPP'
GRUPPE='GGGGGG';
each line is a cell in array after splitting. Use some ifs to make regexp check or something if you must, for example in SUPPR='YES'; you don't want to parse YES.
Now extract everything inside '' quotations.
You have successfully extract your data.

use batch files and cmd commands you will find alot of helpful functions there
#echo off
findstr /m /c:%1 %2
if %errorlevel%==0 (
echo found
)
%1 is the string you are looking for
%2 is the text file you are searching into
and you can output the result into another file with ">"

Related

Find words in multiple files starting with a specific set of characters and replace the whole word with another word

I need to read through multiple files and check for all occurrences of words that start with a specific pattern and replace it in all the files with another word. For example, I need to find all words beginning with 'Hello' in a set of files which may contain words like 'Hellotoall' and then I want the word to be replaced with 'Greetings', just an example. I have tried:
content = content.replaceAll("/Hello(\\w)+/g", "Greetings");
This code results in : Greetingstoall, but I want the whole word to be replaced with 'Greetings', i.e. if the file has a line:
Today i say Hellotoall present here. After replacement the line should be like: Today i say Greetings present here.
How can I achieve such a requirement with a better regex.
You need just "Hello(\\w)*".
isn't the output Greetingsoall? The match would be Hellot - so first thing is that you may want to replace + with *
As talex pointed out, there is sed syntax mixed in, which doesn't work with Java.
content.replaceAll("Hello\w*", "Greetings")

Java 8 programming: Reading a .ini-file and trying to get rid of newline-characters

I'm using Netbeans IDE. For a school project I need to read an .ini-file, and get some specific information.
The reason I'm not using ini4j:
I have a section that has key values which are the same
I have sections that have no key-value inputs that I have to read information from
Example ini-file:
[Section]
Object1 5 m
number = 12
Object2 6 m
;Comment followed by white line
number = 1\
4
\ means the next command or white lines need to be ignored
So the last part of the ini file actually means: number = 14
My task: I need to store the oject names with the corresponding length (meters) and number into a single string like this:
Object1 has length 1m and number 12
My problem:
I use a scanner with delimiter //Z to store the whole file into a single String.
This works (if I print out the String it gives the example above).
I've tried this code:
String file = file.replaceAll("(\\.)(\\\\)(\\n*)(\\.)","");
If I try to only remove the newlines:
String file = file.replace("\n","");
System.out.println(file);
I get an empty output.
Thanks in advance !
You are on right way. But logic is on wrong place. You actually need \n for your logic to recognize new value in your ini file.
I would suggest that you do not read entire file to the string. Why? You will still work with line from file one by one. Now you read whole file to string then split to single strings to analyze. Why not just read file with scanner line by line and analyze these lines as they come?
And when you work with individual line then simply skip empty ones. And it solves your issue.
Your problem is that you need to esacpe \ in Java Strings and in regular expressions, so you need to escape them twice. This means if you want to get rid of empty lines you have to write it like this:
file = file.replaceAll("\\n+", "\n");
If you know that a \ at the end of a line is always followed by an empty line then this means that it is actually followed by 2 new line characters which would give the following:
file = file.replaceAll("\\\\\\n\\n", "");
or (it's the same):
file = file.replaceAll("\\\\\\n{2}", "");
\\\\ will result in \\ in the regex, so it matches \ and \\n will become \n and match the new line character.
And as mentioned by #Bohemian it would be better to fix the ini-file. Standards make everything easier. If you insist you could use your own file extension, because it is actually another format.
It is also possible to write a regular expression that directly extracts you the values:
file = file.replaceAll("\\\\\\n\\n", "");
Pattern pattern = Pattern.compile("^ *([a-zA-Z0-9_]+) *= *(.+?) *$");
Matcher matcher = pattern.matcher(file);
while (matcher.find()) {
System.out.println(matcher.group(1)); // left side of = (already trimmed)
System.out.println(matcher.group(2)); // right side of = (already trimmed)
}
It's easier than reading lines one by one, but performance could be worse. Anyway usually this is not an issue because ini files tend to be small.

parsing strings with or without regex

I am trying create a basic file system to imitate the terminal. I am currently stuck on getting the names after the command. My first thought was to use regex to parse the commands.
Examples of commands would be:
mkdir hello
ls
cd hello
However to account for many whitespaces an input could be mkdir hello. I was wondering if there is another way without using regex? Also I was curious to which method is faster? With or without regex?
You could try splitting the lines like
String[] tokens = line.split(" ");
And for basic commands, most likely your command will be at tokens[0] followed by arguments.
for(String current: line.split("\\s+"){
//do something.
}
Usually, regex is faster because you can compile it.
see java.util.regex - importance of Pattern.compile()?
(Internally, I think the JVM always compile the regex at some point, but if you do it explicitly, you can reuse it. I am not sure if the JVM is smart enough to reuse a compiled regex locally, maybe it is)

split csv according to field in Java

I want to split a csv file according to the last "field".
For instance the csv file contains:
a,1
b,2
c,3
d,1
The numbers indicate categories.
This file should be split into seperate files according to the numbers (resp. categories) so that there exist three files.
first file:
a,1
d,1
second file:
b,2
third file:
c,3
The greedy method would be to read the csv per line, split the string at "," and seperate the last element (here the number). Afterwards I could check the number of the current line and put it into a FileWriter.
But: I do not know how many categories there will be as I want to keep the system extensible. Therefore the number of needed FileWriters is unknown.
As an alternative I could read the complete csv file for each category. In the first iteration only lines of category "1" would be processed and written into "1.csv", in the second step only lines of category "2" go into "2.csv" and so on.
But: This means the file has to be read as many times as categories exist which could be quite often.
Do you know whether there is an elegant solution for this purpose?
I also appreciate linux-based solutions! Maybe it is not necessary to create a Java program?
I guess that awk could be the tool of choice?
Thanks for your help!
Try this awk one-liner:
awk -F, '{print >> "output"$NF".csv"}' input.csv
It will read each line and write it to the appropriate output csv file, based on the value of the last field of the line.
I would make a more generic way. In this case I don't need to know all the items in the second column, so this is automatic:
total.csv:
a,1
b,2
c,3
d,1
script.sh:
#!/bin/bash
for line in $(cat total.csv)
do
filename=$(echo $line | awk -F "," '{print $2}')
echo $line >> $filename.csv
done
outputs: 1.csv 2.csv 3.csv

How to replace a single line in .sh with Java?

I have a .sh script with a property=value. Let it be:
some_property="some value"
The value of the property is used along the script and script is launched in Java code. I want to dynamically change this property's value. I tried to use replaceFirst() method, but I don't know the actual value of "some_property" to replace it correctly using regexp.
How can I edit a .sh file with replacing a single line that starts with "some_property=" by some_property=my_value? By the way there're several places in a file where pattern "some_property=" can be met, so I need to change the first occurrence.
You should be fine with a regex:
line.replaceFirst("some_property=.*$", "some_property=\"" + your_value + "\"");
You can get position of the = and \n using String.indexOf(int) and then replace the string between = and \n using, for example, replace(CharSequence, CharSequence).
Btw - some_property=(.+) (and replacing $1) wouldn't be okay? :)

Categories