Bash: How to store a specific line of CLI output into a file?










7
















  1. Let's assume I receive the following output after executing a bash script in CLI (so this text will be displayed in terminal):



    POST https://mycompany.com/
    COOKIE='BLABLABLABLABLA'
    HOST='ANYIPADDRESS'
    FINGERPRINT='sha256:BLABLABLABLA'


    How can I store the content of COOKIE (only the text between ' and ') into a separate file?





  1. Furthermore, the mentioned text should be pasted into this external file at a specific position.



    The already existing file content looks like that:



    [global]
    Name = Name of VPN connection

    [provider_openconnect]
    Type = OpenConnect
    Name = Name of VPN connection
    Host = IP-address
    Domain = Domain name
    OpenConnect.Cookie = >>>INSERT CONTENT OF THE COOKIE HERE<<<
    OpenConnect.ServerCert = sha256:BLABLABLABLA


    How is that possible?











share|improve this question
























  • I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

    – Jeff Schaller
    Aug 27 '18 at 10:52











  • @JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

    – Dave
    Aug 27 '18 at 11:03















7
















  1. Let's assume I receive the following output after executing a bash script in CLI (so this text will be displayed in terminal):



    POST https://mycompany.com/
    COOKIE='BLABLABLABLABLA'
    HOST='ANYIPADDRESS'
    FINGERPRINT='sha256:BLABLABLABLA'


    How can I store the content of COOKIE (only the text between ' and ') into a separate file?





  1. Furthermore, the mentioned text should be pasted into this external file at a specific position.



    The already existing file content looks like that:



    [global]
    Name = Name of VPN connection

    [provider_openconnect]
    Type = OpenConnect
    Name = Name of VPN connection
    Host = IP-address
    Domain = Domain name
    OpenConnect.Cookie = >>>INSERT CONTENT OF THE COOKIE HERE<<<
    OpenConnect.ServerCert = sha256:BLABLABLABLA


    How is that possible?











share|improve this question
























  • I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

    – Jeff Schaller
    Aug 27 '18 at 10:52











  • @JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

    – Dave
    Aug 27 '18 at 11:03













7












7








7









  1. Let's assume I receive the following output after executing a bash script in CLI (so this text will be displayed in terminal):



    POST https://mycompany.com/
    COOKIE='BLABLABLABLABLA'
    HOST='ANYIPADDRESS'
    FINGERPRINT='sha256:BLABLABLABLA'


    How can I store the content of COOKIE (only the text between ' and ') into a separate file?





  1. Furthermore, the mentioned text should be pasted into this external file at a specific position.



    The already existing file content looks like that:



    [global]
    Name = Name of VPN connection

    [provider_openconnect]
    Type = OpenConnect
    Name = Name of VPN connection
    Host = IP-address
    Domain = Domain name
    OpenConnect.Cookie = >>>INSERT CONTENT OF THE COOKIE HERE<<<
    OpenConnect.ServerCert = sha256:BLABLABLABLA


    How is that possible?











share|improve this question

















  1. Let's assume I receive the following output after executing a bash script in CLI (so this text will be displayed in terminal):



    POST https://mycompany.com/
    COOKIE='BLABLABLABLABLA'
    HOST='ANYIPADDRESS'
    FINGERPRINT='sha256:BLABLABLABLA'


    How can I store the content of COOKIE (only the text between ' and ') into a separate file?





  1. Furthermore, the mentioned text should be pasted into this external file at a specific position.



    The already existing file content looks like that:



    [global]
    Name = Name of VPN connection

    [provider_openconnect]
    Type = OpenConnect
    Name = Name of VPN connection
    Host = IP-address
    Domain = Domain name
    OpenConnect.Cookie = >>>INSERT CONTENT OF THE COOKIE HERE<<<
    OpenConnect.ServerCert = sha256:BLABLABLABLA


    How is that possible?








bash shell-script shell scripting output






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 27 '18 at 10:20







Dave

















asked Aug 27 '18 at 10:10









DaveDave

292217




292217












  • I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

    – Jeff Schaller
    Aug 27 '18 at 10:52











  • @JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

    – Dave
    Aug 27 '18 at 11:03

















  • I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

    – Jeff Schaller
    Aug 27 '18 at 10:52











  • @JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

    – Dave
    Aug 27 '18 at 11:03
















I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

– Jeff Schaller
Aug 27 '18 at 10:52





I assume you're able to re-run the script in order to capture the output again? In other words, you're not trying to "scrape the screen" for that already-passed output?

