[SOLVED] Get strings distributed along up to 3 lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Having the following text, I want to extract 2 patterns and print related patterns in the same line:
Code:
pattern1: bc[0-9]d
pattern2: jk[0-9]lmnopqrs
How to know if they are related? Pattern1 always happens, but pattern2 not always. Then, if pattern1 is found and
the next pattern found is pattern2, then they are related and should be printed in the same line. If 2 consecutive
patterns1 are found (in 2 or 3 lines or in the same line), it means that for the previous pattern1 there is no pattern2.
I don't now if with awk is possible because the problem is that awk
reads line by line and as you can see, the patterns could begin in one line
and ends in the next one. And even begin in one line and ends 2 lines below.
The goal is know how to do it for this sample file and then, extend it for
a big file.
#include <unistd.h>
#define BUFFER_SIZE 2000
char buffer[BUFFER_SIZE];
int main (void) {
int count;
while ((count = read(0, buffer, BUFFER_SIZE))) {
int i, j;
for (i = 0, j = 0; i < count; ++i) {
if (buffer[i] == '\n') {
if (i > j) {
write(1, buffer + j, i - j);
}
j = i + 1;
}
}
if (i > j) {
write(1, buffer + j, i - j);
}
}
}
I'll try asap your code. The original input file is a dump from a binary file got with xxd command and produces a file of 4GB with 256 characters per line.
Do you I could use the same code with this large file?
Or there is a way to use the regex for the patterns to read directly from binary?
I would try to use the string bc as line separator (instead of newline)
next remove all the newlines
finally print matching lines using regexp like
^[0-9]d.*jk[0-9]lmnopqrs
May you help me please in how to it in awk or perl.
The thing is as explained in first post, I need 2 patterns. Pattern1 always happens
And patter2 not always, but both could be in more than one or two lines
With an input of 128 bytes per line (xxd used to dump).
Sure I've tried the codes of all of you, but when I try to replicate in a real file with grep or awk,
it seems the regex is not working for pattern-2. I want to extract these patterns:
For pattern1 the regex I'm using is working, but for the pattern 2 is taken more characters that
I want.
Regex used for pattern-1: ff77.{6,18}532064.{10}814.{13} --> it works Regex for pattern-2: 059.{32,34}.*940e.\{28\} --> Is taken character belonging to more than one pattern2.
Always, after the end of pattern-2 it follows 9506.
The regex for pattern-2 I have now is taken all characters in red.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.