sed one Liner option -e


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed one Liner option -e
# 1  
Old 04-27-2015
HP sed one Liner option -e

Hi,

I have the following command.(Delete all trailing blank lines at the end of a file.)

Code:
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'

I don't understand the logic of this command and also I don't understand why -e is used.
Can you please let me know the logic of this command and why three -e are used


Famous Sed One-Liners Explained, Part III: Selective Deletion of Certain Lines and Special Applications - good coders code, great reuse
here you can find the basic explanation for this command which i am not able to understand (82nd question)

Thank You

Last edited by TomG; 04-27-2015 at 04:03 AM..
# 2  
Old 04-27-2015
Hi,
This command does nothing because no line begin with "\n" when sed read a line.

Regards.
# 3  
Old 04-27-2015
Quote:
Originally Posted by disedorgue
Hi,
This command does nothing because no line begin with "\n" when sed read a line.

Regards.
This command is actually removing blank lines from the end of my file.I have tested it.Its true that this line is not reading any \n from the file and appeding to the pattern space, but N in this command is causing \n to be appended to the pattern space. I am not sure how this command is removing all blank lines from the end of file..SmilieSmilie
# 4  
Old 04-27-2015
That one liner actually does what it is said to do. The -es are not needed (at least in my linux mawk). Try printing the pattern space with the l command to see how it proceeds. If it finds only zero or more <NL> (\n) chars in the pattern space which is one or more empty lines, it apends the next line. If that is empty as well, repeat. If it's the last line, quit. Any other line, print the ensemble:
Code:
sed 'l; :a /^\n*$/{$d;N;l;ba}' file
foo$
foo
$
\n$
\n\nbar$


bar
baz$
baz
$
\n$
\n\n$
\n\n\n$
\n\n\n\n$

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-27-2015
Quote:
Originally Posted by RudiC
That one liner actually does what it is said to do. The -es are not needed (at least in my linux mawk). Try printing the pattern space with the l command to see how it proceeds. If it finds only zero or more <NL> (\n) chars in the pattern space which is one or more empty lines, it apends the next line. If that is empty as well, repeat. If it's the last line, quit. Any other line, print the ensemble:
Code:
sed 'l; :a /^\n*$/{$d;N;l;ba}' file
foo$
foo
$
\n$
\n\nbar$


bar
baz$
baz
$
\n$
\n\n$
\n\n\n$
\n\n\n\n$


I understood the logic..its was a great debugging tip to get the content of Pattern space. That was a new learning for me . But for some reason this command was not working in HP-UX without -e

Thank you..
# 6  
Old 04-27-2015
Quote:
Originally Posted by RudiC
.
.
.
If it's the last line, quit.
.
.
.
I think this needs to be slightly modified: If it's the last line, delete the pattern space, print that (empty space) and quit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk or sed one liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: XXXX AAA234 XXXX BBB678 XXXX CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? XXXX AAA234... (7 Replies)
Discussion started by: jimmyf
7 Replies

2. UNIX for Dummies Questions & Answers

sed one-liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: AAA234 BBB678 CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? AAA234 Thing1 BBB678 Thing2... (5 Replies)
Discussion started by: jimmyf
5 Replies

3. UNIX for Advanced & Expert Users

sed one liner simialr to tail command

Can anyone explain the below sed oneliner? sed -e ':a' -e '$q;N;11,$D;ba' It works same as tail command. I just want to know how it works. Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

4. UNIX for Advanced & Expert Users

Please explain this sed one liner

Can anyone explain the below sed oneliner? sed -e ':a' -e '$q;N;11,$D;ba' It works same as tail command. I just want to know how it works. Thanks ---------- Post updated at 11:42 PM ---------- Previous update was at 11:37 PM ---------- Moderators, Can you please delete this thread?... (0 Replies)
Discussion started by: pandeesh
0 Replies

5. Shell Programming and Scripting

help with sed one liner

hey everyone, I want to remove some characters from a string that i have with sed. For example if my string is: a0=bus a1=car a2=truck I want my output to look like this: bus car truck So i want to delete the two characters before the = and including the =. This is what i came up with... (3 Replies)
Discussion started by: GmGeubt
3 Replies

6. Shell Programming and Scripting

SED | Awk flat file one liner

sed awk one liner (2 Replies)
Discussion started by: jap2614
2 Replies

7. Shell Programming and Scripting

Clarification needed for a SED one liner

I want to use SED to replace all new line characters of a file, I googled and found this one liner sed '{:q;N;s/\n//g;t q}' infile what do :q;N; and t q mean in this script? (6 Replies)
Discussion started by: kevintse
6 Replies

8. Shell Programming and Scripting

Sed one-liner to print specific lines?

I need to print specific lines from a file, say 2-5, 8, 12-15, 17, 19, 21-27. How do I achieve this? (2 Replies)
Discussion started by: Ilja
2 Replies

9. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies

10. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies
Login or Register to Ask a Question