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
shell-script sed
add a comment |
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
shell-script sed
what error....?
– 123
Nov 11 '15 at 10:21
add a comment |
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
shell-script sed
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
shell-script sed
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
add a comment |
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
add a comment |
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.
You don't have to$
vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25
It works, and I started thinking ofexpr
first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26
Note thatnn=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 seetypeset
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
add a comment |
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.
You don't have to$
vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25
It works, and I started thinking ofexpr
first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26
Note thatnn=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 seetypeset
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
add a comment |
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.
You don't have to$
vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25
It works, and I started thinking ofexpr
first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26
Note thatnn=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 seetypeset
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
add a comment |
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.
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.
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 ofexpr
first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26
Note thatnn=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 seetypeset
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
add a comment |
You don't have to$
vairables in arithmetic expansion.
– 123
Nov 11 '15 at 10:25
It works, and I started thinking ofexpr
first before settling on bash-specifics.
– Thomas Dickey
Nov 11 '15 at 10:26
Note thatnn=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 seetypeset
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 error....?
– 123
Nov 11 '15 at 10:21