Reputation: 94990
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
Reputation: 2725
You can use the following way also.
sed -e 's/^{some data}$//g;/^$/d;' results.txt | sed '$!N;s/\n/: /'
Upvotes: 2
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
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
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
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