Search pattern in awk
I want to create a script such that it checks the conditions:
Port string should be present in the file and if yes then it's value should be 20000.
The file should have a mention of sslkey, sslCert, ssl_cipher (The values against these strings/keys can be anything).
The attempt was made:
$ awk '/port|sslKey|sslCert|ssl_cipher/ {print $2,$3}' pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
The problem with the above command is even if one of the strings 'port|sslKey|sslCert|ssl_cipher' is missing even then it runs.
Could this be achieved using only a few lines of awk.
If any of the string/condition is missing then the output should display that condition and also the conditions those are met.
linux bash shell awk
add a comment |
I want to create a script such that it checks the conditions:
Port string should be present in the file and if yes then it's value should be 20000.
The file should have a mention of sslkey, sslCert, ssl_cipher (The values against these strings/keys can be anything).
The attempt was made:
$ awk '/port|sslKey|sslCert|ssl_cipher/ {print $2,$3}' pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
The problem with the above command is even if one of the strings 'port|sslKey|sslCert|ssl_cipher' is missing even then it runs.
Could this be achieved using only a few lines of awk.
If any of the string/condition is missing then the output should display that condition and also the conditions those are met.
linux bash shell awk
2
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36
add a comment |
I want to create a script such that it checks the conditions:
Port string should be present in the file and if yes then it's value should be 20000.
The file should have a mention of sslkey, sslCert, ssl_cipher (The values against these strings/keys can be anything).
The attempt was made:
$ awk '/port|sslKey|sslCert|ssl_cipher/ {print $2,$3}' pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
The problem with the above command is even if one of the strings 'port|sslKey|sslCert|ssl_cipher' is missing even then it runs.
Could this be achieved using only a few lines of awk.
If any of the string/condition is missing then the output should display that condition and also the conditions those are met.
linux bash shell awk
I want to create a script such that it checks the conditions:
Port string should be present in the file and if yes then it's value should be 20000.
The file should have a mention of sslkey, sslCert, ssl_cipher (The values against these strings/keys can be anything).
The attempt was made:
$ awk '/port|sslKey|sslCert|ssl_cipher/ {print $2,$3}' pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
The problem with the above command is even if one of the strings 'port|sslKey|sslCert|ssl_cipher' is missing even then it runs.
Could this be achieved using only a few lines of awk.
If any of the string/condition is missing then the output should display that condition and also the conditions those are met.
linux bash shell awk
linux bash shell awk
edited Nov 20 '18 at 9:30
kvantour
8,49331230
8,49331230
asked Nov 20 '18 at 7:30


Paras PandeyParas Pandey
325
325
2
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36
add a comment |
2
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36
2
2
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36
add a comment |
1 Answer
1
active
oldest
votes
Considering Your details in questions here are input examples:
Correct:
$ cat pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Wrong (port 18000 and missing sslCert):
$ cat pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
AWK solution:
Correct pkg.conf. Returns 0 and outputs nothing:
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg.conf
Wrong pkg_wrong.conf (port 18000 and missing sslCert):
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
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%2f53388169%2fsearch-pattern-in-awk%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
Considering Your details in questions here are input examples:
Correct:
$ cat pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Wrong (port 18000 and missing sslCert):
$ cat pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
AWK solution:
Correct pkg.conf. Returns 0 and outputs nothing:
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg.conf
Wrong pkg_wrong.conf (port 18000 and missing sslCert):
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
add a comment |
Considering Your details in questions here are input examples:
Correct:
$ cat pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Wrong (port 18000 and missing sslCert):
$ cat pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
AWK solution:
Correct pkg.conf. Returns 0 and outputs nothing:
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg.conf
Wrong pkg_wrong.conf (port 18000 and missing sslCert):
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
add a comment |
Considering Your details in questions here are input examples:
Correct:
$ cat pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Wrong (port 18000 and missing sslCert):
$ cat pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
AWK solution:
Correct pkg.conf. Returns 0 and outputs nothing:
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg.conf
Wrong pkg_wrong.conf (port 18000 and missing sslCert):
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Considering Your details in questions here are input examples:
Correct:
$ cat pkg.conf
port 20000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert /usr/product/plat/etc/ssl/server.cert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
Wrong (port 18000 and missing sslCert):
$ cat pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
AWK solution:
Correct pkg.conf. Returns 0 and outputs nothing:
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg.conf
Wrong pkg_wrong.conf (port 18000 and missing sslCert):
$ awk '
/^port [0-9]+$/ { if ( $2 == 20000 ) isport=1; port=$2; }
/^sslKey .+$/ { issslKey=1; sslKey=$2; }
/^sslCert .+$/ { issslCert=1; sslCert=$2; }
/^ssl_cipher .+$/ { isssl_cipher=1; ssl_cipher=$2; }
END { if (isport && issslKey && issslCert && isssl_cipher) exit(0);
else { print("port " port); print("sslKey " sslKey); print("sslCert " sslCert); print("ssl_cipher " ssl_cipher); exit(1); }
}
' pkg_wrong.conf
port 18000
sslKey /usr/product/plat/etc/ssl/server.pem
sslCert
ssl_cipher ECDH+AES128:ECDH+AESGCM:ECDH+AES256:DH+AES:DH+AESGCM:DH+AES256:RSA+AES:RSA+AESGCM:!aNULL:!RC4:!MD5:!DSS:!3DES
answered Nov 20 '18 at 9:01


KubatorKubator
74911
74911
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
add a comment |
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
if port is not equal to 20000 and if any of the four strings port|sslKey|sslCert|ssl_cipher not found then it should show an error like echo -e "Port is 18000 , recommended is 20000 and sslCert is not found in the conf file n"
– Paras Pandey
Nov 20 '18 at 10:06
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
You can add into code those detailed conditions or exactly specify in question next time.
– Kubator
Nov 20 '18 at 10:20
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%2f53388169%2fsearch-pattern-in-awk%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
Not at all clear, please mention 2 simple thins, 1st- Sample of Input_file 2nd- Sample of expected output and let us know then.
– RavinderSingh13
Nov 20 '18 at 7:36