– Jeff Schaller
Aug 27 '18 at 10:52













@JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

– Dave
Aug 27 '18 at 11:03





@JeffSchaller: Exactly, I can re-run the script as often as I want. The cookie will change every time though, but this does not matter.

– Dave
Aug 27 '18 at 11:03










5 Answers
5






active

oldest

votes


















3














These types of thing are not generic in nature, but specific though approach is generic




I am assuming, you want to replace OpenConnect.Cookie = line with OpenConnect.Cookie = BLABLABLABLABLA



So, to first create required string , you can use



sed -i "s/^OpenConnect.Cookie =.*$/$( command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /")/" external_filename


Here I am using command substitution to first create required string



command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /"


and then substituting required line by this required string



sed -i "s/^OpenConnect.Cookie =.*$/output from above command substitution /" external_filename





share|improve this answer


















  • 2





    GNU grep can do lookbehind which saves you the nested sed.

    – MSalters
    Aug 27 '18 at 15:45











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52


















3














You can read the cookie using a combination of bash's read and grep:



IFS="'" read -r _ cookie _ < <(some-command | grep '^COOKIE')


This uses process substitution to feed the output of some-command | grep '^COOKIE') to read. With IFS="='", we split the input on ', discarding the first element of the split (COOKIE=) (and any remaining text after the closing quote), while saving the second in the cookie variable.



Then we can use sed to replace the text:



sed -i 's/>>>INSERT CONTENT OF THE COOKIE HERE<<</'"$cookie"'/' some-file


This depends on the cookie text not containing special characters like &, though.






share|improve this answer




















  • 1





    I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

    – glenn jackman
    Aug 27 '18 at 12:58











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53


















2














You could use:



. <(command | grep "^COOKIE=")
sed -i "s/(OpenConnect.Cookie)s*=.*/1 = ""$COOKIE""/" file


Where:




  • file is the existing file with contents as described in the question.


  • command is the your command that prints the text to the terminal.


  • grep "^COOKIE=" searches for a line starting with COOKIE=

  • and the dot in the beginning of the command "sources" the output. This means that the output is interpreted as shell code. Thus the variable $COOKIE is set in the current shell.

  • The sed command then replaces the line in the destination file with the contents of the variable $COOKIE.





share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52


















2














How about



sed -f <(CLI command | sed -n '/COOKIE=o047/s///OpenConnect.Cookie =/ s/= .*$/= /; s/.$///p;') file
[global]
Name = Name of VPN connection

[provider_openconnect]
Type = OpenConnect
Name = Name of VPN connection
Host = IP-address
Domain = Domain name
OpenConnect.Cookie = BLABLABLABLABLA
OpenConnect.ServerCert = sha256:BLABLABLABLA


It creates a "sed script file" on the fly by extracting / massageing the relevant data from your CLI command, and executes this script file using "process substitution" in a second sed call.






share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53


















1














This answer is based on @MSalters's comment. The shell used is Bash.



prompt% COOKIE=$(./mycmd | grep -Po "(?<=COOKIE=)'[[:alnum:]]+'" | tr -d ')
prompt% echo "$COOKIE" >/tmp/cookie
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file


Alternative solution (using GNU expr)



This solution works if there is only one matching result.



prompt% COOKIE=$(expr "$(./mycmd | grep COOKIE)" : "COOKIE='([[:alnum:]]+)'[[:space:]]*")
prompt% echo "$COOKIE" >/tmp/file
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file





share|improve this answer

























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53










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



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465042%2fbash-how-to-store-a-specific-line-of-cli-output-into-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














These types of thing are not generic in nature, but specific though approach is generic




I am assuming, you want to replace OpenConnect.Cookie = line with OpenConnect.Cookie = BLABLABLABLABLA



So, to first create required string , you can use



sed -i "s/^OpenConnect.Cookie =.*$/$( command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /")/" external_filename


Here I am using command substitution to first create required string



command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /"


and then substituting required line by this required string



sed -i "s/^OpenConnect.Cookie =.*$/output from above command substitution /" external_filename





share|improve this answer


















  • 2





    GNU grep can do lookbehind which saves you the nested sed.

    – MSalters
    Aug 27 '18 at 15:45











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52















3














These types of thing are not generic in nature, but specific though approach is generic




I am assuming, you want to replace OpenConnect.Cookie = line with OpenConnect.Cookie = BLABLABLABLABLA



