Perl multiline match for 'something' which starts without '#s'Perl Negative Lookbehind with variable length...
What does "tea juice" mean in this context?
What is the indigenous Russian word for a wild boar?
How should I push back against my job assigning "homework"?
Difference between antennas for wifi vs ham repeaters
If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?
Can a non-EU citizen travel within the Schengen area without identity documents?
Using PCA vs Linear Regression
How crucial is a waifu game storyline?
The deliberate use of misleading terminology
Why were the Night's Watch required to be celibate?
Is there an evolutionary advantage to having two heads?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
Biblical Basis for 400 years of silence between old and new testament
What are the slash markings on Gatwick's 08R/26L?
What is the difference between nullifying your vote and not going to vote at all?
California: "For quality assurance, this phone call is being recorded"
chmod would set file permission to 000 no matter what permission i try to set
The qvolume of an integer
If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?
Why the lack of hesitance to wear pads on the sabbath?
Is a hash a zero-knowledge proof?
Geometry affects line breaking
Why don't I have ground wiring on any of my outlets?
Different PCB color ( is it different material? )
Perl multiline match for 'something' which starts without '#s'
Perl Negative Lookbehind with variable length bypass maybe?Bash-Perl Make Substitution and Return array (string & number of replacements/substitutions)How to remove multiple blank lines from a file?How to match date in Perl — assuming UNIX regex is the same as Perl'smultiline editing using perl or any other toolGrep all string which do not starts with number(s)Match beginning of a line with something there?How to escape metacharacters for egrep like metaquote from Perl?Multiline Regexp (grep, sed, awk, perl)How do you match a line which starts with a special character using grep?Unix perl match string and delete lineComplex regex sed replacement not working but not throwing errors
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
UPDATE:
- The REAL answer to this MULTILINE question was given here by Stephane https://unix.stackexchange.com/a/521560/354415
- Alternatively go line by line with perl by Terdon below: https://unix.stackexchange.com/a/521512/354415
- Alternatively go line by line with IFS by myself here: https://unix.stackexchange.com/a/521550/354415
Here is my new toy:
Problem is that I want it to match only on lines which do not have the #s* in front of the parameter.
Please do not provide alternative code e.g. sed etc. use perl
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!^# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
Expected output
parameter=replaced
# parameter=10
parameter=replaced
PS if you are interested to see how I progressed with this, look here:Perl Negative Lookbehind with variable length bypass maybe?
grep regular-expression perl
New contributor
|
show 1 more comment
UPDATE:
- The REAL answer to this MULTILINE question was given here by Stephane https://unix.stackexchange.com/a/521560/354415
- Alternatively go line by line with perl by Terdon below: https://unix.stackexchange.com/a/521512/354415
- Alternatively go line by line with IFS by myself here: https://unix.stackexchange.com/a/521550/354415
Here is my new toy:
Problem is that I want it to match only on lines which do not have the #s* in front of the parameter.
Please do not provide alternative code e.g. sed etc. use perl
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!^# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
Expected output
parameter=replaced
# parameter=10
parameter=replaced
PS if you are interested to see how I progressed with this, look here:Perl Negative Lookbehind with variable length bypass maybe?
grep regular-expression perl
New contributor
What is your expected output?
– Inian
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
1
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing$ARGV[N]
ones and you have needless parentheses around yourprint
call.
– terdon♦
18 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago
|
show 1 more comment
UPDATE:
- The REAL answer to this MULTILINE question was given here by Stephane https://unix.stackexchange.com/a/521560/354415
- Alternatively go line by line with perl by Terdon below: https://unix.stackexchange.com/a/521512/354415
- Alternatively go line by line with IFS by myself here: https://unix.stackexchange.com/a/521550/354415
Here is my new toy:
Problem is that I want it to match only on lines which do not have the #s* in front of the parameter.
Please do not provide alternative code e.g. sed etc. use perl
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!^# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
Expected output
parameter=replaced
# parameter=10
parameter=replaced
PS if you are interested to see how I progressed with this, look here:Perl Negative Lookbehind with variable length bypass maybe?
grep regular-expression perl
New contributor
UPDATE:
- The REAL answer to this MULTILINE question was given here by Stephane https://unix.stackexchange.com/a/521560/354415
- Alternatively go line by line with perl by Terdon below: https://unix.stackexchange.com/a/521512/354415
- Alternatively go line by line with IFS by myself here: https://unix.stackexchange.com/a/521550/354415
Here is my new toy:
Problem is that I want it to match only on lines which do not have the #s* in front of the parameter.
Please do not provide alternative code e.g. sed etc. use perl
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!^# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
Expected output
parameter=replaced
# parameter=10
parameter=replaced
PS if you are interested to see how I progressed with this, look here:Perl Negative Lookbehind with variable length bypass maybe?
grep regular-expression perl
grep regular-expression perl
New contributor
New contributor
edited 23 mins ago
Rui F Ribeiro
42.6k1688147
42.6k1688147
New contributor
asked 18 hours ago
conanDrumconanDrum
165
165
New contributor
New contributor
What is your expected output?
– Inian
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
1
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing$ARGV[N]
ones and you have needless parentheses around yourprint
call.
– terdon♦
18 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago
|
show 1 more comment
What is your expected output?
– Inian
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
1
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing$ARGV[N]
ones and you have needless parentheses around yourprint
call.
– terdon♦
18 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago
What is your expected output?
– Inian
18 hours ago
What is your expected output?
– Inian
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
1
1
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing
$ARGV[N]
ones and you have needless parentheses around your print
call.– terdon♦
18 hours ago
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing
$ARGV[N]
ones and you have needless parentheses around your print
call.– terdon♦
18 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
This is complicated by your choice to pass the multi-line string as a single variable. it is much easier to just convert that to an array so you can use the s///
operator in its simplest form. Try this:
You said you're not interested in code improvements, but to help future users, here's a simplified version of what you wrote which also avoids the bad practice of using ALLCAPS for non-environment variables:
newparameter='parameter=replaced'
filesection='
parameter=9
# parameter=10
parameter=10
'
matchparamperl='^([^#]*)parameters*=.*'
sectionfixed=$( perl -le '
@lines=split(/n/,$ARGV[0]);
s/$ARGV[1]/$1$ARGV[2]/g for @lines;
print join "n", @lines
' "$filesection" "$matchparamperl" "$newparameter")
echo "$sectionfixed"
That will return:
$ echo "$sectionfixed"
parameter=replaced
# parameter=10
parameter=replaced
Alternatively, a little shorter:
sectionfixed=$( perl -le '
do{
s/$ARGV[1]/$1$ARGV[2]/g; print
} for split(/n/,$ARGV[0])
' "$filesection" "$matchparamperl" "$newparameter")
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.
– terdon♦
11 hours ago
|
show 2 more comments
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
This is how its done )))
Output:
parameter=replaced
# parameter=10
parameter=replaced
Called Negative Lookbehind
http://www.blackwasp.co.uk/RegexLookahead.aspx
Thanks to Kusalananda for providing the idea.
I would never have thought of it.
New contributor
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to goINPUT VARS > perl > OUTPUT VAR
.
– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
add a comment |
contents='
parameter=9
# parameter=10
parameter=10
'
sed '/^#/!s/parameter[[:blank:]]*=.*/parameter=replaced/' <<<"$contents"
Output:
parameter=replaced
# parameter=10
parameter=replaced
The sed
code is simply applying the substitution to all lines not starting with a #
character.
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
|
show 6 more comments
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
});
}
});
conanDrum 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%2f521508%2fperl-multiline-match-for-something-which-starts-without-s%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is complicated by your choice to pass the multi-line string as a single variable. it is much easier to just convert that to an array so you can use the s///
operator in its simplest form. Try this:
You said you're not interested in code improvements, but to help future users, here's a simplified version of what you wrote which also avoids the bad practice of using ALLCAPS for non-environment variables:
newparameter='parameter=replaced'
filesection='
parameter=9
# parameter=10
parameter=10
'
matchparamperl='^([^#]*)parameters*=.*'
sectionfixed=$( perl -le '
@lines=split(/n/,$ARGV[0]);
s/$ARGV[1]/$1$ARGV[2]/g for @lines;
print join "n", @lines
' "$filesection" "$matchparamperl" "$newparameter")
echo "$sectionfixed"
That will return:
$ echo "$sectionfixed"
parameter=replaced
# parameter=10
parameter=replaced
Alternatively, a little shorter:
sectionfixed=$( perl -le '
do{
s/$ARGV[1]/$1$ARGV[2]/g; print
} for split(/n/,$ARGV[0])
' "$filesection" "$matchparamperl" "$newparameter")
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.
– terdon♦
11 hours ago
|
show 2 more comments
This is complicated by your choice to pass the multi-line string as a single variable. it is much easier to just convert that to an array so you can use the s///
operator in its simplest form. Try this:
You said you're not interested in code improvements, but to help future users, here's a simplified version of what you wrote which also avoids the bad practice of using ALLCAPS for non-environment variables:
newparameter='parameter=replaced'
filesection='
parameter=9
# parameter=10
parameter=10
'
matchparamperl='^([^#]*)parameters*=.*'
sectionfixed=$( perl -le '
@lines=split(/n/,$ARGV[0]);
s/$ARGV[1]/$1$ARGV[2]/g for @lines;
print join "n", @lines
' "$filesection" "$matchparamperl" "$newparameter")
echo "$sectionfixed"
That will return:
$ echo "$sectionfixed"
parameter=replaced
# parameter=10
parameter=replaced
Alternatively, a little shorter:
sectionfixed=$( perl -le '
do{
s/$ARGV[1]/$1$ARGV[2]/g; print
} for split(/n/,$ARGV[0])
' "$filesection" "$matchparamperl" "$newparameter")
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.
– terdon♦
11 hours ago
|
show 2 more comments
This is complicated by your choice to pass the multi-line string as a single variable. it is much easier to just convert that to an array so you can use the s///
operator in its simplest form. Try this:
You said you're not interested in code improvements, but to help future users, here's a simplified version of what you wrote which also avoids the bad practice of using ALLCAPS for non-environment variables:
newparameter='parameter=replaced'
filesection='
parameter=9
# parameter=10
parameter=10
'
matchparamperl='^([^#]*)parameters*=.*'
sectionfixed=$( perl -le '
@lines=split(/n/,$ARGV[0]);
s/$ARGV[1]/$1$ARGV[2]/g for @lines;
print join "n", @lines
' "$filesection" "$matchparamperl" "$newparameter")
echo "$sectionfixed"
That will return:
$ echo "$sectionfixed"
parameter=replaced
# parameter=10
parameter=replaced
Alternatively, a little shorter:
sectionfixed=$( perl -le '
do{
s/$ARGV[1]/$1$ARGV[2]/g; print
} for split(/n/,$ARGV[0])
' "$filesection" "$matchparamperl" "$newparameter")
This is complicated by your choice to pass the multi-line string as a single variable. it is much easier to just convert that to an array so you can use the s///
operator in its simplest form. Try this:
You said you're not interested in code improvements, but to help future users, here's a simplified version of what you wrote which also avoids the bad practice of using ALLCAPS for non-environment variables:
newparameter='parameter=replaced'
filesection='
parameter=9
# parameter=10
parameter=10
'
matchparamperl='^([^#]*)parameters*=.*'
sectionfixed=$( perl -le '
@lines=split(/n/,$ARGV[0]);
s/$ARGV[1]/$1$ARGV[2]/g for @lines;
print join "n", @lines
' "$filesection" "$matchparamperl" "$newparameter")
echo "$sectionfixed"
That will return:
$ echo "$sectionfixed"
parameter=replaced
# parameter=10
parameter=replaced
Alternatively, a little shorter:
sectionfixed=$( perl -le '
do{
s/$ARGV[1]/$1$ARGV[2]/g; print
} for split(/n/,$ARGV[0])
' "$filesection" "$matchparamperl" "$newparameter")
edited 17 hours ago
answered 17 hours ago
terdon♦terdon
136k33276457
136k33276457
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.
– terdon♦
11 hours ago
|
show 2 more comments
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.
– terdon♦
11 hours ago
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
thanks for taking the time. Your answer is better than mine, because yours allows for any number of spaces and any number of # before the parameter to be disregarded. However, as you rightly mention, it is NOT using the multiline string and has to go line by line. My question was for multiline in order to investigate solutions. I would like to invite you then to something more relevant here: unix.stackexchange.com/questions/521523/… where this answer is more appropriate.
– conanDrum
14 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
@conanDrum you can do it with a multiline string but that's just making your life harder and your code less clear for no good reason.
– terdon♦
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I agree, but this was the point of the question. to see what can be done with multiline. And this answers it. nothing much in the way of what was requested. It has to be done line by line. Having said that, the only answer that answered the question regarding multiline was Negative Lookbehind, i think you will aggree.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
I already mentioned you in the other thread. unfortunately I cannot upvote anything yet.
– conanDrum
13 hours ago
1
1
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:
sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.– terdon♦
11 hours ago
@conanDrum that's a completely different issue. And it will depend on how you want this information to be shown. Please post a separate question if you need help with that. But here's a starter:
sectionfixed=$( perl -le '@lines=split(/n/,$ARGV[0]); $k+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; print join "n", @lines; print STDERR "$k substitutions maden";' "$filesection" "$matchparamperl" "$newparameter")
. Of course, it doesn't make much sense if you insist on mixing perl and the shell as you have requested.– terdon♦
11 hours ago
|
show 2 more comments
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
This is how its done )))
Output:
parameter=replaced
# parameter=10
parameter=replaced
Called Negative Lookbehind
http://www.blackwasp.co.uk/RegexLookahead.aspx
Thanks to Kusalananda for providing the idea.
I would never have thought of it.
New contributor
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to goINPUT VARS > perl > OUTPUT VAR
.
– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
add a comment |
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
This is how its done )))
Output:
parameter=replaced
# parameter=10
parameter=replaced
Called Negative Lookbehind
http://www.blackwasp.co.uk/RegexLookahead.aspx
Thanks to Kusalananda for providing the idea.
I would never have thought of it.
New contributor
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to goINPUT VARS > perl > OUTPUT VAR
.
– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
add a comment |
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
This is how its done )))
Output:
parameter=replaced
# parameter=10
parameter=replaced
Called Negative Lookbehind
http://www.blackwasp.co.uk/RegexLookahead.aspx
Thanks to Kusalananda for providing the idea.
I would never have thought of it.
New contributor
perl -we 'my $file= "parameter=9
# parameter=10
parameter=10
"; $file=~ s/.*((?<!# ))parameters*=.*/parameter=replaced/g; print(":$file:n")'
This is how its done )))
Output:
parameter=replaced
# parameter=10
parameter=replaced
Called Negative Lookbehind
http://www.blackwasp.co.uk/RegexLookahead.aspx
Thanks to Kusalananda for providing the idea.
I would never have thought of it.
New contributor
New contributor
answered 17 hours ago
conanDrumconanDrum
165
165
New contributor
New contributor
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to goINPUT VARS > perl > OUTPUT VAR
.
– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
add a comment |
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to goINPUT VARS > perl > OUTPUT VAR
.
– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to go
INPUT VARS > perl > OUTPUT VAR
.– terdon♦
17 hours ago
That isn't the output you asked for. The empty line and the leading space from the first line have been removed. And yu have ignored your own requirement to go
INPUT VARS > perl > OUTPUT VAR
.– terdon♦
17 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
based on -2 points on the question, and your own complaint, I decided to make things simpler. So now the point is this: unix.stackexchange.com/questions/521523/… maybe you can help?
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
And yes, when I simplified the code, I forgot to change the expected output )) I will do it now.
– conanDrum
16 hours ago
add a comment |
contents='
parameter=9
# parameter=10
parameter=10
'
sed '/^#/!s/parameter[[:blank:]]*=.*/parameter=replaced/' <<<"$contents"
Output:
parameter=replaced
# parameter=10
parameter=replaced
The sed
code is simply applying the substitution to all lines not starting with a #
character.
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
|
show 6 more comments
contents='
parameter=9
# parameter=10
parameter=10
'
sed '/^#/!s/parameter[[:blank:]]*=.*/parameter=replaced/' <<<"$contents"
Output:
parameter=replaced
# parameter=10
parameter=replaced
The sed
code is simply applying the substitution to all lines not starting with a #
character.
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
|
show 6 more comments
contents='
parameter=9
# parameter=10
parameter=10
'
sed '/^#/!s/parameter[[:blank:]]*=.*/parameter=replaced/' <<<"$contents"
Output:
parameter=replaced
# parameter=10
parameter=replaced
The sed
code is simply applying the substitution to all lines not starting with a #
character.
contents='
parameter=9
# parameter=10
parameter=10
'
sed '/^#/!s/parameter[[:blank:]]*=.*/parameter=replaced/' <<<"$contents"
Output:
parameter=replaced
# parameter=10
parameter=replaced
The sed
code is simply applying the substitution to all lines not starting with a #
character.
edited 16 hours ago
answered 18 hours ago
Kusalananda♦Kusalananda
148k18279467
148k18279467
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
|
show 6 more comments
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
No thanks.. The task has to be done with the code provided. Only the regex must be manipulated.
– conanDrum
18 hours ago
2
2
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
@conanDrum then please edit your question and make that clear so we don't waste your time, or ours, with solutions that won't work for you. And why can't we fix your perl code? There are a few odd choices you've made there and the syntax is more complicated than it needs to be.
– terdon♦
18 hours ago
thanks done it.
– conanDrum
18 hours ago
thanks done it.
– conanDrum
18 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
Not true... Only the first parameter is changed with MATCHPARAMPERL='^[^#].*parameters*=.*'. this is because you placed the ^ at the start and this caused it to replace only the first one.
– conanDrum
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
@conanDrum I know, that's why I removed that bit. You may want to look up "negative lookbehind assertions".
– Kusalananda♦
17 hours ago
|
show 6 more comments
conanDrum is a new contributor. Be nice, and check out our Code of Conduct.
conanDrum is a new contributor. Be nice, and check out our Code of Conduct.
conanDrum is a new contributor. Be nice, and check out our Code of Conduct.
conanDrum 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%2f521508%2fperl-multiline-match-for-something-which-starts-without-s%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
What is your expected output?
– Inian
18 hours ago
root@ns1:~# ./test.sh parameter=replaced # parameter=10 parameter=replaced
– conanDrum
18 hours ago
Basically it should not replace the second parameter which has a hash and space in front.
– conanDrum
18 hours ago
1
"Please do not provide alternative code." Why not? Your code is not very clear or efficient. For example, you are creating 3 useless variables instead of using the existing
$ARGV[N]
ones and you have needless parentheses around yourprint
call.– terdon♦
18 hours ago
Its only for readability.. You are free to modify, but keep the original idea intact. i.e. INPUT VARS > perl > OUTPUT VAR. Thank you. What I meant by altrnatives is about solutions in other languages and methods. This has to be kept inline with a project that requires perl regex
– conanDrum
17 hours ago