Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to get something like this

#Batch Job - AB1234
texts
texts
texts
texts
texts
#--------

I used

awk '/#Batch Job.*AB1234/,/#--.*/' filename

and i went well.

But then when I tried to parameterize the AB1234, using this script, I failed. Help?

BATCHJOBNAME=AB1234
awk -v batchjobname=$BATCHJOBNAME '
  /#Batch.*/ { f=1 ; m=0 ; res="" }
  f { res = res $0 ORS }
  f && /Job.*/ && index($2,batchjobname) > 0 { m=1 ;}
  /<#--.*>/ { f=0 ; if (m) print res ; }
 ' filename
share|improve this question
    
Have you try to remove spaces on variable assignment? BATCHJOBNAME=AB1234 –  Romeo Ninov Jun 9 at 7:48
    
Just a typo in transposition. I edited it now thanks @Romeo –  Philip Morris Jun 9 at 7:53
    
What about awk -v batchjobname=AB1234 '$0~batchjobname,/#--.*/' filename ? –  FloHimself Jun 9 at 7:55
    
I personally have problem to understand the logic and what you expect as result. Can you please clarify? –  Romeo Ninov Jun 9 at 7:55
    
@FloHimself it's printing right but Im having an error –  Philip Morris Jun 9 at 8:08

1 Answer 1

up vote 1 down vote accepted

Try this:

BATCH=AB1234
awk -v batch="#Batch Job.*${BATCH}" '($0 ~ batch),/#--.*/' filename
share|improve this answer
    
It's good when AB1234 is hardcoded but when I use $BATCHJOBNAME, it is failing. I am getting my parameters from a .properties file so I cannot hardcode it –  Philip Morris Jun 9 at 8:18
    
@PhilipMorris awk -v batchjobname="#Batch Job.*$BATCHJOBNAME" .... Just make sure to keep it a valid regexp. –  lcd047 Jun 9 at 8:22
    
I tried that. It failed –  Philip Morris Jun 9 at 8:22
    
@PhilipMorris Try understanding it first. Ok, I edited my answer. –  lcd047 Jun 9 at 8:23
    
I am having errors when I run it the second time. It is good when ran the first time. Why is that –  Philip Morris Jun 9 at 10:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.