Use sed replace with line number from variable
This is my sed
command:
while ...;
do sed -r "${counter}s/^S+ /$line /g" $in > $out;
..
..
done
Unfortunately this command isn't doing anything when called from within a bash script/loop. So I thought to check if the variables are being resolved the right way:
do echo ´sed -r "${counter}s/^S+/$line/g" $in > $out´;
which printed this to the console:
sed -r <line number>/^S+/<replace pattern>/g <infile> > <outfile>
When executing this very command (without the ´) from the console, I get this:
sed: -e expression #1, char 8: unterminated s' command
I guess this is because the '
are missing around the pattern.
So how do I combine double (for resolving variables in sed command) and single (for completing the search/replace pattern) quotation marks when calling this from a bash script?
bash sed
bumped to the homepage by Community♦ 14 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 3 more comments
This is my sed
command:
while ...;
do sed -r "${counter}s/^S+ /$line /g" $in > $out;
..
..
done
Unfortunately this command isn't doing anything when called from within a bash script/loop. So I thought to check if the variables are being resolved the right way:
do echo ´sed -r "${counter}s/^S+/$line/g" $in > $out´;
which printed this to the console:
sed -r <line number>/^S+/<replace pattern>/g <infile> > <outfile>
When executing this very command (without the ´) from the console, I get this:
sed: -e expression #1, char 8: unterminated s' command
I guess this is because the '
are missing around the pattern.
So how do I combine double (for resolving variables in sed command) and single (for completing the search/replace pattern) quotation marks when calling this from a bash script?
bash sed
bumped to the homepage by Community♦ 14 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
how could it possibly work? i dont see any input at all, and it looks like youre callingsed
to work a single line at a time. if thats the case then$counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do:while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.
– mikeserv
Oct 28 '15 at 9:46
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
1
Is there/
orn
in$line
variable? Any way try to executesed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?
– Costas
Oct 28 '15 at 10:16
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25
|
show 3 more comments
This is my sed
command:
while ...;
do sed -r "${counter}s/^S+ /$line /g" $in > $out;
..
..
done
Unfortunately this command isn't doing anything when called from within a bash script/loop. So I thought to check if the variables are being resolved the right way:
do echo ´sed -r "${counter}s/^S+/$line/g" $in > $out´;
which printed this to the console:
sed -r <line number>/^S+/<replace pattern>/g <infile> > <outfile>
When executing this very command (without the ´) from the console, I get this:
sed: -e expression #1, char 8: unterminated s' command
I guess this is because the '
are missing around the pattern.
So how do I combine double (for resolving variables in sed command) and single (for completing the search/replace pattern) quotation marks when calling this from a bash script?
bash sed
This is my sed
command:
while ...;
do sed -r "${counter}s/^S+ /$line /g" $in > $out;
..
..
done
Unfortunately this command isn't doing anything when called from within a bash script/loop. So I thought to check if the variables are being resolved the right way:
do echo ´sed -r "${counter}s/^S+/$line/g" $in > $out´;
which printed this to the console:
sed -r <line number>/^S+/<replace pattern>/g <infile> > <outfile>
When executing this very command (without the ´) from the console, I get this:
sed: -e expression #1, char 8: unterminated s' command
I guess this is because the '
are missing around the pattern.
So how do I combine double (for resolving variables in sed command) and single (for completing the search/replace pattern) quotation marks when calling this from a bash script?
bash sed
bash sed
edited Oct 28 '15 at 10:16
serenesat
9341519
9341519
asked Oct 28 '15 at 9:42
entenbeinentenbein
12
12
bumped to the homepage by Community♦ 14 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 14 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
how could it possibly work? i dont see any input at all, and it looks like youre callingsed
to work a single line at a time. if thats the case then$counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do:while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.
– mikeserv
Oct 28 '15 at 9:46
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
1
Is there/
orn
in$line
variable? Any way try to executesed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?
– Costas
Oct 28 '15 at 10:16
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25
|
show 3 more comments
how could it possibly work? i dont see any input at all, and it looks like youre callingsed
to work a single line at a time. if thats the case then$counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do:while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.
– mikeserv
Oct 28 '15 at 9:46
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
1
Is there/
orn
in$line
variable? Any way try to executesed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?
– Costas
Oct 28 '15 at 10:16
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25
how could it possibly work? i dont see any input at all, and it looks like youre calling
sed
to work a single line at a time. if thats the case then $counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do: while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.– mikeserv
Oct 28 '15 at 9:46
how could it possibly work? i dont see any input at all, and it looks like youre calling
sed
to work a single line at a time. if thats the case then $counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do: while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.– mikeserv
Oct 28 '15 at 9:46
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
1
1
Is there
/
or n
in $line
variable? Any way try to execute sed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?– Costas
Oct 28 '15 at 10:16
Is there
/
or n
in $line
variable? Any way try to execute sed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?– Costas
Oct 28 '15 at 10:16
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25
|
show 3 more comments
1 Answer
1
active
oldest
votes
Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile>
with the following sed replace command inplace works like a charm....
Thanks for your help!
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f239177%2fuse-sed-replace-with-line-number-from-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile>
with the following sed replace command inplace works like a charm....
Thanks for your help!
add a comment |
Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile>
with the following sed replace command inplace works like a charm....
Thanks for your help!
add a comment |
Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile>
with the following sed replace command inplace works like a charm....
Thanks for your help!
Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile>
with the following sed replace command inplace works like a charm....
Thanks for your help!
answered Oct 28 '15 at 10:57
entenbeinentenbein
12
12
add a comment |
add a comment |
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%2f239177%2fuse-sed-replace-with-line-number-from-variable%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
how could it possibly work? i dont see any input at all, and it looks like youre calling
sed
to work a single line at a time. if thats the case then$counter
wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do:while gen ouput; do :; done | sed 's/edit/the_whole_stream/'
you'll be a lot better off.– mikeserv
Oct 28 '15 at 9:46
$counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line.
– entenbein
Oct 28 '15 at 9:57
awk or perl are better tools for doing that.
– cas
Oct 28 '15 at 10:02
1
Is there
/
orn
in$line
variable? Any way try to executesed -r "<line number>/^S+/<replace pattern>/g" <infile> > <outfile>
for testing purpose. Is it doing what you wants?– Costas
Oct 28 '15 at 10:16
Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far.
– entenbein
Oct 28 '15 at 10:25