So, to first create required string , you can use



sed -i "s/^OpenConnect.Cookie =.*$/$( command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /")/" external_filename


Here I am using command substitution to first create required string



command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /"


and then substituting required line by this required string



sed -i "s/^OpenConnect.Cookie =.*$/output from above command substitution /" external_filename





share|improve this answer


















  • 2





    GNU grep can do lookbehind which saves you the nested sed.

    – MSalters
    Aug 27 '18 at 15:45











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52













3












3








3







These types of thing are not generic in nature, but specific though approach is generic




I am assuming, you want to replace OpenConnect.Cookie = line with OpenConnect.Cookie = BLABLABLABLABLA



So, to first create required string , you can use



sed -i "s/^OpenConnect.Cookie =.*$/$( command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /")/" external_filename


Here I am using command substitution to first create required string



command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /"


and then substituting required line by this required string



sed -i "s/^OpenConnect.Cookie =.*$/output from above command substitution /" external_filename





share|improve this answer













These types of thing are not generic in nature, but specific though approach is generic




I am assuming, you want to replace OpenConnect.Cookie = line with OpenConnect.Cookie = BLABLABLABLABLA



So, to first create required string , you can use



sed -i "s/^OpenConnect.Cookie =.*$/$( command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /")/" external_filename


Here I am using command substitution to first create required string



command_giving_output | grep 'COOKIE=' | sed "s/COOKIE='//; s/'//g; s/^/OpenConnect.Cookie = /"


and then substituting required line by this required string



sed -i "s/^OpenConnect.Cookie =.*$/output from above command substitution /" external_filename






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 27 '18 at 10:38









mkmayankmkmayank

46110




46110







  • 2





    GNU grep can do lookbehind which saves you the nested sed.

    – MSalters
    Aug 27 '18 at 15:45











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52












  • 2





    GNU grep can do lookbehind which saves you the nested sed.

    – MSalters
    Aug 27 '18 at 15:45











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52







2




2





GNU grep can do lookbehind which saves you the nested sed.

– MSalters
Aug 27 '18 at 15:45





GNU grep can do lookbehind which saves you the nested sed.

– MSalters
Aug 27 '18 at 15:45













It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:52





It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:52













3














You can read the cookie using a combination of bash's read and grep:



IFS="'" read -r _ cookie _ < <(some-command | grep '^COOKIE')


This uses process substitution to feed the output of some-command | grep '^COOKIE') to read. With IFS="='", we split the input on ', discarding the first element of the split (COOKIE=) (and any remaining text after the closing quote), while saving the second in the cookie variable.



Then we can use sed to replace the text:



sed -i 's/>>>INSERT CONTENT OF THE COOKIE HERE<<</'"$cookie"'/' some-file


This depends on the cookie text not containing special characters like &, though.






share|improve this answer




















  • 1





    I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

    – glenn jackman
    Aug 27 '18 at 12:58











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53















3














You can read the cookie using a combination of bash's read and grep:



IFS="'" read -r _ cookie _ < <(some-command | grep '^COOKIE')


This uses process substitution to feed the output of some-command | grep '^COOKIE') to read. With IFS="='", we split the input on ', discarding the first element of the split (COOKIE=) (and any remaining text after the closing quote), while saving the second in the cookie variable.



Then we can use sed to replace the text:



sed -i 's/>>>INSERT CONTENT OF THE COOKIE HERE<<</'"$cookie"'/' some-file


This depends on the cookie text not containing special characters like &, though.






share|improve this answer




















  • 1





    I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

    – glenn jackman
    Aug 27 '18 at 12:58











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53













3












3








3







You can read the cookie using a combination of bash's read and grep:



IFS="'" read -r _ cookie _ < <(some-command | grep '^COOKIE')


This uses process substitution to feed the output of some-command | grep '^COOKIE') to read. With IFS="='", we split the input on ', discarding the first element of the split (COOKIE=) (and any remaining text after the closing quote), while saving the second in the cookie variable.



Then we can use sed to replace the text:



sed -i 's/>>>INSERT CONTENT OF THE COOKIE HERE<<</'"$cookie"'/' some-file


This depends on the cookie text not containing special characters like &, though.






share|improve this answer















You can read the cookie using a combination of bash's read and grep:



IFS="'" read -r _ cookie _ < <(some-command | grep '^COOKIE')


This uses process substitution to feed the output of some-command | grep '^COOKIE') to read. With IFS="='", we split the input on ', discarding the first element of the split (COOKIE=) (and any remaining text after the closing quote), while saving the second in the cookie variable.



