Search pattern in awk












0















I want to create a script such that it checks the conditions:




  1. Port string should be present in the file and if yes then it's value should be 20000.


  2. 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.










share|improve this question




















  • 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
















0















I want to create a script such that it checks the conditions:




  1. Port string should be present in the file and if yes then it's value should be 20000.


  2. 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.










share|improve this question




















  • 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














0












0








0








I want to create a script such that it checks the conditions:




  1. Port string should be present in the file and if yes then it's value should be 20000.


  2. 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.










share|improve this question
















I want to create a script such that it checks the conditions:




  1. Port string should be present in the file and if yes then it's value should be 20000.


  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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





share|improve this answer
























  • 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
















0














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





share|improve this answer
























  • 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














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith