Lazer
Lazer

Reputation: 94990

Extracting data from a file

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?

Upvotes: 4

Views: 1006

Answers (6)

SergioAraujo
SergioAraujo

Reputation: 11830

awk '{print $1": "$4}' RS="\n\n" results.txt

Upvotes: 0

rekha_sri
rekha_sri

Reputation: 2725

You can use the following way also.

sed -e 's/^{some data}$//g;/^$/d;' results.txt | sed '$!N;s/\n/: /'

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 343067

$ cat file
a.txt
{some
  blah
data}
success!!

b.txt
{some data}
success!!

c.txt
{some data}
error!!

$ awk 'BEGIN{ FS="[{}]|\n";RS=""}{gsub(/!!/,"",$NF);print $1":"$NF}' file
a.txt:success
b.txt:success
c.txt:error

Update:

$ awk -vRS= -vFS="\n" '{print $1":"$NF}' file
a.txt:success!!
b.txt:success!!
c.txt:error!!

Upvotes: 2

David Kanarek
David Kanarek

Reputation: 12613

cat results.txt | grep -E "(([a-z]\.txt)|((success)|(error)!!))" | tr -d '\n' | sed 's/!!/!!\n/' should do it. You might have to replace \n with a literal newline though.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799390

awk:

BEGIN {
  state=0
}

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

state==1 && /!!$/ {
  print filename ": " gensub(/!!$/, "", $0)
  state=0
  next
}

Upvotes: 3

Enrico Carlesso
Enrico Carlesso

Reputation: 6934

That works for me:

cat result.txt | xargs |sed 's/\ {[^}]*}/:/g' | sed 's/!! /\n/g'

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

Upvotes: 1

Related Questions