sed break into lines (advanced)
I'm looking to use sed
(or combining it with another grep
command)to convert the following string
John: Hi!,How are you,?,Dylan: Hey,OK
into
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
If that's not possible I'm willing to compromise for
John: Hi!,How are you,?
Dylan: Hey,OK
Many thanks
bash awk sed command-line grep
add a comment |
I'm looking to use sed
(or combining it with another grep
command)to convert the following string
John: Hi!,How are you,?,Dylan: Hey,OK
into
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
If that's not possible I'm willing to compromise for
John: Hi!,How are you,?
Dylan: Hey,OK
Many thanks
bash awk sed command-line grep
1
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46
add a comment |
I'm looking to use sed
(or combining it with another grep
command)to convert the following string
John: Hi!,How are you,?,Dylan: Hey,OK
into
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
If that's not possible I'm willing to compromise for
John: Hi!,How are you,?
Dylan: Hey,OK
Many thanks
bash awk sed command-line grep
I'm looking to use sed
(or combining it with another grep
command)to convert the following string
John: Hi!,How are you,?,Dylan: Hey,OK
into
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
If that's not possible I'm willing to compromise for
John: Hi!,How are you,?
Dylan: Hey,OK
Many thanks
bash awk sed command-line grep
bash awk sed command-line grep
edited Nov 16 '18 at 10:18
RavinderSingh13
30.3k41639
30.3k41639
asked Nov 16 '18 at 8:30
roizcorproizcorp
263
263
1
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46
add a comment |
1
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46
1
1
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46
add a comment |
5 Answers
5
active
oldest
votes
sed
can be used.
There is a great Sed - An Introduction and Tutorial by Bruce Barnett online page, which I always open aside when writing sed
script.
Give a try to this:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n '
:1
/./ {
/^[^:,][^:,]*: / {
h
s/^([^:,][^,:]*: ).*$/1/
x
s/,/n/
P
D
}
x
/./ {
x
H
x
s/n//g
x
s/.//g
x
b 1
}
}'
The output is:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft usedD
to control the newline cycle.
– David C. Rankin
Nov 16 '18 at 10:31
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
From my point of view both/./ {
and the corresponding closing bracket}
can be omitted.
– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
add a comment |
In awk:
$ awk 'BEGIN{RS=","}{if($1~/:$/)p=$1;print ($1==p?"":p " ") $0}' file
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
There will be an extra empty line in the end. For some awks (at least GNU awk, mawk and Busybox awk) you can use RS="[,n]"
.
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
add a comment |
With tr
and awk
:
tr ',' 'n' <file | awk '/:/{name=$1; print; next}; {print name,$0}'
or shorter:
tr ',' 'n' <file | awk '/:/?name=$1:$0=name " " $0'
Output:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
add a comment |
In single awk
, considering that your Input_file is same as shown samples then following may help you here.
awk -F',' '
{
for(i=1;i<=NF;i++){
if($i~/:/){
if($i ~ /: /){
print $i
split($i,array," ")
val=array[1]
}
else{
val=$i
}
}
else{
print val,$i
}
}
}' Input_file
add a comment |
You may try to do something like this:
echo 'John: Hi!,How are you,?,Dylan: Hey,OK' | sed -E "s|(w+:)|n1|g"
It will return:
John: Hi!,How are you,?,
Dylan: Hey,OK
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53334047%2fsed-break-into-lines-advanced%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed
can be used.
There is a great Sed - An Introduction and Tutorial by Bruce Barnett online page, which I always open aside when writing sed
script.
Give a try to this:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n '
:1
/./ {
/^[^:,][^:,]*: / {
h
s/^([^:,][^,:]*: ).*$/1/
x
s/,/n/
P
D
}
x
/./ {
x
H
x
s/n//g
x
s/.//g
x
b 1
}
}'
The output is:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft usedD
to control the newline cycle.
– David C. Rankin
Nov 16 '18 at 10:31
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
From my point of view both/./ {
and the corresponding closing bracket}
can be omitted.
– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
add a comment |
sed
can be used.
There is a great Sed - An Introduction and Tutorial by Bruce Barnett online page, which I always open aside when writing sed
script.
Give a try to this:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n '
:1
/./ {
/^[^:,][^:,]*: / {
h
s/^([^:,][^,:]*: ).*$/1/
x
s/,/n/
P
D
}
x
/./ {
x
H
x
s/n//g
x
s/.//g
x
b 1
}
}'
The output is:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft usedD
to control the newline cycle.
– David C. Rankin
Nov 16 '18 at 10:31
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
From my point of view both/./ {
and the corresponding closing bracket}
can be omitted.
– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
add a comment |
sed
can be used.
There is a great Sed - An Introduction and Tutorial by Bruce Barnett online page, which I always open aside when writing sed
script.
Give a try to this:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n '
:1
/./ {
/^[^:,][^:,]*: / {
h
s/^([^:,][^,:]*: ).*$/1/
x
s/,/n/
P
D
}
x
/./ {
x
H
x
s/n//g
x
s/.//g
x
b 1
}
}'
The output is:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
sed
can be used.
There is a great Sed - An Introduction and Tutorial by Bruce Barnett online page, which I always open aside when writing sed
script.
Give a try to this:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n '
:1
/./ {
/^[^:,][^:,]*: / {
h
s/^([^:,][^,:]*: ).*$/1/
x
s/,/n/
P
D
}
x
/./ {
x
H
x
s/n//g
x
s/.//g
x
b 1
}
}'
The output is:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
answered Nov 16 '18 at 10:18
Jay jargotJay jargot
1,9821511
1,9821511
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft usedD
to control the newline cycle.
– David C. Rankin
Nov 16 '18 at 10:31
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
From my point of view both/./ {
and the corresponding closing bracket}
can be omitted.
– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
add a comment |
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft usedD
to control the newline cycle.
– David C. Rankin
Nov 16 '18 at 10:31
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
From my point of view both/./ {
and the corresponding closing bracket}
can be omitted.
– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
2
2
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft used
D
to control the newline cycle.– David C. Rankin
Nov 16 '18 at 10:31
Bravo! That is worth the UV just for the canny use of pattern and hold space and the lest oft used
D
to control the newline cycle.– David C. Rankin
Nov 16 '18 at 10:31
1
1
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
I am BLOWN away! Thank you so much!
– roizcorp
Nov 16 '18 at 12:06
2
2
From my point of view both
/./ {
and the corresponding closing bracket }
can be omitted.– Cyrus
Nov 16 '18 at 19:43
From my point of view both
/./ {
and the corresponding closing bracket }
can be omitted.– Cyrus
Nov 16 '18 at 19:43
@Cyrus This is true, this would give on one line:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
@Cyrus This is true, this would give on one line:
printf 'John: Hi!,How are you,?,Dylan: Hey,OKn' | sed -n ':1 ; /^[^:,][^:,]*: / { h ; s/^([^:,][^,:]*: ).*$/1/ ; x ; s/,/n/ ; P ; D } ; H ; x ; s/n//g ; x ; s/.//g ; x ; b 1'
– Jay jargot
Nov 19 '18 at 9:20
add a comment |
In awk:
$ awk 'BEGIN{RS=","}{if($1~/:$/)p=$1;print ($1==p?"":p " ") $0}' file
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
There will be an extra empty line in the end. For some awks (at least GNU awk, mawk and Busybox awk) you can use RS="[,n]"
.
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
add a comment |
In awk:
$ awk 'BEGIN{RS=","}{if($1~/:$/)p=$1;print ($1==p?"":p " ") $0}' file
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
There will be an extra empty line in the end. For some awks (at least GNU awk, mawk and Busybox awk) you can use RS="[,n]"
.
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
add a comment |
In awk:
$ awk 'BEGIN{RS=","}{if($1~/:$/)p=$1;print ($1==p?"":p " ") $0}' file
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
There will be an extra empty line in the end. For some awks (at least GNU awk, mawk and Busybox awk) you can use RS="[,n]"
.
In awk:
$ awk 'BEGIN{RS=","}{if($1~/:$/)p=$1;print ($1==p?"":p " ") $0}' file
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
There will be an extra empty line in the end. For some awks (at least GNU awk, mawk and Busybox awk) you can use RS="[,n]"
.
edited Nov 16 '18 at 10:59
answered Nov 16 '18 at 9:15
James BrownJames Brown
20.1k42037
20.1k42037
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
add a comment |
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
I made the sed answer as correct because this didn't worked in my android in terms it made an extra line for "John" as part of "Dylan" line i.e. "John: Dylan: Hey"
– roizcorp
Nov 16 '18 at 13:35
add a comment |
With tr
and awk
:
tr ',' 'n' <file | awk '/:/{name=$1; print; next}; {print name,$0}'
or shorter:
tr ',' 'n' <file | awk '/:/?name=$1:$0=name " " $0'
Output:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
add a comment |
With tr
and awk
:
tr ',' 'n' <file | awk '/:/{name=$1; print; next}; {print name,$0}'
or shorter:
tr ',' 'n' <file | awk '/:/?name=$1:$0=name " " $0'
Output:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
add a comment |
With tr
and awk
:
tr ',' 'n' <file | awk '/:/{name=$1; print; next}; {print name,$0}'
or shorter:
tr ',' 'n' <file | awk '/:/?name=$1:$0=name " " $0'
Output:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
With tr
and awk
:
tr ',' 'n' <file | awk '/:/{name=$1; print; next}; {print name,$0}'
or shorter:
tr ',' 'n' <file | awk '/:/?name=$1:$0=name " " $0'
Output:
John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK
edited Nov 16 '18 at 11:37
answered Nov 16 '18 at 8:58
CyrusCyrus
46.9k43880
46.9k43880
add a comment |
add a comment |
In single awk
, considering that your Input_file is same as shown samples then following may help you here.
awk -F',' '
{
for(i=1;i<=NF;i++){
if($i~/:/){
if($i ~ /: /){
print $i
split($i,array," ")
val=array[1]
}
else{
val=$i
}
}
else{
print val,$i
}
}
}' Input_file
add a comment |
In single awk
, considering that your Input_file is same as shown samples then following may help you here.
awk -F',' '
{
for(i=1;i<=NF;i++){
if($i~/:/){
if($i ~ /: /){
print $i
split($i,array," ")
val=array[1]
}
else{
val=$i
}
}
else{
print val,$i
}
}
}' Input_file
add a comment |
In single awk
, considering that your Input_file is same as shown samples then following may help you here.
awk -F',' '
{
for(i=1;i<=NF;i++){
if($i~/:/){
if($i ~ /: /){
print $i
split($i,array," ")
val=array[1]
}
else{
val=$i
}
}
else{
print val,$i
}
}
}' Input_file
In single awk
, considering that your Input_file is same as shown samples then following may help you here.
awk -F',' '
{
for(i=1;i<=NF;i++){
if($i~/:/){
if($i ~ /: /){
print $i
split($i,array," ")
val=array[1]
}
else{
val=$i
}
}
else{
print val,$i
}
}
}' Input_file
answered Nov 16 '18 at 10:13
RavinderSingh13RavinderSingh13
30.3k41639
30.3k41639
add a comment |
add a comment |
You may try to do something like this:
echo 'John: Hi!,How are you,?,Dylan: Hey,OK' | sed -E "s|(w+:)|n1|g"
It will return:
John: Hi!,How are you,?,
Dylan: Hey,OK
add a comment |
You may try to do something like this:
echo 'John: Hi!,How are you,?,Dylan: Hey,OK' | sed -E "s|(w+:)|n1|g"
It will return:
John: Hi!,How are you,?,
Dylan: Hey,OK
add a comment |
You may try to do something like this:
echo 'John: Hi!,How are you,?,Dylan: Hey,OK' | sed -E "s|(w+:)|n1|g"
It will return:
John: Hi!,How are you,?,
Dylan: Hey,OK
You may try to do something like this:
echo 'John: Hi!,How are you,?,Dylan: Hey,OK' | sed -E "s|(w+:)|n1|g"
It will return:
John: Hi!,How are you,?,
Dylan: Hey,OK
edited Nov 16 '18 at 8:50
answered Nov 16 '18 at 8:42
Vladimir KovpakVladimir Kovpak
11.2k43949
11.2k43949
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53334047%2fsed-break-into-lines-advanced%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
1
Please provide a minimalistic code demo of what you have tried.
– Daan
Nov 16 '18 at 8:33
I tried too many things pretty messed the entire thing until you guys came to the rescue
– roizcorp
Nov 16 '18 at 19:46