Bash: How to store a specific line of CLI output into a file?
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?
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:BLABLABLABLAHow is that possible?
bash shell-script shell scripting output
add a comment |
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?
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:BLABLABLABLAHow is that possible?
bash shell-script shell scripting output
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
add a comment |
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?
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:BLABLABLABLAHow is that possible?
bash shell-script shell scripting output
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?
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:BLABLABLABLAHow is that possible?
bash shell-script shell scripting output
bash shell-script shell scripting output
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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
2
GNU grep can do lookbehind which saves you the nestedsed
.
– 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
add a comment |
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.
1
I suggest youread -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
add a comment |
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 withCOOKIE=
- 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
.
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
add a comment |
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.
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
add a comment |
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
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
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%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
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
2
GNU grep can do lookbehind which saves you the nestedsed
.
– 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
add a comment |
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
2
GNU grep can do lookbehind which saves you the nestedsed
.
– 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
add a comment |
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
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
answered Aug 27 '18 at 10:38
mkmayankmkmayank
46110
46110
2
GNU grep can do lookbehind which saves you the nestedsed
.
– 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
add a comment |
2
GNU grep can do lookbehind which saves you the nestedsed
.
– 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
add a comment |
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.
1
I suggest youread -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
add a comment |
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.
1
I suggest youread -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
add a comment |
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.
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.
edited Aug 27 '18 at 13:26
answered Aug 27 '18 at 10:37
murumuru
1
1
1
I suggest youread -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
add a comment |
1
I suggest youread -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
add a comment |
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 withCOOKIE=
- 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
.
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
add a comment |
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 withCOOKIE=
- 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
.
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
add a comment |
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 withCOOKIE=
- 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
.
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 withCOOKIE=
- 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
.
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
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%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
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
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