Find string in file and find the all records by string

Hello

I would like to get know how to do this:
I got a big file (about 1GB) and I need to find a string (for instance by grep )
and then find all records in this file based on a string.

Thanks for advice.

Martin

What is the record separator?
Does the string need to be in a specific position in order for the record to be "based" on it?
under normal circumstances grep $STRING $FILE will return each line containing the string, which may be all you need.

Is this a homework assignment?

How are the strings passed to your script?

What files are you trying to process?

What have you tried to solve this on your own?

I need to find string like ABCD

grep ABCD $FILE

This grep gives me some records where is another string for instance BABA
and I need to find all records by BABA on $FILE

---------- Post updated at 02:21 AM ---------- Previous update was at 02:17 AM ----------

Just first its easy to find ABCD using by GREP ABCD $FILE

But the next step I dont know exactly how to write this.

Hi Martin,

you can find multiple patterns using egrep
like

egrep 'string1|string2' $FILE 

thanks

venkat

I just know the first string1 what I would like to use, but the second string2 not.
So its must be stored somewhere and then use grep on string2

Hi

sorry if i misunderstood,

what are you going to store string2?

Thanks
venkat

what is the relationship between ABCD and BABA in the file, could you provide a data sample (not 1G, just an example of the desired string and the second desired string and how it is discovered?).

I repeat: Is this a homework assignment? Homework assignments need to be posted in the Homework & Coursework Forum and require that a questionnaire be filled out as specified in the rules for that forum.

When you grep for a string in a file, we generally expect to find that string (not a different string); but, obviously, grep will give you an entire line, not just the string you matched. If you show us the format of the file you are processing and show us a real-life example with a sample input file (shown in CODE tags), a string that you want to search for in that sample input file, explain what variables should be set as a result of searching that sample file, and show us what secondary search you want to perform based on those results; we might be able to understand what you're trying to do and suggest a way to approach the problem.

1 Like

As I see you dont know what I mean.

Log file consist of bunch of information about transactions.
Each transaction has session_id and by this session_id I can identify some important things.

But first of all I need to look up for instance information about login, so I use :

grep loginWithIdCode file_log.txt

I get record something like this:

0F3CE08D6E53C97CD9B54A862B493D57 DEBUG (JSONConverter): JSON Request: {"idCode":*****,"reqType":"loginWithIdCode"}

And string 0F3CE08D6E53C97CD9B54A862B493D57 means session_id and by this session_id I would like to get all records from log file.

It is much more clear ?

A clear, precise, and detailed specification "ab initio" including some meaningful and representative samples could have avoided a long discussion, as we can see again.

If the line with your search pattern is the first line to be found in the file, try

awk '/loginWithIdCode/ {PAT=$1} $0 ~ PAT' file

When I look up session_id I would like to get all records again from log file which consist session_id.

Is there something you need done that the code suggested by RudiC didn't do?