pass parameter to SED

My script(ksh) works fine for
---------------------------------------------------

sed -n '28,31p' ${l_name} >> ${LOG_DIR}/Email.txt

---------------------------------------------------
But I wand to pass parrmeter to this syntax

I did the following things
---------------------------------------------------

 grep -n '^bad' ${l_name}|read lstart 
 grep -n '^Statistics' ${l_name}|read lend

---------------------------------------------------

lstart will give me 28 and lend will 31 . Now How I will pass it sed.
below code doesnt work
---------------------------------------------------

sed -n '$lstart,${lend}p' ${l_name} >> ${LOG_DIR}/Email.txt

---------------------------------------------------
Pease suggest me .

Try it with double quotes(") so that the shell will expand the variable:

sed -n "${lstart},${lend}p" ${l_name} >> ${LOG_DIR}/Email.txt

Single quotes prevent parameter expansion. Use double quotes instead.
--
Bye

What are you actually trying to accomplish with the 2 grep commands? There might be better ways.

Double quote also didnt work :frowning:

There is a log file i want to capture the portion between line 28 and 31 .

f_email()
{
  message "Sending  Bad record details  through Email "
  
  f_name=`ls -ltr ${BAD_FILE1} 2>/dev/null|awk '{print $9}'`
  l_name=`ls -ltr ${LOG_FILE1} 2>/dev/null|awk '{print $9}'`

  grep -n '^bad' ${l_name}|read lstart 
  grep -n '^Statistics' ${l_name}|read lend

echo "Please find the below reason for the rejection . To check the bad data plase find the attachment\n" > ${LOG_DIR}/Email.txt

#sed -n '28,31p' ${l_name} >> ${LOG_DIR}/Email.txt
sed -n "${lstart},${lend}p" ${l_name} >> ${LOG_DIR}/Email.txt

if [[ -s $f_name ]] then
    (cat ${LOG_DIR}/Email.txt;uuencode $f_name $f_name)|mailx -s "MFP SIZE Exceed report error" BalB@dressbarn.com
fi
}
head -31 sample.txt | tail -4

will give you the four lines 28-29-30-31 from your file.

joeyg,
how to pass this 31 and 4 dynamically . these nos are not fix .
it may be 26 and 10

After this:

  grep -n '^bad' ${l_name}|read lstart 
  grep -n '^Statistics' ${l_name}|read lend

The two variables will be empty in most shells.
Try:

lstart=$(grep -n '^bad' "${l_name}")
etc.

instead...

Alternatively try:

sed -n '/^bad/,/^Statistics/p' "$l_name"

I might miss something but to me the whole construction looks wrong from the start. Try the following, which should do the same in one pass what you try to achieve in 3 commands with 2 pipelines:

sed -n '/^bad/,$ {;p;/^Statistics/q;}'

It prints nothing at first. Until encountering the first line starting with "bad" it starts printing the lines to the end - but the first line starting with "Statistics" will quit sed, so no more lines get printed. It prints therefore only the lines in between these two lines. (To be precise: the first occcurrence of each.)

I hope this helps.

bakunin

Thanks a lot Scrutinizer . it works. :b:
below code works fine . One more question why it it not working in command line.

appworx ds prd > pwd
/app/oretail/rdw/12/ds/prd/log
appworx ds prd > sed -n '/^bad/,/^Statistics/p'  SRC_DRS_ADDL_SLS_INFO_LM_DM.20121008.log
appworx ds prd >


f_email()
{
  message "Sending  Bad record details  through Email "
  
  f_name=`ls -ltr ${BAD_FILE1} 2>/dev/null|awk '{print $9}'`
  l_name=`ls -ltr ${LOG_FILE1} 2>/dev/null|awk '{print $9}'`

 echo "  --Please find the below reason for the rejection . To check the bad data plase find the attachment\n" > ${LOG_DIR}/Email.txt
 sed -n '/^bad/,/^Statistics/p' "$l_name" >> ${LOG_DIR}/Email.txt

if [[ -s $f_name ]] then
    (cat ${LOG_DIR}/Email.txt;uuencode $f_name $f_name)|mailx -s "MFP SIZE Exceed report error" BalB@dressbarn.com
fi
}

> redirects into files, not other commands. You probably wanted | sed, not > sed.

I think "appworx ds prd >" is the prompt, no?

didnt work

$sed -n '/^bad/,/^Statistics/p'  SRC_DRS_ADDL_SLS_INFO_LM_DM.20121008.log > a.txt 

"appworx ds prd >" is the prompt .

Try it this way:

cat SRC_DRS_ADDL_SLS_INFO_LM_DM.20121008.log | sed -n '/^bad/,/^Statistics/p' >a.txt

Thanks spacebar :b:

My problem is fixed now . Thank you all . :slight_smile: