Bash: how do I know the NUM parameter in grep -A -B on the fly?

I am trying to analyze the results of my agent from a collection of 20 txt files here .

If you are wondering about the source information, go to my page , then what I'm doing here is just one step.

Basically, I would like to get only the result of my agent from a messy context, so I have this command for a single file:

cat run15.txt | grep -A 50 -E '^Agent Name: agent10479475' | grep -B 50 '^=='

This means: after matching the regular expression, continue moving forward by 50 lines, stop, and then match the line separator with "==", return by 50 lines, if possible (this will probably collide with the very first line).

This approach depends on the fact that the line counter with a hard-coded number 50 will be just fine to get exactly one line separator.

And this will not work if I do the following code:

cat run*.txt | grep -A 50 -E '^Agent Name: agent10479475' | grep -B 50 '^=='

The exit will be messy ...

My question is: how to make sure grep knows exactly when to stop moving forward and when to stop coming back?

Any suggestions or tips are greatly appreciated.

Edited: I was doing something difficult, again ... The bash guru offers several more powerful tools. Now I will definitely switch to sed or awk. This is my approach at the end, after testing and errors:

NUM=10479475 
for i in {1..20}; do cat "run$i.txt" | grep -A 50 -E "^Agent Name: agent_*$NUM"
| grep -B 50 '^==';done > myresults.txt
+3
source share
2 answers

You can use sedto achieve the desired result:

sed -s -n -e '/^Agent Name: agent10479475/,/^==/ p' run*.txt

sed , , , , , , .

+3

awk. , grep . , , , , Agent:, Agent:.

awk 'BEGIN{RS="==*"}
/Agent Name: agent10479475/{
  print "-->"$0
}' file

$ ./shell.sh
-->
Agent Name: agent10479475
Bank Balance: $4356
Buy bids placed: 87
Desired assets --
  ticket_10960: Quantity(0),  Desired Quantity(1)
  ticket_11162: Quantity(0),  Desired Quantity(2)
  ticket_11213: Quantity(0),  Desired Quantity(1)
  ticket_11334: Quantity(0),  Desired Quantity(1)
  ticket_11425: Quantity(0),  Desired Quantity(3)
  ticket_11510: Quantity(0),  Desired Quantity(1)
  ticket_11732: Quantity(0),  Desired Quantity(2)
  ticket_11843: Quantity(1),  Desired Quantity(1)
  ticket_11904: Quantity(0),  Desired Quantity(1)
  ticket_12035: Quantity(0),  Desired Quantity(3)
  ticket_12120: Quantity(0),  Desired Quantity(1)
  ticket_12332: Quantity(0),  Desired Quantity(2)
  ticket_12433: Quantity(0),  Desired Quantity(1)
  ticket_12564: Quantity(1),  Desired Quantity(1)
  ticket_12645: Quantity(0),  Desired Quantity(3)
  flight to melbourne: Quantity(8),  Desired Quantity(8)
  room: Quantity(32),  Desired Quantity(24)
Negative quantity assets --
  ticket_7401: Quantity(-14)
  ticket_22013: Quantity(-15)
  ticket_28401: Quantity(-14)
Remaining assets --
  ticket_6463: Quantity(1)
  ticket_9861: Quantity(1)
  ticket_13441: Quantity(1)
  ticket_20813: Quantity(1)
  ticket_26853: Quantity(1)

awk (RS) "====", Agent: Agent:. , , $0.

Perl script,

$line = s/^\s+[0-9]+//;

bids.txt. , ? , . , . cat + grep + cut Perl script awk

awk 'BEGIN{
  printf "Enter agent num: "
  getline num <"-"
  regex="Agent Name: agent"num
  RS="==*"
  FS="\n"
}
$0 ~ regex{
  for(i=1;i<=NF;i++){
    if($i~/Buy bids placed:/){
       m=split($i, bids,": ")
       print bids[2]
       avgbids[num]++
       total+=bids[2]
    }
  }
}
END{
  for(i in avgbids) print i, total/avgbids[i]
}' run*txt
+1

Source: https://habr.com/ru/post/1741372/