Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

pass parameter to SED

Shell Programming and Scripting


Tags
sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-09-2012
deep_kol deep_kol is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 10 October 2012, 3:00 PM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
pass parameter to SED

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

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

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

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

Code:
 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
---------------------------------------------------

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

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

Last edited by Scrutinizer; 10-09-2012 at 01:41 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 10-09-2012
spacebar's Unix or Linux Image
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 21 September 2016, 12:03 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 71 Times in 70 Posts
Try it with double quotes(") so that the shell will expand the variable:

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

Sponsored Links
    #3  
Old Unix and Linux 10-09-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 2 May 2016, 6:07 AM EDT
Location: Lombardia, Italy
Posts: 182
Thanks: 5
Thanked 38 Times in 38 Posts
Quote:
Originally Posted by deep_kol View Post
Code:
sed -n '$lstart,${lend}p' ${l_name} >> ${LOG_DIR}/Email.txt

Single quotes prevent parameter expansion. Use double quotes instead.
--
Bye
    #4  
Old Unix and Linux 10-09-2012
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 September 2016, 7:22 AM EDT
Location: India
Posts: 1,413
Thanks: 101
Thanked 497 Times in 474 Posts
What are you actually trying to accomplish with the 2 grep commands? There might be better ways.
Sponsored Links
    #5  
Old Unix and Linux 10-09-2012
deep_kol deep_kol is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 10 October 2012, 3:00 PM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Double quote also didnt work Linux

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

Code:
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
}

Sponsored Links
    #6  
Old Unix and Linux 10-09-2012
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 26 September 2016, 11:28 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,409
Thanks: 99
Thanked 177 Times in 163 Posts
Why not...


Code:
head -31 sample.txt | tail -4

will give you the four lines 28-29-30-31 from your file.
Sponsored Links
    #7  
Old Unix and Linux 10-09-2012
deep_kol deep_kol is offline
Registered User
 
Join Date: Jun 2007
Last Activity: 10 October 2012, 3:00 PM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
joeyg,
how to pass this 31 and 4 dynamically . these nos are not fix .
it may be 26 and 10
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass the parameter value to a... ? khestoi UNIX for Dummies Questions & Answers 5 02-15-2011 12:59 PM
How to pass parameter to subroutine ssuresh1999 UNIX for Dummies Questions & Answers 2 08-15-2008 05:04 PM
How to pass a parameter megh Shell Programming and Scripting 1 07-18-2008 02:39 AM
PASS parameter to AWK unisam UNIX for Dummies Questions & Answers 2 05-14-2004 09:51 AM



All times are GMT -4. The time now is 03:02 PM.