ed command: Delete from line 1 until the first blank lineWhy does my shell script choke on whitespace or other special characters?What are the shell's control and redirection operators?Security implications of forgetting to quote a variable in bash/POSIX shellsHow to switch from input mode into command mode in ed ?How to use 'itextESC' when using ex as a text editor in command line?Insert text N lines before the last lineed invalid address macOSproblem understaning /.*/How shall I understand the different outputs by `-A` and by `-e` of diff3?how to remove the empty/blank lines from files that appears as @ from vi
CSV how to trim values to 2 places in multiple columns using UNIX
How to ensure color fidelity of the same file on two computers?
Active low-pass filters --- good to what frequencies?
Artificer Creativity
LuaLaTex - how to use number, computed later in the document
Check if three arrays contains the same element
Should I ask for an extra raise?
Has there been a multiethnic Star Trek character?
Generate basis elements of the Steenrod algebra
A map of non-pathological topology?
How to handle (one's own) self-harm scars (on the arm), in a work environment?
New pedal fell off maybe 50 miles after installation. Should I replace the entire crank, just the arm, or repair the thread?
Print lines between start & end pattern, but if end pattern does not exist, don't print
Which languages would be most useful in Europe at the end of the 19th century?
Cascading Switches. Will it affect performance?
Are there any important biographies of nobodies?
How to communicate to my GM that not being allowed to use stealth isn't fun for me?
GroupBy operation using an entire dataframe to group values
Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?
A word that means "blending into a community too much"
Thread Pool C++ Implementation
Why was this person allowed to become Grand Maester?
Does the Long March-11 increase its thrust after clearing the launch tower?
Is it a bad idea to to run 24 tap and shock lands in standard
ed command: Delete from line 1 until the first blank line
Why does my shell script choke on whitespace or other special characters?What are the shell's control and redirection operators?Security implications of forgetting to quote a variable in bash/POSIX shellsHow to switch from input mode into command mode in ed ?How to use 'itextESC' when using ex as a text editor in command line?Insert text N lines before the last lineed invalid address macOSproblem understaning /.*/How shall I understand the different outputs by `-A` and by `-e` of diff3?how to remove the empty/blank lines from files that appears as @ from vi
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I came across this code from a textbook; the book only says that 1,/^[ ]*$/d
means “Delete from line 1 until the first blank line.” What does each character specifically mean?
ed $1 << EOF
1,/^[ ]*$/d
w
q
EOF
ed
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I came across this code from a textbook; the book only says that 1,/^[ ]*$/d
means “Delete from line 1 until the first blank line.” What does each character specifically mean?
ed $1 << EOF
1,/^[ ]*$/d
w
q
EOF
ed
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I came across this code from a textbook; the book only says that 1,/^[ ]*$/d
means “Delete from line 1 until the first blank line.” What does each character specifically mean?
ed $1 << EOF
1,/^[ ]*$/d
w
q
EOF
ed
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I came across this code from a textbook; the book only says that 1,/^[ ]*$/d
means “Delete from line 1 until the first blank line.” What does each character specifically mean?
ed $1 << EOF
1,/^[ ]*$/d
w
q
EOF
ed
ed
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
Jeff Schaller♦
46.4k1166150
46.4k1166150
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
Jack ChenJack Chen
334
334
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jack Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
1,/.../ means the range from the 1st line to a line matching the pattern between the /.
/^[ ]*$/ matches a line that contains 0 or any number of spaces [ ]* from the beginning ^ to the end $ of the line. (You could also have a space and a tab character between the brackets.)
d is the command to delete the line
w write the file
q quit the editor
Those commands are sent to ed via a here document, indicated by the << EOF. The EOF string is a semi-arbitrary name that is paired to the other EOF four lines down. Leaving the EOF unquoted means that any variables in the intervening lines will be expanded.
The other thing to note here is the example's unquoted $1. The ed command/script will execute against the first positional parameter (argument to the script or function) subject to further whitespace splitting and filename generation. The argument really should be quoted. For further reading, see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells.
Assuming the[ ]does contain a space and a tab, then it could be replaced by[[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching[[:blank:]](or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and[[:blank:]], or whether it was just my mind making that connection.
– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do1,/^ *$/d.
– G-Man
1 min ago
add a comment |
Command and input
The $1 is the file name to be edited and everything between the EOFs are commands to ed.
Blow by blow description of 1,/^[ ]*$/d
1,effect line1see note below/indicates we are about to search for a string^indicates we want to match the start of the line in the file[indicates we are about to specify many characters- '
' we want to match a space - normally there would be more characters here ]finished specifying characters*we want to match 0 or more spaces (or whatever characters between[])$until the end of the line in the file/closing the searchddelete the line
Then the next line w writes the changes, and q quits ed.
Effect
If the file's ($1) line one is empty or has only spaces (no tabs) then it will be removed.
Notes
NOTE: g can be used to explicitly 'grep' the line, ie. 1,g/^[ ]*$/d. It is the same as 1,/^[ ]*$/d in this case since we are matching from the start of line one until the first occurrence of the end-of-line ($). The g becomes very useful when specifying multiple lines, eg. 1,3g/^[ ]*$/d. However, since ed is a line editor, before issuing a command like 1,3g/^[ ]*$/d you need to skip to the last line specified, in this case 3. The ed command 3 will skip to line 3 and all lines will be in the buffer.
(1) No,1,g/^[ ]*$/dis not the same as1,/^[ ]*$/d.1,g/^[ ]*$/dis the same as1g/^[ ]*$/d(i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”
– G-Man
9 mins ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Jack Chen is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523324%2fed-command-delete-from-line-1-until-the-first-blank-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
1,/.../ means the range from the 1st line to a line matching the pattern between the /.
/^[ ]*$/ matches a line that contains 0 or any number of spaces [ ]* from the beginning ^ to the end $ of the line. (You could also have a space and a tab character between the brackets.)
d is the command to delete the line
w write the file
q quit the editor
Those commands are sent to ed via a here document, indicated by the << EOF. The EOF string is a semi-arbitrary name that is paired to the other EOF four lines down. Leaving the EOF unquoted means that any variables in the intervening lines will be expanded.
The other thing to note here is the example's unquoted $1. The ed command/script will execute against the first positional parameter (argument to the script or function) subject to further whitespace splitting and filename generation. The argument really should be quoted. For further reading, see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells.
Assuming the[ ]does contain a space and a tab, then it could be replaced by[[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching[[:blank:]](or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and[[:blank:]], or whether it was just my mind making that connection.
– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do1,/^ *$/d.
– G-Man
1 min ago
add a comment |
1,/.../ means the range from the 1st line to a line matching the pattern between the /.
/^[ ]*$/ matches a line that contains 0 or any number of spaces [ ]* from the beginning ^ to the end $ of the line. (You could also have a space and a tab character between the brackets.)
d is the command to delete the line
w write the file
q quit the editor
Those commands are sent to ed via a here document, indicated by the << EOF. The EOF string is a semi-arbitrary name that is paired to the other EOF four lines down. Leaving the EOF unquoted means that any variables in the intervening lines will be expanded.
The other thing to note here is the example's unquoted $1. The ed command/script will execute against the first positional parameter (argument to the script or function) subject to further whitespace splitting and filename generation. The argument really should be quoted. For further reading, see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells.
Assuming the[ ]does contain a space and a tab, then it could be replaced by[[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching[[:blank:]](or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and[[:blank:]], or whether it was just my mind making that connection.
– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do1,/^ *$/d.
– G-Man
1 min ago
add a comment |
1,/.../ means the range from the 1st line to a line matching the pattern between the /.
/^[ ]*$/ matches a line that contains 0 or any number of spaces [ ]* from the beginning ^ to the end $ of the line. (You could also have a space and a tab character between the brackets.)
d is the command to delete the line
w write the file
q quit the editor
Those commands are sent to ed via a here document, indicated by the << EOF. The EOF string is a semi-arbitrary name that is paired to the other EOF four lines down. Leaving the EOF unquoted means that any variables in the intervening lines will be expanded.
The other thing to note here is the example's unquoted $1. The ed command/script will execute against the first positional parameter (argument to the script or function) subject to further whitespace splitting and filename generation. The argument really should be quoted. For further reading, see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells.
1,/.../ means the range from the 1st line to a line matching the pattern between the /.
/^[ ]*$/ matches a line that contains 0 or any number of spaces [ ]* from the beginning ^ to the end $ of the line. (You could also have a space and a tab character between the brackets.)
d is the command to delete the line
w write the file
q quit the editor
Those commands are sent to ed via a here document, indicated by the << EOF. The EOF string is a semi-arbitrary name that is paired to the other EOF four lines down. Leaving the EOF unquoted means that any variables in the intervening lines will be expanded.
The other thing to note here is the example's unquoted $1. The ed command/script will execute against the first positional parameter (argument to the script or function) subject to further whitespace splitting and filename generation. The argument really should be quoted. For further reading, see Why does my shell script choke on whitespace or other special characters? and Security implications of forgetting to quote a variable in bash/POSIX shells.
edited 8 hours ago
Jeff Schaller♦
46.4k1166150
46.4k1166150
answered 9 hours ago
BodoBodo
2,581618
2,581618
Assuming the[ ]does contain a space and a tab, then it could be replaced by[[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching[[:blank:]](or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and[[:blank:]], or whether it was just my mind making that connection.
– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do1,/^ *$/d.
– G-Man
1 min ago
add a comment |
Assuming the[ ]does contain a space and a tab, then it could be replaced by[[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching[[:blank:]](or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and[[:blank:]], or whether it was just my mind making that connection.
– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do1,/^ *$/d.
– G-Man
1 min ago
Assuming the
[ ] does contain a space and a tab, then it could be replaced by [[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching [[:blank:]] (or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and [[:blank:]], or whether it was just my mind making that connection.– Kusalananda♦
8 hours ago
Assuming the
[ ] does contain a space and a tab, then it could be replaced by [[:blank:]], which would allow "blank" characters. The line is, in a sense "blank" if it contains only characters matching [[:blank:]] (or contains nothing). I'm not certain if this was tho textbook's intention, i.e. the connection between its "blank line" and [[:blank:]], or whether it was just my mind making that connection.– Kusalananda♦
8 hours ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do
1,/^ *$/d.– G-Man
1 min ago
And, if the brackets contain only a space (rather than a space and a tab), then they (the brackets) can be omitted: just do
1,/^ *$/d.– G-Man
1 min ago
add a comment |
Command and input
The $1 is the file name to be edited and everything between the EOFs are commands to ed.
Blow by blow description of 1,/^[ ]*$/d
1,effect line1see note below/indicates we are about to search for a string^indicates we want to match the start of the line in the file[indicates we are about to specify many characters- '
' we want to match a space - normally there would be more characters here ]finished specifying characters*we want to match 0 or more spaces (or whatever characters between[])$until the end of the line in the file/closing the searchddelete the line
Then the next line w writes the changes, and q quits ed.
Effect
If the file's ($1) line one is empty or has only spaces (no tabs) then it will be removed.
Notes
NOTE: g can be used to explicitly 'grep' the line, ie. 1,g/^[ ]*$/d. It is the same as 1,/^[ ]*$/d in this case since we are matching from the start of line one until the first occurrence of the end-of-line ($). The g becomes very useful when specifying multiple lines, eg. 1,3g/^[ ]*$/d. However, since ed is a line editor, before issuing a command like 1,3g/^[ ]*$/d you need to skip to the last line specified, in this case 3. The ed command 3 will skip to line 3 and all lines will be in the buffer.
(1) No,1,g/^[ ]*$/dis not the same as1,/^[ ]*$/d.1,g/^[ ]*$/dis the same as1g/^[ ]*$/d(i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”
– G-Man
9 mins ago
add a comment |
Command and input
The $1 is the file name to be edited and everything between the EOFs are commands to ed.
Blow by blow description of 1,/^[ ]*$/d
1,effect line1see note below/indicates we are about to search for a string^indicates we want to match the start of the line in the file[indicates we are about to specify many characters- '
' we want to match a space - normally there would be more characters here ]finished specifying characters*we want to match 0 or more spaces (or whatever characters between[])$until the end of the line in the file/closing the searchddelete the line
Then the next line w writes the changes, and q quits ed.
Effect
If the file's ($1) line one is empty or has only spaces (no tabs) then it will be removed.
Notes
NOTE: g can be used to explicitly 'grep' the line, ie. 1,g/^[ ]*$/d. It is the same as 1,/^[ ]*$/d in this case since we are matching from the start of line one until the first occurrence of the end-of-line ($). The g becomes very useful when specifying multiple lines, eg. 1,3g/^[ ]*$/d. However, since ed is a line editor, before issuing a command like 1,3g/^[ ]*$/d you need to skip to the last line specified, in this case 3. The ed command 3 will skip to line 3 and all lines will be in the buffer.
(1) No,1,g/^[ ]*$/dis not the same as1,/^[ ]*$/d.1,g/^[ ]*$/dis the same as1g/^[ ]*$/d(i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”
– G-Man
9 mins ago
add a comment |
Command and input
The $1 is the file name to be edited and everything between the EOFs are commands to ed.
Blow by blow description of 1,/^[ ]*$/d
1,effect line1see note below/indicates we are about to search for a string^indicates we want to match the start of the line in the file[indicates we are about to specify many characters- '
' we want to match a space - normally there would be more characters here ]finished specifying characters*we want to match 0 or more spaces (or whatever characters between[])$until the end of the line in the file/closing the searchddelete the line
Then the next line w writes the changes, and q quits ed.
Effect
If the file's ($1) line one is empty or has only spaces (no tabs) then it will be removed.
Notes
NOTE: g can be used to explicitly 'grep' the line, ie. 1,g/^[ ]*$/d. It is the same as 1,/^[ ]*$/d in this case since we are matching from the start of line one until the first occurrence of the end-of-line ($). The g becomes very useful when specifying multiple lines, eg. 1,3g/^[ ]*$/d. However, since ed is a line editor, before issuing a command like 1,3g/^[ ]*$/d you need to skip to the last line specified, in this case 3. The ed command 3 will skip to line 3 and all lines will be in the buffer.
Command and input
The $1 is the file name to be edited and everything between the EOFs are commands to ed.
Blow by blow description of 1,/^[ ]*$/d
1,effect line1see note below/indicates we are about to search for a string^indicates we want to match the start of the line in the file[indicates we are about to specify many characters- '
' we want to match a space - normally there would be more characters here ]finished specifying characters*we want to match 0 or more spaces (or whatever characters between[])$until the end of the line in the file/closing the searchddelete the line
Then the next line w writes the changes, and q quits ed.
Effect
If the file's ($1) line one is empty or has only spaces (no tabs) then it will be removed.
Notes
NOTE: g can be used to explicitly 'grep' the line, ie. 1,g/^[ ]*$/d. It is the same as 1,/^[ ]*$/d in this case since we are matching from the start of line one until the first occurrence of the end-of-line ($). The g becomes very useful when specifying multiple lines, eg. 1,3g/^[ ]*$/d. However, since ed is a line editor, before issuing a command like 1,3g/^[ ]*$/d you need to skip to the last line specified, in this case 3. The ed command 3 will skip to line 3 and all lines will be in the buffer.
edited 5 hours ago
answered 8 hours ago
Colin PearseColin Pearse
864
864
(1) No,1,g/^[ ]*$/dis not the same as1,/^[ ]*$/d.1,g/^[ ]*$/dis the same as1g/^[ ]*$/d(i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”
– G-Man
9 mins ago
add a comment |
(1) No,1,g/^[ ]*$/dis not the same as1,/^[ ]*$/d.1,g/^[ ]*$/dis the same as1g/^[ ]*$/d(i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”
– G-Man
9 mins ago
(1) No,
1,g/^[ ]*$/d is not the same as 1,/^[ ]*$/d. 1,g/^[ ]*$/d is the same as 1g/^[ ]*$/d (i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”– G-Man
9 mins ago
(1) No,
1,g/^[ ]*$/d is not the same as 1,/^[ ]*$/d. 1,g/^[ ]*$/d is the same as 1g/^[ ]*$/d (i.e., the comma doesn’t change anything); that command deletes the first line if it is “blank”, but no others. (2) What are you saying about “you need to skip to the last line specified?”– G-Man
9 mins ago
add a comment |
Jack Chen is a new contributor. Be nice, and check out our Code of Conduct.
Jack Chen is a new contributor. Be nice, and check out our Code of Conduct.
Jack Chen is a new contributor. Be nice, and check out our Code of Conduct.
Jack Chen is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523324%2fed-command-delete-from-line-1-until-the-first-blank-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown