turn awk command into an awk script (multiple F)
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a working command
awk -F '[)#/(:]' 'BEGIN fw="";dev="" if ($0~/sh failover/) fw=$1 ; if (($0~/This host/)' OFS="|" test_data
I would like to turn it into a script. When doing so...
#!/bin/sh
awk '
BEGIN ";fw="";dev=""
' test_data
...the F='[)#/(:]' results in an error.
[...srv01]$ ./test
./test: line 3: syntax error near unexpected token `)'
./test: line 3: `BEGIN ";fw="";dev="" '
[...srv01]$
When changing to double quotes it takes everything between the twp double quotes as separator so will look for )#/(: instead of ) or # or / or ( or :
Here is the file content of test_data
[...srv01]$ cat test_data
JoeASA# sh failover | i ):|host
This host: Primary - Active
admin Interface management (313.13.0.13): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.7): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.7): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.7): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.7): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.27): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.27): Normal (Not-Monitored)
GW Interface management (331.1.1.47): Normal (Not-Monitored)
Other host: Secondary - Standby Ready
admin Interface management (313.13.0.12): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.6): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.6): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.6): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.6): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.26): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.26): Normal (Not-Monitored)
GW Interface management (331.1.1.46): Normal (Not-Monitored)
SIMPLEASA1/sec/act# sh failover | i ):|host
This host: Secondary - Active
Interface Edge (912.168.22.17): Normal (Monitored)
Interface Inside (310.13.19.17): Normal (Monitored)
Interface EXT (912.168.50.17): Normal (Monitored)
Interface WIFI (912.168.11.17): Normal (Monitored)
Other host: Primary - Standby Ready
Interface Edge (912.168.22.16): Normal (Monitored)
Interface Inside (310.13.19.16): Normal (Monitored)
Interface EXT (912.168.50.16): Normal (Monitored)
Interface WIFI (912.168.11.16): Normal (Monitored)
[..srv01]$
awk scripting
add a comment |
I have a working command
awk -F '[)#/(:]' 'BEGIN fw="";dev="" if ($0~/sh failover/) fw=$1 ; if (($0~/This host/)' OFS="|" test_data
I would like to turn it into a script. When doing so...
#!/bin/sh
awk '
BEGIN ";fw="";dev=""
' test_data
...the F='[)#/(:]' results in an error.
[...srv01]$ ./test
./test: line 3: syntax error near unexpected token `)'
./test: line 3: `BEGIN ";fw="";dev="" '
[...srv01]$
When changing to double quotes it takes everything between the twp double quotes as separator so will look for )#/(: instead of ) or # or / or ( or :
Here is the file content of test_data
[...srv01]$ cat test_data
JoeASA# sh failover | i ):|host
This host: Primary - Active
admin Interface management (313.13.0.13): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.7): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.7): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.7): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.7): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.27): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.27): Normal (Not-Monitored)
GW Interface management (331.1.1.47): Normal (Not-Monitored)
Other host: Secondary - Standby Ready
admin Interface management (313.13.0.12): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.6): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.6): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.6): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.6): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.26): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.26): Normal (Not-Monitored)
GW Interface management (331.1.1.46): Normal (Not-Monitored)
SIMPLEASA1/sec/act# sh failover | i ):|host
This host: Secondary - Active
Interface Edge (912.168.22.17): Normal (Monitored)
Interface Inside (310.13.19.17): Normal (Monitored)
Interface EXT (912.168.50.17): Normal (Monitored)
Interface WIFI (912.168.11.17): Normal (Monitored)
Other host: Primary - Standby Ready
Interface Edge (912.168.22.16): Normal (Monitored)
Interface Inside (310.13.19.16): Normal (Monitored)
Interface EXT (912.168.50.16): Normal (Monitored)
Interface WIFI (912.168.11.16): Normal (Monitored)
[..srv01]$
awk scripting
2
Using double quotes is correct, but the input field separator variable isFS
, notF
. It's unclear why you suddenly setFS
to a newline and why you setRS
to an empty string. This is not what your first command uses.
– Kusalananda♦
Mar 17 at 16:35
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43
add a comment |
I have a working command
awk -F '[)#/(:]' 'BEGIN fw="";dev="" if ($0~/sh failover/) fw=$1 ; if (($0~/This host/)' OFS="|" test_data
I would like to turn it into a script. When doing so...
#!/bin/sh
awk '
BEGIN ";fw="";dev=""
' test_data
...the F='[)#/(:]' results in an error.
[...srv01]$ ./test
./test: line 3: syntax error near unexpected token `)'
./test: line 3: `BEGIN ";fw="";dev="" '
[...srv01]$
When changing to double quotes it takes everything between the twp double quotes as separator so will look for )#/(: instead of ) or # or / or ( or :
Here is the file content of test_data
[...srv01]$ cat test_data
JoeASA# sh failover | i ):|host
This host: Primary - Active
admin Interface management (313.13.0.13): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.7): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.7): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.7): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.7): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.27): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.27): Normal (Not-Monitored)
GW Interface management (331.1.1.47): Normal (Not-Monitored)
Other host: Secondary - Standby Ready
admin Interface management (313.13.0.12): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.6): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.6): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.6): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.6): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.26): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.26): Normal (Not-Monitored)
GW Interface management (331.1.1.46): Normal (Not-Monitored)
SIMPLEASA1/sec/act# sh failover | i ):|host
This host: Secondary - Active
Interface Edge (912.168.22.17): Normal (Monitored)
Interface Inside (310.13.19.17): Normal (Monitored)
Interface EXT (912.168.50.17): Normal (Monitored)
Interface WIFI (912.168.11.17): Normal (Monitored)
Other host: Primary - Standby Ready
Interface Edge (912.168.22.16): Normal (Monitored)
Interface Inside (310.13.19.16): Normal (Monitored)
Interface EXT (912.168.50.16): Normal (Monitored)
Interface WIFI (912.168.11.16): Normal (Monitored)
[..srv01]$
awk scripting
I have a working command
awk -F '[)#/(:]' 'BEGIN fw="";dev="" if ($0~/sh failover/) fw=$1 ; if (($0~/This host/)' OFS="|" test_data
I would like to turn it into a script. When doing so...
#!/bin/sh
awk '
BEGIN ";fw="";dev=""
' test_data
...the F='[)#/(:]' results in an error.
[...srv01]$ ./test
./test: line 3: syntax error near unexpected token `)'
./test: line 3: `BEGIN ";fw="";dev="" '
[...srv01]$
When changing to double quotes it takes everything between the twp double quotes as separator so will look for )#/(: instead of ) or # or / or ( or :
Here is the file content of test_data
[...srv01]$ cat test_data
JoeASA# sh failover | i ):|host
This host: Primary - Active
admin Interface management (313.13.0.13): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.7): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.7): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.7): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.7): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.27): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.27): Normal (Not-Monitored)
GW Interface management (331.1.1.47): Normal (Not-Monitored)
Other host: Secondary - Standby Ready
admin Interface management (313.13.0.12): Normal (Monitored)
DMZ-FW Interface Inside (310.13.19.6): Normal (Not-Monitored)
DMZ-FW Interface Outside-Zone2 (912.168.119.6): Normal (Not-Monitored)
ENET Interface OUTSIDE(912.168.191.6): Normal (Not-Monitored)
ENET Interface dmarc (912.168.192.6): Normal (Not-Monitored)
GW Interface Extranet (912.168.23.26): Normal (Not-Monitored)
GW Interface Outside-Zone (912.168.123.26): Normal (Not-Monitored)
GW Interface management (331.1.1.46): Normal (Not-Monitored)
SIMPLEASA1/sec/act# sh failover | i ):|host
This host: Secondary - Active
Interface Edge (912.168.22.17): Normal (Monitored)
Interface Inside (310.13.19.17): Normal (Monitored)
Interface EXT (912.168.50.17): Normal (Monitored)
Interface WIFI (912.168.11.17): Normal (Monitored)
Other host: Primary - Standby Ready
Interface Edge (912.168.22.16): Normal (Monitored)
Interface Inside (310.13.19.16): Normal (Monitored)
Interface EXT (912.168.50.16): Normal (Monitored)
Interface WIFI (912.168.11.16): Normal (Monitored)
[..srv01]$
awk scripting
awk scripting
edited Mar 18 at 0:38
Rui F Ribeiro
42.1k1483142
42.1k1483142
asked Mar 17 at 16:32
peti27peti27
62
62
2
Using double quotes is correct, but the input field separator variable isFS
, notF
. It's unclear why you suddenly setFS
to a newline and why you setRS
to an empty string. This is not what your first command uses.
– Kusalananda♦
Mar 17 at 16:35
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43
add a comment |
2
Using double quotes is correct, but the input field separator variable isFS
, notF
. It's unclear why you suddenly setFS
to a newline and why you setRS
to an empty string. This is not what your first command uses.
– Kusalananda♦
Mar 17 at 16:35
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43
2
2
Using double quotes is correct, but the input field separator variable is
FS
, not F
. It's unclear why you suddenly set FS
to a newline and why you set RS
to an empty string. This is not what your first command uses.– Kusalananda♦
Mar 17 at 16:35
Using double quotes is correct, but the input field separator variable is
FS
, not F
. It's unclear why you suddenly set FS
to a newline and why you set RS
to an empty string. This is not what your first command uses.– Kusalananda♦
Mar 17 at 16:35
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43
add a comment |
1 Answer
1
active
oldest
votes
You're passing the script to awk as a single-quoted string from the shell, but you seem to have single quotes inside the script, too. They actually end the quoted string:
awk 'BEGIN ";fw="";dev=""
^^^^^^^ not quoted
The shell sees an unquoted )
, which causes a syntax error. Not that you'd want to use single quotes in the awk script anyway, they would be a syntax error in awk. So use double-quotes instead, as you did for the other assignments, they fit inside single-quotes in the shell nicely, and actually work in awk code:
awk 'BEGIN foo="bar"; ...'
Then, note that what the -F
option to awk, does is to set the field separator, which is the variable FS
, not F
. So, you want to have BEGIN { FS="[)#/(:]"; ...
, and you probably don't want to change the default record separator RS
either — at least you didn't change it in your one-liner above.
Also, instead of putting an awk script inside a shell script, you could just skip the shell, and make awk the interpreter of that script directly (assuming your awk is at /usr/bin/awk
):
#!/usr/bin/awk -f
BEGIN FS="[)#/(:]"; OFS="
If that's called ./script.awk
and made executable, you can then run it as ./script.awk filename
, i.e. with the file to process as argument to the script.
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%2f506834%2fturn-awk-command-into-an-awk-script-multiple-f%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
You're passing the script to awk as a single-quoted string from the shell, but you seem to have single quotes inside the script, too. They actually end the quoted string:
awk 'BEGIN ";fw="";dev=""
^^^^^^^ not quoted
The shell sees an unquoted )
, which causes a syntax error. Not that you'd want to use single quotes in the awk script anyway, they would be a syntax error in awk. So use double-quotes instead, as you did for the other assignments, they fit inside single-quotes in the shell nicely, and actually work in awk code:
awk 'BEGIN foo="bar"; ...'
Then, note that what the -F
option to awk, does is to set the field separator, which is the variable FS
, not F
. So, you want to have BEGIN { FS="[)#/(:]"; ...
, and you probably don't want to change the default record separator RS
either — at least you didn't change it in your one-liner above.
Also, instead of putting an awk script inside a shell script, you could just skip the shell, and make awk the interpreter of that script directly (assuming your awk is at /usr/bin/awk
):
#!/usr/bin/awk -f
BEGIN FS="[)#/(:]"; OFS="
If that's called ./script.awk
and made executable, you can then run it as ./script.awk filename
, i.e. with the file to process as argument to the script.
add a comment |
You're passing the script to awk as a single-quoted string from the shell, but you seem to have single quotes inside the script, too. They actually end the quoted string:
awk 'BEGIN ";fw="";dev=""
^^^^^^^ not quoted
The shell sees an unquoted )
, which causes a syntax error. Not that you'd want to use single quotes in the awk script anyway, they would be a syntax error in awk. So use double-quotes instead, as you did for the other assignments, they fit inside single-quotes in the shell nicely, and actually work in awk code:
awk 'BEGIN foo="bar"; ...'
Then, note that what the -F
option to awk, does is to set the field separator, which is the variable FS
, not F
. So, you want to have BEGIN { FS="[)#/(:]"; ...
, and you probably don't want to change the default record separator RS
either — at least you didn't change it in your one-liner above.
Also, instead of putting an awk script inside a shell script, you could just skip the shell, and make awk the interpreter of that script directly (assuming your awk is at /usr/bin/awk
):
#!/usr/bin/awk -f
BEGIN FS="[)#/(:]"; OFS="
If that's called ./script.awk
and made executable, you can then run it as ./script.awk filename
, i.e. with the file to process as argument to the script.
add a comment |
You're passing the script to awk as a single-quoted string from the shell, but you seem to have single quotes inside the script, too. They actually end the quoted string:
awk 'BEGIN ";fw="";dev=""
^^^^^^^ not quoted
The shell sees an unquoted )
, which causes a syntax error. Not that you'd want to use single quotes in the awk script anyway, they would be a syntax error in awk. So use double-quotes instead, as you did for the other assignments, they fit inside single-quotes in the shell nicely, and actually work in awk code:
awk 'BEGIN foo="bar"; ...'
Then, note that what the -F
option to awk, does is to set the field separator, which is the variable FS
, not F
. So, you want to have BEGIN { FS="[)#/(:]"; ...
, and you probably don't want to change the default record separator RS
either — at least you didn't change it in your one-liner above.
Also, instead of putting an awk script inside a shell script, you could just skip the shell, and make awk the interpreter of that script directly (assuming your awk is at /usr/bin/awk
):
#!/usr/bin/awk -f
BEGIN FS="[)#/(:]"; OFS="
If that's called ./script.awk
and made executable, you can then run it as ./script.awk filename
, i.e. with the file to process as argument to the script.
You're passing the script to awk as a single-quoted string from the shell, but you seem to have single quotes inside the script, too. They actually end the quoted string:
awk 'BEGIN ";fw="";dev=""
^^^^^^^ not quoted
The shell sees an unquoted )
, which causes a syntax error. Not that you'd want to use single quotes in the awk script anyway, they would be a syntax error in awk. So use double-quotes instead, as you did for the other assignments, they fit inside single-quotes in the shell nicely, and actually work in awk code:
awk 'BEGIN foo="bar"; ...'
Then, note that what the -F
option to awk, does is to set the field separator, which is the variable FS
, not F
. So, you want to have BEGIN { FS="[)#/(:]"; ...
, and you probably don't want to change the default record separator RS
either — at least you didn't change it in your one-liner above.
Also, instead of putting an awk script inside a shell script, you could just skip the shell, and make awk the interpreter of that script directly (assuming your awk is at /usr/bin/awk
):
#!/usr/bin/awk -f
BEGIN FS="[)#/(:]"; OFS="
If that's called ./script.awk
and made executable, you can then run it as ./script.awk filename
, i.e. with the file to process as argument to the script.
edited Mar 17 at 17:08
answered Mar 17 at 17:02
ilkkachuilkkachu
63.4k10104181
63.4k10104181
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%2f506834%2fturn-awk-command-into-an-awk-script-multiple-f%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
2
Using double quotes is correct, but the input field separator variable is
FS
, notF
. It's unclear why you suddenly setFS
to a newline and why you setRS
to an empty string. This is not what your first command uses.– Kusalananda♦
Mar 17 at 16:35
Thanks! This was it....I needed that clarification with the FS (field separator). RS was just leftover from editing and trying different things.
– peti27
Mar 18 at 0:43