Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from a file

Tags:

grep

unix

sed

I have a file results.txt which is like:

a.txt
{some data}
success!!

b.txt
{some data}
success!!

c.txt
{some data}
error!!

I want to extract data from it. I want an output like:

a.txt: success
b.txt: success
c.txt: error

The problem is that the {some data} part can be arbitrarily long.

How can this be done?

like image 811
Lazer Avatar asked Feb 27 '23 12:02

Lazer


1 Answers

awk:

BEGIN {
  state=0
}

state==0 && /.txt$/ {
  filename=$0
  state=1
  next
}

state==1 && /!!$/ {
  print filename ": " gensub(/!!$/, "", $0)
  state=0
  next
}
like image 134
Ignacio Vazquez-Abrams Avatar answered Apr 27 '23 22:04

Ignacio Vazquez-Abrams