Awk: if and conditional statement in same block
Clash Royale CLAN TAG#URR8PPP
Here is the code for a script where I must check the filename to be 'pretempsc.cfg' and print its contents as it is. . For any other file only should I modify lines starting with 'abc disable ...' to 'no abc ...'.
#!/bin/sh
IN_FILE=$1
OUT_FILE=$2
WhatFileIsIt()
awk -v filename=$IN_FILE '
BEGIN
scnode = 0;
if (filename == "pretempsc.cfg")
scnode = 1;
if (!scnode)
/^abc disable/
print "no abc "$4"";
next;
print;
else print
'
}
cat $IN_FILE | WhatFileIsIt | cat > $OUT_FILE
On executing this script, I get the following error:
awk: cmd. line:9: /^abc disable/
Here is the code for a script where I must check the filename to be 'pretempsc.cfg' and print its contents as it is. . For any other file only should I modify lines starting with 'abc disable ...' to 'no abc ...'.
#!/bin/sh
IN_FILE=$1
OUT_FILE=$2
WhatFileIsIt()
awk -v filename=$IN_FILE '
BEGIN
scnode = 0;
if (filename == "pretempsc.cfg")
scnode = 1;
if (!scnode)
/^abc disable/
print "no abc "$4"";
next;
print;
else print
'
cat $IN_FILE | WhatFileIsIt | cat > $OUT_FILE
On executing this script, I get the following error:
awk: cmd. line:9: /^abc disable/
Here is the code for a script where I must check the filename to be 'pretempsc.cfg' and print its contents as it is. . For any other file only should I modify lines starting with 'abc disable ...' to 'no abc ...'.
#!/bin/sh
IN_FILE=$1
OUT_FILE=$2
WhatFileIsIt()
awk -v filename=$IN_FILE '
BEGIN
scnode = 0;
if (filename == "pretempsc.cfg")
scnode = 1;
if (!scnode)
/^abc disable/
print "no abc "$4"";
next;
print;
else print
'
cat $IN_FILE | WhatFileIsIt | cat > $OUT_FILE
On executing this script, I get the following error:
awk: cmd. line:9: /^abc disable/ improve this question
Here is the code for a script where I must check the filename to be 'pretempsc.cfg' and print its contents as it is. . For any other file only should I modify lines starting with 'abc disable ...' to 'no abc ...'.
#!/bin/sh
IN_FILE=$1
OUT_FILE=$2
WhatFileIsIt()
awk -v filename=$IN_FILE '
BEGIN
scnode = 0;
if (filename == "pretempsc.cfg")
scnode = 1;
if (!scnode)
/^abc disable/
print "no abc "$4"";
next;
print;
else print
'
cat $IN_FILE | WhatFileIsIt | cat > $OUT_FILE
On executing this script, I get the following error:
awk: cmd. line:9: /^abc disable/ {
awk: cmd. line:9: ^ syntax error
awk: cmd. line:16: else print
awk: cmd. line:16: ^ syntax error
From what I could look up, I suspect I am wrong in the use of if and condition-action within the action block, but I am unable to figure out what is wrong exactly.
To note: I MUST use awk within the shell script, and there are a lot of other functions similar to WhatFileIsIt doing there own processing on the IN_FILE.
awk scripting
awk scripting
edited Jan 14 at 0:47
Rui F Ribeiro
39.7k1479132
39.7k1479132
asked Sep 22 '16 at 14:28
StudentForLifeStudentForLife
112
112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yep, you need to change that pattern match to a full if
statement:
if (/^abc disable/)
print "no abc "$4"";
next;
(/pattern/
is shorthand for $0 ~ /pattern/
, where $0
contains the whole input line.)
Note that the print
there prints only the fourth column of the input after no abc
, so abc disable foo bar doo
turns into just no abc bar
.
I'm not sure if this is what you wanted.
While we're at it, some other things spring to mind... Feel free to ignore any of this, if they are too obvious or conflict with the rest of your script. (And let's hope I didn't make too many mistakes.)
I think there's an extra right brace at the end of the print
, and it seems the indentation of the deepest condition is a bit off, so a little rewrite:
if (!scnode)
if (/^abc disable/)
print "no abc " $4;
next;
print;
else
print;
But from here, it seems that the only time something special is done is when both !scnode
is true and /^abc disable/
matches, in all other cases there's just the print
. So we could combine the conditions with &&
(Of course the separation between different types of files isn't that clear any more.):
if (!scnode && /^abc disable/)
print "no abc "$4"";
next;
else
print;
Since there's a next
that cuts the execution short, the final print
can stand without the else
clause, and in fact since the whole code block is just an if
, we could drop the condition to the main level and follow up with an unconditional default action of printing.
!scnode && /^abc disable/
print "no abc "$4"";
next;
1;
(Of course this may look a bit too condensed now.)
Also, in the shell script, you don't need to bother the cats, better leave them sleeping and use just the shell for redirection. (And quote the shell variables.)
WhatFileIsIt < "$IN_FILE" > "$OUT_FILE"
The name of that function is a bit confusing, it doesn't really answer any "what"-question, but processes a file. Maybe something like ProcessFile
?
And well, talking about functions, that function uses the variable IN_FILE
, which is not local to it. May be confusing if there's ever need to run a function for two different files. Like the shell script itself, the function can also take parameters, calling MyFunction foo
, makes $1
contain foo
inside the function..
So I'd maybe do something like
ProcessFile() {
awk < "$1" -v filename="$1" '
[...]
(It doesn't matter if you put the input redirection in the middle or in the end of the awk command line.)
Use with:
ProcessFile "$IN_FILE" > "$OUT_FILE"
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%2f311628%2fawk-if-and-conditional-statement-in-same-block%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
Yep, you need to change that pattern match to a full if
statement:
if (/^abc disable/)
print "no abc "$4"";
next;
(/pattern/
is shorthand for $0 ~ /pattern/
, where $0
contains the whole input line.)
Note that the print
there prints only the fourth column of the input after no abc
, so abc disable foo bar doo
turns into just no abc bar
.
I'm not sure if this is what you wanted.
While we're at it, some other things spring to mind... Feel free to ignore any of this, if they are too obvious or conflict with the rest of your script. (And let's hope I didn't make too many mistakes.)
I think there's an extra right brace at the end of the print
, and it seems the indentation of the deepest condition is a bit off, so a little rewrite:
if (!scnode)
if (/^abc disable/)
print "no abc " $4;
next;
print;
else
print;
But from here, it seems that the only time something special is done is when both !scnode
is true and /^abc disable/
matches, in all other cases there's just the print
. So we could combine the conditions with &&
(Of course the separation between different types of files isn't that clear any more.):
if (!scnode && /^abc disable/)
print "no abc "$4"";
next;
else
print;
Since there's a next
that cuts the execution short, the final print
can stand without the else
clause, and in fact since the whole code block is just an if
, we could drop the condition to the main level and follow up with an unconditional default action of printing.
!scnode && /^abc disable/
print "no abc "$4"";
next;
1;
(Of course this may look a bit too condensed now.)
Also, in the shell script, you don't need to bother the cats, better leave them sleeping and use just the shell for redirection. (And quote the shell variables.)
WhatFileIsIt < "$IN_FILE" > "$OUT_FILE"
The name of that function is a bit confusing, it doesn't really answer any "what"-question, but processes a file. Maybe something like ProcessFile
?
And well, talking about functions, that function uses the variable IN_FILE
, which is not local to it. May be confusing if there's ever need to run a function for two different files. Like the shell script itself, the function can also take parameters, calling MyFunction foo
, makes $1
contain foo
inside the function..
So I'd maybe do something like
ProcessFile() {
awk < "$1" -v filename="$1" '
[...]
(It doesn't matter if you put the input redirection in the middle or in the end of the awk command line.)
Use with:
ProcessFile "$IN_FILE" > "$OUT_FILE"
add a comment |
Yep, you need to change that pattern match to a full if
statement:
if (/^abc disable/)
print "no abc "$4"";
next;
(/pattern/
is shorthand for $0 ~ /pattern/
, where $0
contains the whole input line.)
Note that the print
there prints only the fourth column of the input after no abc
, so abc disable foo bar doo
turns into just no abc bar
.
I'm not sure if this is what you wanted.
While we're at it, some other things spring to mind... Feel free to ignore any of this, if they are too obvious or conflict with the rest of your script. (And let's hope I didn't make too many mistakes.)
I think there's an extra right brace at the end of the print
, and it seems the indentation of the deepest condition is a bit off, so a little rewrite:
if (!scnode)
if (/^abc disable/)
print "no abc " $4;
next;
print;
else
print;
But from here, it seems that the only time something special is done is when both !scnode
is true and /^abc disable/
matches, in all other cases there's just the print
. So we could combine the conditions with &&
(Of course the separation between different types of files isn't that clear any more.):
if (!scnode && /^abc disable/)
print "no abc "$4"";
next;
else
print;
Since there's a next
that cuts the execution short, the final print
can stand without the else
clause, and in fact since the whole code block is just an if
, we could drop the condition to the main level and follow up with an unconditional default action of printing.
!scnode && /^abc disable/
print "no abc "$4"";
next;
1;
(Of course this may look a bit too condensed now.)
Also, in the shell script, you don't need to bother the cats, better leave them sleeping and use just the shell for redirection. (And quote the shell variables.)
WhatFileIsIt < "$IN_FILE" > "$OUT_FILE"
The name of that function is a bit confusing, it doesn't really answer any "what"-question, but processes a file. Maybe something like ProcessFile
?
And well, talking about functions, that function uses the variable IN_FILE
, which is not local to it. May be confusing if there's ever need to run a function for two different files. Like the shell script itself, the function can also take parameters, calling MyFunction foo
, makes $1
contain foo
inside the function..
So I'd maybe do something like
ProcessFile() {
awk < "$1" -v filename="$1" '
[...]
(It doesn't matter if you put the input redirection in the middle or in the end of the awk command line.)
Use with:
ProcessFile "$IN_FILE" > "$OUT_FILE"
add a comment |
Yep, you need to change that pattern match to a full if
statement:
if (/^abc disable/)
print "no abc "$4"";
next;
(/pattern/
is shorthand for $0 ~ /pattern/
, where $0
contains the whole input line.)
Note that the print
there prints only the fourth column of the input after no abc
, so abc disable foo bar doo
turns into just no abc bar
.
I'm not sure if this is what you wanted.
While we're at it, some other things spring to mind... Feel free to ignore any of this, if they are too obvious or conflict with the rest of your script. (And let's hope I didn't make too many mistakes.)
I think there's an extra right brace at the end of the print
, and it seems the indentation of the deepest condition is a bit off, so a little rewrite:
if (!scnode)
if (/^abc disable/)
print "no abc " $4;
next;
print;
else
print;
But from here, it seems that the only time something special is done is when both !scnode
is true and /^abc disable/
matches, in all other cases there's just the print
. So we could combine the conditions with &&
(Of course the separation between different types of files isn't that clear any more.):
if (!scnode && /^abc disable/)
print "no abc "$4"";
next;
else
print;
Since there's a next
that cuts the execution short, the final print
can stand without the else
clause, and in fact since the whole code block is just an if
, we could drop the condition to the main level and follow up with an unconditional default action of printing.
!scnode && /^abc disable/
print "no abc "$4"";
next;
1;
(Of course this may look a bit too condensed now.)
Also, in the shell script, you don't need to bother the cats, better leave them sleeping and use just the shell for redirection. (And quote the shell variables.)
WhatFileIsIt < "$IN_FILE" > "$OUT_FILE"
The name of that function is a bit confusing, it doesn't really answer any "what"-question, but processes a file. Maybe something like ProcessFile
?
And well, talking about functions, that function uses the variable IN_FILE
, which is not local to it. May be confusing if there's ever need to run a function for two different files. Like the shell script itself, the function can also take parameters, calling MyFunction foo
, makes $1
contain foo
inside the function..
So I'd maybe do something like
ProcessFile() {
awk < "$1" -v filename="$1" '
[...]
(It doesn't matter if you put the input redirection in the middle or in the end of the awk command line.)
Use with:
ProcessFile "$IN_FILE" > "$OUT_FILE"
Yep, you need to change that pattern match to a full if
statement:
if (/^abc disable/)
print "no abc "$4"";
next;
(/pattern/
is shorthand for $0 ~ /pattern/
, where $0
contains the whole input line.)
Note that the print
there prints only the fourth column of the input after no abc
, so abc disable foo bar doo
turns into just no abc bar
.
I'm not sure if this is what you wanted.
While we're at it, some other things spring to mind... Feel free to ignore any of this, if they are too obvious or conflict with the rest of your script. (And let's hope I didn't make too many mistakes.)
I think there's an extra right brace at the end of the print
, and it seems the indentation of the deepest condition is a bit off, so a little rewrite:
if (!scnode)
if (/^abc disable/)
print "no abc " $4;
next;
print;
else
print;
But from here, it seems that the only time something special is done is when both !scnode
is true and /^abc disable/
matches, in all other cases there's just the print
. So we could combine the conditions with &&
(Of course the separation between different types of files isn't that clear any more.):
if (!scnode && /^abc disable/)
print "no abc "$4"";
next;
else
print;
Since there's a next
that cuts the execution short, the final print
can stand without the else
clause, and in fact since the whole code block is just an if
, we could drop the condition to the main level and follow up with an unconditional default action of printing.
!scnode && /^abc disable/
print "no abc "$4"";
next;
1;
(Of course this may look a bit too condensed now.)
Also, in the shell script, you don't need to bother the cats, better leave them sleeping and use just the shell for redirection. (And quote the shell variables.)
WhatFileIsIt < "$IN_FILE" > "$OUT_FILE"
The name of that function is a bit confusing, it doesn't really answer any "what"-question, but processes a file. Maybe something like ProcessFile
?
And well, talking about functions, that function uses the variable IN_FILE
, which is not local to it. May be confusing if there's ever need to run a function for two different files. Like the shell script itself, the function can also take parameters, calling MyFunction foo
, makes $1
contain foo
inside the function..
So I'd maybe do something like
ProcessFile() {
awk < "$1" -v filename="$1" '
[...]
(It doesn't matter if you put the input redirection in the middle or in the end of the awk command line.)
Use with:
ProcessFile "$IN_FILE" > "$OUT_FILE"
answered Sep 22 '16 at 18:09
ilkkachuilkkachu
57.3k786159
57.3k786159
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%2f311628%2fawk-if-and-conditional-statement-in-same-block%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