Then we can use sed to replace the text:



sed -i 's/>>>INSERT CONTENT OF THE COOKIE HERE<<</'"$cookie"'/' some-file


This depends on the cookie text not containing special characters like &, though.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 27 '18 at 13:26

























answered Aug 27 '18 at 10:37









murumuru

1




1







  • 1





    I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

    – glenn jackman
    Aug 27 '18 at 12:58











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53












  • 1





    I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

    – glenn jackman
    Aug 27 '18 at 12:58











  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53







1




1





I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

– glenn jackman
Aug 27 '18 at 12:58





I suggest you read -r _ cookie _ to capture any garbage that might follow the 2nd single quote.

– glenn jackman
Aug 27 '18 at 12:58













It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53





It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53











2














You could use:



. <(command | grep "^COOKIE=")
sed -i "s/(OpenConnect.Cookie)s*=.*/1 = ""$COOKIE""/" file


Where:




  • file is the existing file with contents as described in the question.


  • command is the your command that prints the text to the terminal.


  • grep "^COOKIE=" searches for a line starting with COOKIE=

  • and the dot in the beginning of the command "sources" the output. This means that the output is interpreted as shell code. Thus the variable $COOKIE is set in the current shell.

  • The sed command then replaces the line in the destination file with the contents of the variable $COOKIE.





share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52















2














You could use:



. <(command | grep "^COOKIE=")
sed -i "s/(OpenConnect.Cookie)s*=.*/1 = ""$COOKIE""/" file


Where:




  • file is the existing file with contents as described in the question.


  • command is the your command that prints the text to the terminal.


  • grep "^COOKIE=" searches for a line starting with COOKIE=

  • and the dot in the beginning of the command "sources" the output. This means that the output is interpreted as shell code. Thus the variable $COOKIE is set in the current shell.

  • The sed command then replaces the line in the destination file with the contents of the variable $COOKIE.





share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52













2












2








2







You could use:



. <(command | grep "^COOKIE=")
sed -i "s/(OpenConnect.Cookie)s*=.*/1 = ""$COOKIE""/" file


Where:




  • file is the existing file with contents as described in the question.


  • command is the your command that prints the text to the terminal.


  • grep "^COOKIE=" searches for a line starting with COOKIE=

  • and the dot in the beginning of the command "sources" the output. This means that the output is interpreted as shell code. Thus the variable $COOKIE is set in the current shell.

  • The sed command then replaces the line in the destination file with the contents of the variable $COOKIE.





share|improve this answer













You could use:



. <(command | grep "^COOKIE=")
sed -i "s/(OpenConnect.Cookie)s*=.*/1 = ""$COOKIE""/" file


Where:




  • file is the existing file with contents as described in the question.


  • command is the your command that prints the text to the terminal.


  • grep "^COOKIE=" searches for a line starting with COOKIE=

  • and the dot in the beginning of the command "sources" the output. This means that the output is interpreted as shell code. Thus the variable $COOKIE is set in the current shell.

  • The sed command then replaces the line in the destination file with the contents of the variable $COOKIE.






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 27 '18 at 10:53









chaoschaos

35.7k974117




35.7k974117












  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52

















  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:52
















It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:52





It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:52











2














How about



sed -f <(CLI command | sed -n '/COOKIE=o047/s///OpenConnect.Cookie =/ s/= .*$/= /; s/.$///p;') file
[global]
Name = Name of VPN connection

[provider_openconnect]
Type = OpenConnect
Name = Name of VPN connection
Host = IP-address
Domain = Domain name
OpenConnect.Cookie = BLABLABLABLABLA
OpenConnect.ServerCert = sha256:BLABLABLABLA


It creates a "sed script file" on the fly by extracting / massageing the relevant data from your CLI command, and executes this script file using "process substitution" in a second sed call.






share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53















2














How about



sed -f <(CLI command | sed -n '/COOKIE=o047/s///OpenConnect.Cookie =/ s/= .*$/= /; s/.$///p;') file
[global]
Name = Name of VPN connection

[provider_openconnect]
Type = OpenConnect
Name = Name of VPN connection
Host = IP-address
Domain = Domain name
OpenConnect.Cookie = BLABLABLABLABLA
OpenConnect.ServerCert = sha256:BLABLABLABLA


It creates a "sed script file" on the fly by extracting / massageing the relevant data from your CLI command, and executes this script file using "process substitution" in a second sed call.






