The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-17-2007
Registered User
 

Join Date: Apr 2007
Posts: 10
Sed Help !

Hi,

I have a command in one of my shell script as below.

sed -n "/^$a/,\$p" $home/abc.log

$a and $home are variables. Not sure about $p.

Can you please let me know what this command is exactly trying to do.

Thanks in advance
LM
Reply With Quote
Forum Sponsor
  #2  
Old 04-17-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Lijju,
The "sed" command: sed -n "/^$a/,\$p" $home/abc.log
Will replace all records in the file "$home/abc.log" that start with the value of the
variable "$a" by a three character string ",$p" (comma-dollar-p).
Because there is a backslash "\", the dollar sign "$" is treated literally, as opposed
to a prefix of a shell variable.
Reply With Quote
  #3  
Old 04-17-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by Shell_Life
Lijju,
The "sed" command: sed -n "/^$a/,\$p" $home/abc.log
Will replace all records in the file "$home/abc.log" that start with the value of the
variable "$a" by a three character string ",$p" (comma-dollar-p).
Because there is a backslash "\", the dollar sign "$" is treated literally, as opposed
to a prefix of a shell variable.
don't think it does replacement. there should be 's' modifier for replacement.
Reply With Quote
  #4  
Old 04-17-2007
Registered User
 

Join Date: Apr 2007
Posts: 10
Yes it is not replacing. It doing some other thing
Reply With Quote
  #5  
Old 04-17-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
You are right, ghostdog74 -- my eyes didn't catch this one.
Reply With Quote
  #6  
Old 04-17-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Quote:
Originally Posted by Shell_Life
Lijju,
The "sed" command: sed -n "/^$a/,\$p" $home/abc.log
Will replace all records in the file "$home/abc.log" that start with the value of the
variable "$a" by a three character string ",$p" (comma-dollar-p).
Because there is a backslash "\", the dollar sign "$" is treated literally, as opposed
to a prefix of a shell variable.
Shell_life,
I dont think so. It will print all lines of the file between the line in the file that begins with the value of $a and the "$" - Last line of the file.

Simply once the sed finds out the line which begins with the value of $a,it prints all lines from there to the end of the file.

Example:
Assuming the variable "a" is set to "the" ie a=the
Quote:
1) cat abc.log
The beauty
he is the
no the
the fdsf
The big boss

HTML Code:
sed -n "/^$a/,\$p" abc.log
OUTPUT:
the fdsf
The big boss
Quote:
2)cat abc1.log
the first linme
no matched line
good
here is the end of the file

Code:
sed -n "/^$a/,\$p" abc1.log
OUTPUT:
the first linme
no matched line
good
here is the end of the file
Please correct me of am wrong.

Thanks
Nagarajan Ganesan
Reply With Quote
  #7  
Old 04-17-2007
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,644
ennstate, you are correct.

The $ is escaped because of the use of double quotes. The shell would otherwise attempt to perform substitution.

assuming a=foo

Without variables, one would normall write this as:

Code:
sed '/^foo/,$p'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0