pass parameter to SED | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

pass parameter to SED

Shell Programming and Scripting


Tags
sed

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 02:41 PM.. Reason: code tags
Sponsored Links
    #2  
Old 10-09-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 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 10-09-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 13 June 2014, 10:20 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 10-09-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
What are you actually trying to accomplish with the 2 grep commands? There might be better ways.
Sponsored Links
    #5  
Old 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

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 10-09-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 17 November 2014, 3:07 PM EST
Location: Out running a Marathon.
Posts: 2,337
Thanks: 73
Thanked 155 Times in 144 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 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 01:59 PM
How to pass parameter to subroutine ssuresh1999 UNIX for Dummies Questions & Answers 2 08-15-2008 06:04 PM
How to pass a parameter megh Shell Programming and Scripting 1 07-18-2008 03:39 AM
PASS parameter to AWK unisam UNIX for Dummies Questions & Answers 2 05-14-2004 10:51 AM



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