share|improve this answer























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53













2












2








2







How about



sed -f <(CLI command | sed -n '/COOKIE=o047/s///OpenConnect.Cookie =/ s/= .*$/= /; s/.$///p;') file
[global]
Name = Name of VPN connection

[provider_openconnect]
Type = OpenConnect
Name = Name of VPN connection
Host = IP-address
Domain = Domain name
OpenConnect.Cookie = BLABLABLABLABLA
OpenConnect.ServerCert = sha256:BLABLABLABLA


It creates a "sed script file" on the fly by extracting / massageing the relevant data from your CLI command, and executes this script file using "process substitution" in a second sed call.






share|improve this answer













How about



sed -f <(CLI command | sed -n '/COOKIE=o047/s///OpenConnect.Cookie =/ s/= .*$/= /; s/.$///p;') file
[global]
Name = Name of VPN connection

[provider_openconnect]
Type = OpenConnect
Name = Name of VPN connection
Host = IP-address
Domain = Domain name
OpenConnect.Cookie = BLABLABLABLABLA
OpenConnect.ServerCert = sha256:BLABLABLABLA


It creates a "sed script file" on the fly by extracting / massageing the relevant data from your CLI command, and executes this script file using "process substitution" in a second sed call.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 27 '18 at 11:55









RudiCRudiC

4,2541312




4,2541312












  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53

















  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53
















It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53





It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53











1














This answer is based on @MSalters's comment. The shell used is Bash.



prompt% COOKIE=$(./mycmd | grep -Po "(?<=COOKIE=)'[[:alnum:]]+'" | tr -d ')
prompt% echo "$COOKIE" >/tmp/cookie
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file


Alternative solution (using GNU expr)



This solution works if there is only one matching result.



prompt% COOKIE=$(expr "$(./mycmd | grep COOKIE)" : "COOKIE='([[:alnum:]]+)'[[:space:]]*")
prompt% echo "$COOKIE" >/tmp/file
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file





share|improve this answer

























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53















1














This answer is based on @MSalters's comment. The shell used is Bash.



prompt% COOKIE=$(./mycmd | grep -Po "(?<=COOKIE=)'[[:alnum:]]+'" | tr -d ')
prompt% echo "$COOKIE" >/tmp/cookie
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file


Alternative solution (using GNU expr)



This solution works if there is only one matching result.



prompt% COOKIE=$(expr "$(./mycmd | grep COOKIE)" : "COOKIE='([[:alnum:]]+)'[[:space:]]*")
prompt% echo "$COOKIE" >/tmp/file
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file





share|improve this answer

























  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53













1












1








1







This answer is based on @MSalters's comment. The shell used is Bash.



prompt% COOKIE=$(./mycmd | grep -Po "(?<=COOKIE=)'[[:alnum:]]+'" | tr -d ')
prompt% echo "$COOKIE" >/tmp/cookie
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file


Alternative solution (using GNU expr)



This solution works if there is only one matching result.



prompt% COOKIE=$(expr "$(./mycmd | grep COOKIE)" : "COOKIE='([[:alnum:]]+)'[[:space:]]*")
prompt% echo "$COOKIE" >/tmp/file
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file





share|improve this answer















This answer is based on @MSalters's comment. The shell used is Bash.



prompt% COOKIE=$(./mycmd | grep -Po "(?<=COOKIE=)'[[:alnum:]]+'" | tr -d ')
prompt% echo "$COOKIE" >/tmp/cookie
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file


Alternative solution (using GNU expr)



This solution works if there is only one matching result.



prompt% COOKIE=$(expr "$(./mycmd | grep COOKIE)" : "COOKIE='([[:alnum:]]+)'[[:space:]]*")
prompt% echo "$COOKIE" >/tmp/file
prompt% sed -i "s:(OpenConnect.Cookie =).*:1 $COOKIE:" file






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 28 '18 at 6:57

























answered Aug 28 '18 at 6:25









FólkvangrFólkvangr

32912




32912












  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53

















  • It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

    – Dave
    Aug 28 '18 at 14:53
















It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53





It is pretty difficult to decide which one of all those great answers deserves the "Answer"-flag. I have chosen the answer with the most upvotes - I hope that is okay for you? Thank you very much for your help!!!

– Dave
Aug 28 '18 at 14:53

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465042%2fbash-how-to-store-a-specific-line-of-cli-output-into-a-file%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)