extracting parts of a file to different file with sed











up vote
-1
down vote

favorite












I want to write a script to extract parts of my data file and save it in different files. I wrote a script but it gives some error. The script is



#!/bin/bash

nn=0
mn=0

for var1 in 1 2 3 ; do
nn=nn+2
mn=mn+302

sed -n '$nn','$mn'p dos.dat > $var1.dat

nn=mn
done









share|improve this question
























  • what error....?
    – 123
    Nov 11 '15 at 10:21















up vote
-1
down vote

favorite












I want to write a script to extract parts of my data file and save it in different files. I wrote a script but it gives some error. The script is



#!/bin/bash

nn=0
mn=0

for var1 in 1 2 3 ; do
nn=nn+2
mn=mn+302

sed -n '$nn','$mn'p dos.dat > $var1.dat

nn=mn
done









share|improve this question
























  • what error....?
    – 123
    Nov 11 '15 at 10:21













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to write a script to extract parts of my data file and save it in different files. I wrote a script but it gives some error. The script is



#!/bin/bash

nn=0
mn=0

for var1 in 1 2 3 ; do
nn=nn+2
mn=mn+302

sed -n '$nn','$mn'p dos.dat > $var1.dat

nn=mn
done









share|improve this question















I want to write a script to extract parts of my data file and save it in different files. I wrote a script but it gives some error. The script is



#!/bin/bash

nn=0
mn=0

for var1 in 1 2 3 ; do
nn=nn+2
mn=mn+302

sed -n '$nn','$mn'p dos.dat > $var1.dat

nn=mn
done






shell-script sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 14:58









Rui F Ribeiro

38.3k1475126




38.3k1475126










asked Nov 11 '15 at 9:57









dsd

11




11












  • what error....?
    – 123
    Nov 11 '15 at 10:21


















  • what error....?
    – 123
    Nov 11 '15 at 10:21
















what error....?
– 123
Nov 11 '15 at 10:21




what error....?
– 123
Nov 11 '15 at 10:21










1 Answer
1






active

oldest

votes

















up vote
2
down vote













The immediate problem with your script is that the variables are single-quoted, preventing them from being expanded by the shell:



sed -n '$nn','$mn'p dos.dat > $var1.dat


Perhaps you meant something like



sed -n "$nn,${mn}p" dos.dat > $var1.dat


where the second variable is delimited with { and } to separate it from p.



Also, the expression syntax is a problem. The variables on the right hand side of "=" need "$" to get their value. Without treating them with '$((' or expr, the right hand side also is just a string — not a number.



Perhaps you meant something like this:



#!/bin/bash

nn=0 ; mn=0
for var1 in 1 2 3 ; do
nn=$(($nn+2))
mn=$(($mn+302))

sed -n "$nn,${mn}p" dos.dat > $var1.dat

nn=$mn
done


