The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exec explanation needed myle UNIX for Dummies Questions & Answers 3 04-05-2008 04:23 PM
SED command ---------help needed veerapureddy Shell Programming and Scripting 4 03-17-2008 02:12 PM
help needed in ls command anju Shell Programming and Scripting 1 02-18-2008 04:20 AM
Help needed for ps command in AIX moe2266 AIX 0 09-30-2005 08:43 AM
help needed for sed command manualvin UNIX for Dummies Questions & Answers 2 06-24-2004 08:41 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-26-2006
Registered User
 

Join Date: Sep 2006
Posts: 5
sed command explanation needed

Hi,

Could you please explain me the below statement -- phrase wise.

sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt


if suppose $cnt contains value: 10
it copies last 9 lines of abc.txt to xyz.txt

why it is copying last 9 rather than 10.
and also what is ba and $D over there in command.

reply me as early as possible.

Thanks,
Subbu
Reply With Quote
Forum Sponsor
  #2  
Old 09-26-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
A more lisible format of the command (with variable substitition made)
Code:
sed -e '
:a
$q
N
10,$D
ba
' abc.txt > xyz.txt
:a

(0):label Marks a branch point to be referenced by the b and t subcommands.
This label can be any sequence of eight or fewer bytes.

Defines label a

$q

(1)q Branches to the end of the script. It does not start a new cycle.

$ means last line of input.
If last line of input, print the line and stop processing.

N

(2)N Appends the next line of input to the pattern space with an embedded
new-line character (the current line number changes). You can use this to search for patterns that are split onto two lines.


10,$D

(2)D Deletes the initial segment of the pattern space through the first new-line character and then starts the next cycle.

From line 10 to end, removes the oldest line (added by N command) at the begining of the buffer.
So the buffer contains all time at most 9 lines.

ba

(2)b[label] Branches to the : command bearing the label variable. If the label variable is empty, it branches to the end of the script.

Branch to label a


The command is equivalant to :
Code:
tail -f 9 abc.txt > xyz.txt

Jean-Pierre.
Reply With Quote
  #3  
Old 09-26-2006
Registered User
 

Join Date: Sep 2006
Posts: 5
Hi Jean-Pierre,

Thank you very much for your quick response.

Is this correct way to copy all the lines of abc.txt, except the first line to xyz.txt using sed command as following....

cnt=`cat abc.txt | wc -l`
sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt

or, is there any effective way of doing this using sed command?
Pls, give me solution using sed command only.

Thanks,
Subbu.
Reply With Quote
  #4  
Old 09-26-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
The simplest manner, erases the first line and prints the others
Code:
sed -e '1d' abc.txt > xyz.txt
Why solution using sed command only ?


Jean-Pierre.
Reply With Quote
  #5  
Old 09-26-2006
Registered User
 

Join Date: Sep 2006
Posts: 5
Hi

Hi Jean-Pierre,

why I have asked you only sed command is .... I don't want to use tail command (it's truncating some part of data as it handles in only 20 k buffer). Let me know any other options available except tail.

Thank you very much,
Subbu.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:10 PM.


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