awk if-else and variable reassignment
I have a text file consisting of xyz coordinates, each defining a particular depth contour of a slope.
All of these lines are stored in one file, with each contour separated by ">"
The file looks like:
>
x1 y1 z1
x2 y2 z2
>
x3 y3 z3
...
The file is huge and unwieldy and I want to print out the 7th point along each contour and pipe it into a tab delimited new file.
My code looks like this:
awk -v OFS='t' -v count=1 'if ($1 == ">") count/=count; else if (count%7 == 0) count+=1 print $0; else count+=1' infile > outfile
I keep getting an error message that says
awk: syntax error at source line 1
context is
if ($1 == ">") count/=count; >>> else <<< if (count%7 == 0) count+=1; print $0; else count+=1
awk: illegal statement at source line 1
I've spent a while checking my syntax and bracketing and it seems ok, I just might be missing something with the variable reassignment?
if-statement awk syntax
add a comment |
I have a text file consisting of xyz coordinates, each defining a particular depth contour of a slope.
All of these lines are stored in one file, with each contour separated by ">"
The file looks like:
>
x1 y1 z1
x2 y2 z2
>
x3 y3 z3
...
The file is huge and unwieldy and I want to print out the 7th point along each contour and pipe it into a tab delimited new file.
My code looks like this:
awk -v OFS='t' -v count=1 'if ($1 == ">") count/=count; else if (count%7 == 0) count+=1 print $0; else count+=1' infile > outfile
I keep getting an error message that says
awk: syntax error at source line 1
context is
if ($1 == ">") count/=count; >>> else <<< if (count%7 == 0) count+=1; print $0; else count+=1
awk: illegal statement at source line 1
I've spent a while checking my syntax and bracketing and it seems ok, I just might be missing something with the variable reassignment?
if-statement awk syntax
3
Remove the;
between the}
andelse
, ie.if(1) something(); else
butif(1) some(); thing() else
so you could basically:if ($1 == ">") count/=count; else
orif ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23
add a comment |
I have a text file consisting of xyz coordinates, each defining a particular depth contour of a slope.
All of these lines are stored in one file, with each contour separated by ">"
The file looks like:
>
x1 y1 z1
x2 y2 z2
>
x3 y3 z3
...
The file is huge and unwieldy and I want to print out the 7th point along each contour and pipe it into a tab delimited new file.
My code looks like this:
awk -v OFS='t' -v count=1 'if ($1 == ">") count/=count; else if (count%7 == 0) count+=1 print $0; else count+=1' infile > outfile
I keep getting an error message that says
awk: syntax error at source line 1
context is
if ($1 == ">") count/=count; >>> else <<< if (count%7 == 0) count+=1; print $0; else count+=1
awk: illegal statement at source line 1
I've spent a while checking my syntax and bracketing and it seems ok, I just might be missing something with the variable reassignment?
if-statement awk syntax
I have a text file consisting of xyz coordinates, each defining a particular depth contour of a slope.
All of these lines are stored in one file, with each contour separated by ">"
The file looks like:
>
x1 y1 z1
x2 y2 z2
>
x3 y3 z3
...
The file is huge and unwieldy and I want to print out the 7th point along each contour and pipe it into a tab delimited new file.
My code looks like this:
awk -v OFS='t' -v count=1 'if ($1 == ">") count/=count; else if (count%7 == 0) count+=1 print $0; else count+=1' infile > outfile
I keep getting an error message that says
awk: syntax error at source line 1
context is
if ($1 == ">") count/=count; >>> else <<< if (count%7 == 0) count+=1; print $0; else count+=1
awk: illegal statement at source line 1
I've spent a while checking my syntax and bracketing and it seems ok, I just might be missing something with the variable reassignment?
if-statement awk syntax
if-statement awk syntax
edited Nov 14 '18 at 20:23
Connor
asked Nov 14 '18 at 20:15
ConnorConnor
1
1
3
Remove the;
between the}
andelse
, ie.if(1) something(); else
butif(1) some(); thing() else
so you could basically:if ($1 == ">") count/=count; else
orif ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23
add a comment |
3
Remove the;
between the}
andelse
, ie.if(1) something(); else
butif(1) some(); thing() else
so you could basically:if ($1 == ">") count/=count; else
orif ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23
3
3
Remove the
;
between the }
and else
, ie. if(1) something(); else
but if(1) some(); thing() else
so you could basically: if ($1 == ">") count/=count; else
or if ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23
Remove the
;
between the }
and else
, ie. if(1) something(); else
but if(1) some(); thing() else
so you could basically: if ($1 == ">") count/=count; else
or if ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23
add a comment |
1 Answer
1
active
oldest
votes
Your syntax is very close; just a bit off. Looks like there may be some confusion between the curly braces
and the normal parentheses. As you play around more with awk the difference should become much clearer.
Before I get to your particular syntax issue, note that a simpler approach can solve the same problem:
awk -v OFS='t' '$1 == ">" count = 1; next !(count++ % 7)' file
A multiline version your corrected code would be:
if ($1 == ">")
count = 1
else
if (count % 7 == 0)
count += 1
print $0
else
count += 1
As long as a statement is on its own line, you don't need a semicolon. But note that to make it a one-liner, you will need one as shown:
if ($1 == ">") count = 1 else if (count % 7 == 0) count += 1; print $0 else count += 1
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53308102%2fawk-if-else-and-variable-reassignment%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
Your syntax is very close; just a bit off. Looks like there may be some confusion between the curly braces
and the normal parentheses. As you play around more with awk the difference should become much clearer.
Before I get to your particular syntax issue, note that a simpler approach can solve the same problem:
awk -v OFS='t' '$1 == ">" count = 1; next !(count++ % 7)' file
A multiline version your corrected code would be:
if ($1 == ">")
count = 1
else
if (count % 7 == 0)
count += 1
print $0
else
count += 1
As long as a statement is on its own line, you don't need a semicolon. But note that to make it a one-liner, you will need one as shown:
if ($1 == ">") count = 1 else if (count % 7 == 0) count += 1; print $0 else count += 1
add a comment |
Your syntax is very close; just a bit off. Looks like there may be some confusion between the curly braces
and the normal parentheses. As you play around more with awk the difference should become much clearer.
Before I get to your particular syntax issue, note that a simpler approach can solve the same problem:
awk -v OFS='t' '$1 == ">" count = 1; next !(count++ % 7)' file
A multiline version your corrected code would be:
if ($1 == ">")
count = 1
else
if (count % 7 == 0)
count += 1
print $0
else
count += 1
As long as a statement is on its own line, you don't need a semicolon. But note that to make it a one-liner, you will need one as shown:
if ($1 == ">") count = 1 else if (count % 7 == 0) count += 1; print $0 else count += 1
add a comment |
Your syntax is very close; just a bit off. Looks like there may be some confusion between the curly braces
and the normal parentheses. As you play around more with awk the difference should become much clearer.
Before I get to your particular syntax issue, note that a simpler approach can solve the same problem:
awk -v OFS='t' '$1 == ">" count = 1; next !(count++ % 7)' file
A multiline version your corrected code would be:
if ($1 == ">")
count = 1
else
if (count % 7 == 0)
count += 1
print $0
else
count += 1
As long as a statement is on its own line, you don't need a semicolon. But note that to make it a one-liner, you will need one as shown:
if ($1 == ">") count = 1 else if (count % 7 == 0) count += 1; print $0 else count += 1
Your syntax is very close; just a bit off. Looks like there may be some confusion between the curly braces
and the normal parentheses. As you play around more with awk the difference should become much clearer.
Before I get to your particular syntax issue, note that a simpler approach can solve the same problem:
awk -v OFS='t' '$1 == ">" count = 1; next !(count++ % 7)' file
A multiline version your corrected code would be:
if ($1 == ">")
count = 1
else
if (count % 7 == 0)
count += 1
print $0
else
count += 1
As long as a statement is on its own line, you don't need a semicolon. But note that to make it a one-liner, you will need one as shown:
if ($1 == ">") count = 1 else if (count % 7 == 0) count += 1; print $0 else count += 1
edited Nov 15 '18 at 1:20
answered Nov 14 '18 at 23:58
jasjas
7,34621833
7,34621833
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53308102%2fawk-if-else-and-variable-reassignment%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
3
Remove the
;
between the}
andelse
, ie.if(1) something(); else
butif(1) some(); thing() else
so you could basically:if ($1 == ">") count/=count; else
orif ($1 == ">") count/=count else
– James Brown
Nov 14 '18 at 20:23