|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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.
Last edited by zaxxon; 07-30-2010 at 04:11 AM.. |
| Sponsored Links | |
|
|
|
#2
|
|||
|
|||
|
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
doneSo, 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
|
||||
|
||||
|
Try: Code:
sed ':a;N;s/\n$//;ta' infile or Code:
sed -e :a -e 'N;s/\n$//;ta' infile |
| Sponsored Links | ||
|
|
![]() |
| Tags |
| command, delete, sed |
| Thread Tools | Search this Thread |
| 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 |
|
|