Copying lines from one file to another using sed and shell variablesUsing sed to replace a string with...
Billiard balls collision
Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?
Why is strlen so complex in C?
Talk interpreter
Is a memoized pure function itself considered pure?
How does the OS tell whether an "Address is already in use"?
Contours of a national emergency in the United States
How long do you think advanced cybernetic implants would plausibly last?
Set orthographic view using python?
How to prevent a hosting company from accessing a VM's encryption keys?
Why is a statement like 1 + n *= 3 allowed in Ruby?
Changing JPEG to RAW to use on Lightroom?
How to say "I only speak one which is English." in French?
What is the best way to solve this 6x6 sudoku?
74S vs 74LS ICs
Why is "dyadic" the only word with the prefix "dy-"?
Why does matter stays collapsed following the supernova explosion?
Breaker Mapping Questions
Given current technology, could TV display screens double as video camera sensors?
Why does Windows store Wi-Fi passwords in a reversible format?
Is it unusual for a math department not to have a mail/web server?
How does Mercy remove conditions?
Which old Technic set included large yellow motor?
Can MuseScore be used programmatically?
Copying lines from one file to another using sed and shell variables
Using sed to replace a string with special chars with another string with special charactersCutting logs (and not copying) from a log file between two time stampsShell Script to insert lines in between filePerforming Prime Key function using 'Sed' in BashDelete all lines that contain duplicate lettersFind words after specific symbol on linesed - calling a variable from a file with multilineReplace a pattern in File1 and replace it with corresponding matched pattern + column in File2Fixing a .csv file where some rows have missing columnsHow can I use sed to insert some text after a multiline match?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.
sed -n "$var1,$var2'p'" file1.txt > file2.txt
I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.
shell sed variable-substitution
New contributor
add a comment |
I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.
sed -n "$var1,$var2'p'" file1.txt > file2.txt
I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.
shell sed variable-substitution
New contributor
1
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
1
> file2.txt
will overwritefile2
: if you want to append the copied lines to the end of the file, use>> file2.txt
– steeldriver
1 hour ago
add a comment |
I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.
sed -n "$var1,$var2'p'" file1.txt > file2.txt
I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.
shell sed variable-substitution
New contributor
I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.
sed -n "$var1,$var2'p'" file1.txt > file2.txt
I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.
shell sed variable-substitution
shell sed variable-substitution
New contributor
New contributor
New contributor
asked 1 hour ago
CrushCrush
1
1
New contributor
New contributor
1
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
1
> file2.txt
will overwritefile2
: if you want to append the copied lines to the end of the file, use>> file2.txt
– steeldriver
1 hour ago
add a comment |
1
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
1
> file2.txt
will overwritefile2
: if you want to append the copied lines to the end of the file, use>> file2.txt
– steeldriver
1 hour ago
1
1
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
1
1
> file2.txt
will overwrite file2
: if you want to append the copied lines to the end of the file, use >> file2.txt
– steeldriver
1 hour ago
> file2.txt
will overwrite file2
: if you want to append the copied lines to the end of the file, use >> file2.txt
– steeldriver
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
Use curly braces around your variable names:
sed -n "${var1},${var2}p" file1.txt > file2.txt
The braces around the first variable are not needed, but better make it consistent.
add a comment |
Freddy's answer works, but lacks an explanation. The sed
format that you are probably aiming for is something like
sed -n 4,6p file1.txt
Where I've used 4
and 6
as dummy numbers.
Let's examine your expression, using these dummy numbers again, and the echo
command.
$ var1=4
$ var2=6
$ echo "$var1,$var2'p'"
4,6'p'
Because you enclosed the '
s in the "
s, they are interpreted literally, and you are literal '
s to the sed command. Hence, when you try to run your command:
$ sed -n "$var1,$var2'p'" file1.txt
sed: -e expression #1, char 4: unknown command: `''
Solution
You can remove the '
s from your expression, but this would change the variable name to $var2p
instead. Hence, as Freddy suggests, you can use {…}
to explicitly enclose the variable name(s). i.e.
sed -n "${var1},${var2}p" file1.txt
You can test if this works by putting an echo before the command. An alternative format is
sed -n "$var1,$var2"p file1.txt
but IMO it's less readable.
Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt
instead of > file2.txt
.
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
});
}
});
Crush 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%2f537555%2fcopying-lines-from-one-file-to-another-using-sed-and-shell-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use curly braces around your variable names:
sed -n "${var1},${var2}p" file1.txt > file2.txt
The braces around the first variable are not needed, but better make it consistent.
add a comment |
Use curly braces around your variable names:
sed -n "${var1},${var2}p" file1.txt > file2.txt
The braces around the first variable are not needed, but better make it consistent.
add a comment |
Use curly braces around your variable names:
sed -n "${var1},${var2}p" file1.txt > file2.txt
The braces around the first variable are not needed, but better make it consistent.
Use curly braces around your variable names:
sed -n "${var1},${var2}p" file1.txt > file2.txt
The braces around the first variable are not needed, but better make it consistent.
answered 1 hour ago
FreddyFreddy
6,8481 gold badge6 silver badges24 bronze badges
6,8481 gold badge6 silver badges24 bronze badges
add a comment |
add a comment |
Freddy's answer works, but lacks an explanation. The sed
format that you are probably aiming for is something like
sed -n 4,6p file1.txt
Where I've used 4
and 6
as dummy numbers.
Let's examine your expression, using these dummy numbers again, and the echo
command.
$ var1=4
$ var2=6
$ echo "$var1,$var2'p'"
4,6'p'
Because you enclosed the '
s in the "
s, they are interpreted literally, and you are literal '
s to the sed command. Hence, when you try to run your command:
$ sed -n "$var1,$var2'p'" file1.txt
sed: -e expression #1, char 4: unknown command: `''
Solution
You can remove the '
s from your expression, but this would change the variable name to $var2p
instead. Hence, as Freddy suggests, you can use {…}
to explicitly enclose the variable name(s). i.e.
sed -n "${var1},${var2}p" file1.txt
You can test if this works by putting an echo before the command. An alternative format is
sed -n "$var1,$var2"p file1.txt
but IMO it's less readable.
Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt
instead of > file2.txt
.
add a comment |
Freddy's answer works, but lacks an explanation. The sed
format that you are probably aiming for is something like
sed -n 4,6p file1.txt
Where I've used 4
and 6
as dummy numbers.
Let's examine your expression, using these dummy numbers again, and the echo
command.
$ var1=4
$ var2=6
$ echo "$var1,$var2'p'"
4,6'p'
Because you enclosed the '
s in the "
s, they are interpreted literally, and you are literal '
s to the sed command. Hence, when you try to run your command:
$ sed -n "$var1,$var2'p'" file1.txt
sed: -e expression #1, char 4: unknown command: `''
Solution
You can remove the '
s from your expression, but this would change the variable name to $var2p
instead. Hence, as Freddy suggests, you can use {…}
to explicitly enclose the variable name(s). i.e.
sed -n "${var1},${var2}p" file1.txt
You can test if this works by putting an echo before the command. An alternative format is
sed -n "$var1,$var2"p file1.txt
but IMO it's less readable.
Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt
instead of > file2.txt
.
add a comment |
Freddy's answer works, but lacks an explanation. The sed
format that you are probably aiming for is something like
sed -n 4,6p file1.txt
Where I've used 4
and 6
as dummy numbers.
Let's examine your expression, using these dummy numbers again, and the echo
command.
$ var1=4
$ var2=6
$ echo "$var1,$var2'p'"
4,6'p'
Because you enclosed the '
s in the "
s, they are interpreted literally, and you are literal '
s to the sed command. Hence, when you try to run your command:
$ sed -n "$var1,$var2'p'" file1.txt
sed: -e expression #1, char 4: unknown command: `''
Solution
You can remove the '
s from your expression, but this would change the variable name to $var2p
instead. Hence, as Freddy suggests, you can use {…}
to explicitly enclose the variable name(s). i.e.
sed -n "${var1},${var2}p" file1.txt
You can test if this works by putting an echo before the command. An alternative format is
sed -n "$var1,$var2"p file1.txt
but IMO it's less readable.
Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt
instead of > file2.txt
.
Freddy's answer works, but lacks an explanation. The sed
format that you are probably aiming for is something like
sed -n 4,6p file1.txt
Where I've used 4
and 6
as dummy numbers.
Let's examine your expression, using these dummy numbers again, and the echo
command.
$ var1=4
$ var2=6
$ echo "$var1,$var2'p'"
4,6'p'
Because you enclosed the '
s in the "
s, they are interpreted literally, and you are literal '
s to the sed command. Hence, when you try to run your command:
$ sed -n "$var1,$var2'p'" file1.txt
sed: -e expression #1, char 4: unknown command: `''
Solution
You can remove the '
s from your expression, but this would change the variable name to $var2p
instead. Hence, as Freddy suggests, you can use {…}
to explicitly enclose the variable name(s). i.e.
sed -n "${var1},${var2}p" file1.txt
You can test if this works by putting an echo before the command. An alternative format is
sed -n "$var1,$var2"p file1.txt
but IMO it's less readable.
Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt
instead of > file2.txt
.
answered 8 mins ago
SparhawkSparhawk
11.3k8 gold badges53 silver badges107 bronze badges
11.3k8 gold badges53 silver badges107 bronze badges
add a comment |
add a comment |
Crush is a new contributor. Be nice, and check out our Code of Conduct.
Crush is a new contributor. Be nice, and check out our Code of Conduct.
Crush is a new contributor. Be nice, and check out our Code of Conduct.
Crush 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%2f537555%2fcopying-lines-from-one-file-to-another-using-sed-and-shell-variables%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
What error do you receive when running the sed command in your question? Please edit your post and provide that information.
– Peschke
1 hour ago
1
> file2.txt
will overwritefile2
: if you want to append the copied lines to the end of the file, use>> file2.txt
– steeldriver
1 hour ago