Sed to merge lines that are delimited with a token [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
This question already has an answer here:
Replace comma with newline following with just a command and space
8 answers
I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.
So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.
The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.
sed
marked as duplicate by ñÃÂsýù÷, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
2
down vote
favorite
This question already has an answer here:
Replace comma with newline following with just a command and space
8 answers
I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.
So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.
The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.
sed
marked as duplicate by ñÃÂsýù÷, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
Replace comma with newline following with just a command and space
8 answers
I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.
So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.
The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.
sed
This question already has an answer here:
Replace comma with newline following with just a command and space
8 answers
I'm trying to write a script that uses sed to process lines in a text file to generate sample documentation. I've got most of the script working but I'm stuck with one edge case. Consider the following file
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
The problem is that some, but not all, lines end in a special token (it happens to be a comma). The token indicates that the line should be concatenated with following one to produce one long line.
So in my example line-3-should-be-a-very-long, should be concatenated with line-3-continued to give me line-3-should-be-a-very-long, line-3-continued (I do want to keep the comma). There is no special action on line 2 even though it contains a comma which is NOT at the end of the line.
The rest of the processing is done by piping a few sed and grep commands together, so a sed solution would fit well.
This question already has an answer here:
Replace comma with newline following with just a command and space
8 answers
sed
edited Apr 20 at 15:01
asked Apr 20 at 13:18
Stormcloud
1134
1134
marked as duplicate by ñÃÂsýù÷, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ñÃÂsýù÷, Philippos, roaima, G-Man, Rui F Ribeiro Apr 21 at 17:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16
add a comment |Â
1
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16
1
1
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
If the blanks should be deleted:
$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(if you want a single space to remain between the lines, replace // in the code by / /)
If lines can be continued multiple times, as in
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
then,
$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
This last sed script explained with annotations:
/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
# implicit output of (possibly) modified line at end
add a comment |Â
up vote
0
down vote
sed '/,$/N;s/n[[:blank:]]+/ /' file
When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
If the blanks should be deleted:
$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(if you want a single space to remain between the lines, replace // in the code by / /)
If lines can be continued multiple times, as in
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
then,
$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
This last sed script explained with annotations:
/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
# implicit output of (possibly) modified line at end
add a comment |Â
up vote
5
down vote
accepted
$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
If the blanks should be deleted:
$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(if you want a single space to remain between the lines, replace // in the code by / /)
If lines can be continued multiple times, as in
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
then,
$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
This last sed script explained with annotations:
/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
# implicit output of (possibly) modified line at end
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
If the blanks should be deleted:
$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(if you want a single space to remain between the lines, replace // in the code by / /)
If lines can be continued multiple times, as in
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
then,
$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
This last sed script explained with annotations:
/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
# implicit output of (possibly) modified line at end
$ sed '/,$/N;s/n//;' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
If the blanks should be deleted:
$ sed '/,$/N;s/n[[:blank:]]*//;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(if you want a single space to remain between the lines, replace // in the code by / /)
If lines can be continued multiple times, as in
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
then,
$ sed '/,$/:loop;N;s/n[[:blank:]]*//;/,$/bloop;' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
This last sed script explained with annotations:
/,$/ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
# implicit output of (possibly) modified line at end
edited Apr 20 at 14:35
answered Apr 20 at 14:04
Kusalananda
102k13199315
102k13199315
add a comment |Â
add a comment |Â
up vote
0
down vote
sed '/,$/N;s/n[[:blank:]]+/ /' file
When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
add a comment |Â
up vote
0
down vote
sed '/,$/N;s/n[[:blank:]]+/ /' file
When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed '/,$/N;s/n[[:blank:]]+/ /' file
When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.
sed '/,$/N;s/n[[:blank:]]+/ /' file
When you see a line ending with a comma, read in the next line then replace the newline and leading whitespace of the next line with a single space.
answered Apr 20 at 13:37
glenn jackman
45.9k265100
45.9k265100
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
add a comment |Â
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
The OP mentioned that comma is just a special token that implies concatenation. The comma should be replaced as well
â Bruno9779
Apr 20 at 13:59
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
@Bruno9779 The OP says "I do want to keep the comma".
â Kusalananda
Apr 20 at 14:08
add a comment |Â
1
@ñÃÂsýù÷ Not a duplicate if the lines may contain other commas (not just at the end).
â Kusalananda
Apr 20 at 14:50
@Kusalananda is correct
â Stormcloud
Apr 20 at 14:57
@Kusalananda I didn't get your point. if a line contain other comma other than at the end then that line not required to join and as OP sample s/he wants to join every lines if previous line ends with comma. Am I missing something here?
â Ã±ÃÂsýù÷
Apr 20 at 15:00
@ñÃÂsýù÷ Sorry, I might have been a bit too quick on the trigger there. I made the comment because the top answer (and some others) of the proposed duplicate assumes that commas do not occur in the filename. Here, we have a generic "line", which means that there may be commas on the line in other positions than at the end. The top answer of the other question would not work in that case. I'm not going to push this point harder than this, so if more people feels that it is indeed a dupe, well, then it is a dupe.
â Kusalananda
Apr 20 at 15:16