According to POSIX, the '$' within $(( is unnecessary: either would work:




If the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.







share|improve this answer























  • You don't have to $ vairables in arithmetic expansion.
    – 123
    Nov 11 '15 at 10:25










  • It works, and I started thinking of expr first before settling on bash-specifics.
    – Thomas Dickey
    Nov 11 '15 at 10:26










  • Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
    – Stéphane Chazelas
    Nov 11 '15 at 13:43










  • But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
    – Thomas Dickey
    Nov 11 '15 at 22:01











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f242312%2fextracting-parts-of-a-file-to-different-file-with-sed%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








up vote
2
down vote













The immediate problem with your script is that the variables are single-quoted, preventing them from being expanded by the shell:



sed -n '$nn','$mn'p dos.dat > $var1.dat


Perhaps you meant something like



sed -n "$nn,${mn}p" dos.dat > $var1.dat


where the second variable is delimited with { and } to separate it from p.



Also, the expression syntax is a problem. The variables on the right hand side of "=" need "$" to get their value. Without treating them with '$((' or expr, the right hand side also is just a string — not a number.



Perhaps you meant something like this:



#!/bin/bash

nn=0 ; mn=0
for var1 in 1 2 3 ; do
nn=$(($nn+2))
mn=$(($mn+302))

sed -n "$nn,${mn}p" dos.dat > $var1.dat

nn=$mn
done


According to POSIX, the '$' within $(( is unnecessary: either would work:




If the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.







share|improve this answer























  • You don't have to $ vairables in arithmetic expansion.
    – 123
    Nov 11 '15 at 10:25










  • It works, and I started thinking of expr first before settling on bash-specifics.
    – Thomas Dickey
    Nov 11 '15 at 10:26










  • Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
    – Stéphane Chazelas
    Nov 11 '15 at 13:43










  • But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
    – Thomas Dickey
    Nov 11 '15 at 22:01















up vote
2
down vote













The immediate problem with your script is that the variables are single-quoted, preventing them from being expanded by the shell:



sed -n '$nn','$mn'p dos.dat > $var1.dat


Perhaps you meant something like



sed -n "$nn,${mn}p" dos.dat > $var1.dat


where the second variable is delimited with { and } to separate it from p.



Also, the expression syntax is a problem. The variables on the right hand side of "=" need "$" to get their value. Without treating them with '$((' or expr, the right hand side also is just a string — not a number.



Perhaps you meant something like this:



#!/bin/bash

nn=0 ; mn=0
for var1 in 1 2 3 ; do
nn=$(($nn+2))
mn=$(($mn+302))

sed -n "$nn,${mn}p" dos.dat > $var1.dat

nn=$mn
done


According to POSIX, the '$' within $(( is unnecessary: either would work:




If the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.







share|improve this answer























  • You don't have to $ vairables in arithmetic expansion.
    – 123
    Nov 11 '15 at 10:25










  • It works, and I started thinking of expr first before settling on bash-specifics.
    – Thomas Dickey
    Nov 11 '15 at 10:26










  • Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
    – Stéphane Chazelas
    Nov 11 '15 at 13:43










  • But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
    – Thomas Dickey
    Nov 11 '15 at 22:01













up vote
2
down vote










up vote
2
down vote









The immediate problem with your script is that the variables are single-quoted, preventing them from being expanded by the shell:



sed -n '$nn','$mn'p dos.dat > $var1.dat


Perhaps you meant something like



sed -n "$nn,${mn}p" dos.dat > $var1.dat


where the second variable is delimited with { and } to separate it from p.



Also, the expression syntax is a problem. The variables on the right hand side of "=" need "$" to get their value. Without treating them with '$((' or expr, the right hand side also is just a string — not a number.



Perhaps you meant something like this:



#!/bin/bash

nn=0 ; mn=0
for var1 in 1 2 3 ; do
nn=$(($nn+2))
mn=$(($mn+302))

sed -n "$nn,${mn}p" dos.dat > $var1.dat

nn=$mn
done


According to POSIX, the '$' within $(( is unnecessary: either would work:




If the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.







share|improve this answer














The immediate problem with your script is that the variables are single-quoted, preventing them from being expanded by the shell:



sed -n '$nn','$mn'p dos.dat > $var1.dat


Perhaps you meant something like



sed -n "$nn,${mn}p" dos.dat > $var1.dat


where the second variable is delimited with { and } to separate it from p.



Also, the expression syntax is a problem. The variables on the right hand side of "=" need "$" to get their value. Without treating them with '$((' or expr, the right hand side also is just a string — not a number.



Perhaps you meant something like this:



#!/bin/bash

nn=0 ; mn=0
for var1 in 1 2 3 ; do
nn=$(($nn+2))
mn=$(($mn+302))

sed -n "$nn,${mn}p" dos.dat > $var1.dat

nn=$mn
done


According to POSIX, the '$' within $(( is unnecessary: either would work:




If the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 '15 at 10:48









cas

38.3k44898




38.3k44898










answered Nov 11 '15 at 10:15









Thomas Dickey

51.5k594164




51.5k594164












  • You don't have to $ vairables in arithmetic expansion.
    – 123
    Nov 11 '15 at 10:25










  • It works, and I started thinking of expr first before settling on bash-specifics.
    – Thomas Dickey
    Nov 11 '15 at 10:26










  • Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
    – Stéphane Chazelas
    Nov 11 '15 at 13:43










  • But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
    – Thomas Dickey
    Nov 11 '15 at 22:01


















  • You don't have to $ vairables in arithmetic expansion.
    – 123
    Nov 11 '15 at 10:25










  • It works, and I started thinking of expr first before settling on bash-specifics.
    – Thomas Dickey
    Nov 11 '15 at 10:26










  • Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
    – Stéphane Chazelas
    Nov 11 '15 at 13:43










  • But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
    – Thomas Dickey
    Nov 11 '15 at 22:01
















You don't have to $ vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25




You don't have to $ vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25












It works, and I started thinking of expr first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26




It works, and I started thinking of expr first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26












Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
– Stéphane Chazelas
Nov 11 '15 at 13:43




Note that nn=nn+2 will work in ksh/zsh/bash if the variable was previously declared as integer (typeset -i nn).
– Stéphane Chazelas
Nov 11 '15 at 13:43












But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
– Thomas Dickey
Nov 11 '15 at 22:01




But I don't see typeset in POSIX, and as a rule I start off by checking if there was a valid need for non-POSIX features before considering how to improve their use. In this case, there was no such need.
– Thomas Dickey
Nov 11 '15 at 22:01


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f242312%2fextracting-parts-of-a-file-to-different-file-with-sed%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

サソリ

広島県道265号伴広島線

Accessing regular linux commands in Huawei's Dopra Linux