Portable way to remove the first line from the pattern space (when multiple lines are present)
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
What's the portable way to delete the first line1 from the pattern space ?
With gnu sed
I can do
s/[^n]*n//
but as far as I know this (using n
in a bracket expression) is not portable.
Practical example: here, sed
prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed
it's simple:
sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile
1: Obviously this should be done without restarting the cycle of commands...
sed
add a comment |Â
up vote
3
down vote
favorite
What's the portable way to delete the first line1 from the pattern space ?
With gnu sed
I can do
s/[^n]*n//
but as far as I know this (using n
in a bracket expression) is not portable.
Practical example: here, sed
prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed
it's simple:
sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile
1: Obviously this should be done without restarting the cycle of commands...
sed
Indeed. POSIXsed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters.
,*
,[
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂ
â myrdd
2 days ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
What's the portable way to delete the first line1 from the pattern space ?
With gnu sed
I can do
s/[^n]*n//
but as far as I know this (using n
in a bracket expression) is not portable.
Practical example: here, sed
prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed
it's simple:
sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile
1: Obviously this should be done without restarting the cycle of commands...
sed
What's the portable way to delete the first line1 from the pattern space ?
With gnu sed
I can do
s/[^n]*n//
but as far as I know this (using n
in a bracket expression) is not portable.
Practical example: here, sed
prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed
it's simple:
sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile
1: Obviously this should be done without restarting the cycle of commands...
sed
sed
asked 2 days ago
don_crissti
47k15124154
47k15124154
Indeed. POSIXsed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters.
,*
,[
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂ
â myrdd
2 days ago
add a comment |Â
Indeed. POSIXsed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters.
,*
,[
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂ
â myrdd
2 days ago
Indeed. POSIX
sed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters .
, *
, [
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂâ myrdd
2 days ago
Indeed. POSIX
sed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters .
, *
, [
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂâ myrdd
2 days ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
One way to portably do this sort of a thing is as follows:
sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'
add a comment |Â
up vote
2
down vote
I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:
sed 'H
/===/h
$!d
//d
x
:rpt
s/^n//
t done
s/.//
t rpt
:done
'
The second test-branch, t rpt
, needs to be a t
rather than a b
in order to reset the internal flag that says an s
has suceeded since the last read. You don't need the , they are just to show the loop better.
add a comment |Â
up vote
1
down vote
Silly, but working:
sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'
What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.
If you don't want to use the hold buffer:
sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'
And no, I don't like that. If there is a way to avoid it, avoid it.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
One way to portably do this sort of a thing is as follows:
sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'
add a comment |Â
up vote
2
down vote
accepted
One way to portably do this sort of a thing is as follows:
sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
One way to portably do this sort of a thing is as follows:
sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'
One way to portably do this sort of a thing is as follows:
sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'
answered yesterday
Rakesh Sharma
55513
55513
add a comment |Â
add a comment |Â
up vote
2
down vote
I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:
sed 'H
/===/h
$!d
//d
x
:rpt
s/^n//
t done
s/.//
t rpt
:done
'
The second test-branch, t rpt
, needs to be a t
rather than a b
in order to reset the internal flag that says an s
has suceeded since the last read. You don't need the , they are just to show the loop better.
add a comment |Â
up vote
2
down vote
I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:
sed 'H
/===/h
$!d
//d
x
:rpt
s/^n//
t done
s/.//
t rpt
:done
'
The second test-branch, t rpt
, needs to be a t
rather than a b
in order to reset the internal flag that says an s
has suceeded since the last read. You don't need the , they are just to show the loop better.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:
sed 'H
/===/h
$!d
//d
x
:rpt
s/^n//
t done
s/.//
t rpt
:done
'
The second test-branch, t rpt
, needs to be a t
rather than a b
in order to reset the internal flag that says an s
has suceeded since the last read. You don't need the , they are just to show the loop better.
I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:
sed 'H
/===/h
$!d
//d
x
:rpt
s/^n//
t done
s/.//
t rpt
:done
'
The second test-branch, t rpt
, needs to be a t
rather than a b
in order to reset the internal flag that says an s
has suceeded since the last read. You don't need the , they are just to show the loop better.
edited 2 days ago
answered 2 days ago
meuh
29.8k11751
29.8k11751
add a comment |Â
add a comment |Â
up vote
1
down vote
Silly, but working:
sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'
What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.
If you don't want to use the hold buffer:
sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'
And no, I don't like that. If there is a way to avoid it, avoid it.
add a comment |Â
up vote
1
down vote
Silly, but working:
sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'
What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.
If you don't want to use the hold buffer:
sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'
And no, I don't like that. If there is a way to avoid it, avoid it.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Silly, but working:
sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'
What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.
If you don't want to use the hold buffer:
sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'
And no, I don't like that. If there is a way to avoid it, avoid it.
Silly, but working:
sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'
What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.
If you don't want to use the hold buffer:
sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'
And no, I don't like that. If there is a way to avoid it, avoid it.
edited 2 days ago
don_crissti
47k15124154
47k15124154
answered 2 days ago
Philippos
5,93211546
5,93211546
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468002%2fportable-way-to-remove-the-first-line-from-the-pattern-space-when-multiple-line%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Indeed. POSIX
sed
only supports BREs (Basic Regular Expressions), and in BREs âÂÂthe special characters.
,*
,[
, and `\` [â¦] shall lose their special meaning within a bracket expression.âÂÂâ myrdd
2 days ago