Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2010
gwgreen1's Avatar
Registered User
 

Join Date: Oct 2008
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
Exclamation SED delete only first line command - Help need

Can any one help me how the sed delete only first line command (D) works.

Code:
sed -f sedcmds File.txt

In this how the sed delete only first line command (D) works to delete the blank lines appearing more than ones leaving only one blank and deleting others blanks.
While doing step by step when two blank line appended in the pattern space, according to D command one blank is deleted in the pattern space leaving second one alone.
My question is what will happen to the remaining blank space, whether it will send as output immediately by clearing pattern space or append next pattern to that blank space???. After this how the next loop will start?
Can u explain this detailly... coz I couldn't able to find the detailed explanation for sed's D delete command in net.

sedcmds

Code:
/^$/{
       N
       /^\n$/D
       }

File.txt

Code:
This is line1

This is line2


this is line3




This is line4.

Moderator's Comments:
Having 14 posts in this forum, you should be familiar with using code tags.

Last edited by zaxxon; 07-30-2010 at 04:11 AM..
Sponsored Links
    #2  
Old 07-30-2010
agama agama is offline Forum Advisor  
Always Learning
 

Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,210
Thanks: 86
Thanked 405 Times in 389 Posts
The sed man page is pretty clear about the function of the D command which says that the D command will cause the next cycle to start, but will prevent the reading of the next input line if the pattern space is not empty.

Thus, the way your programme works can be explained with a bit of pseudo code:


Code:
while not at end of file 
do
   if pattern space is an empty line  (/^$/)
   then
       read next line and append to pattern space (N)
       if pattern space is two empty lines   (/^\n$)
       then
           delete to the first newline  (D)
           goto loop (leaves a single empty line in the pattern space)
       end
   end

   print pattern space
   read next line

:loop
done

So, as sed encounters blank lines, it 'buffers' one of them in the pattern space until the pattern space contains a blank line followed by a line that is more than just a newline.

Hope this makes sense.
Sponsored Links
    #3  
Old 07-31-2010
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Try:

Code:
sed ':a;N;s/\n$//;ta' infile

or

Code:
sed -e :a -e 'N;s/\n$//;ta' infile

Sponsored Links
Closed Thread

Tags
command, delete, sed

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
Command to delete numbers at beginning of txt file line li_bi UNIX for Dummies Questions & Answers 4 01-25-2010 02:18 PM
single line command to delete a 6 months old file wtolentino UNIX for Dummies Questions & Answers 6 07-29-2009 02:23 PM
how to delete RAID of solaris volume manager thru command line vr76413 UNIX for Advanced & Expert Users 1 12-13-2006 04:42 PM
delete contents from command line juanbro UNIX for Dummies Questions & Answers 4 12-23-2003 11:25 AM
Can't delete the user from command line spavlov UNIX for Advanced & Expert Users 4 09-23-2002 09:38 AM



All times are GMT -4. The time now is 04:02 AM.