How to find text in a file in bash and echo the output and add it to a variable












1















I'm trying to make a bash script that needs to find http://0.0.0.0:3468/*in a bunch of logs in /opt/plex/*.log, display it at the terminal for the user and at same time add it to a variable.



How can I do this?



Example : Print http://0.0.0.0:3468/abc to the terminal, and create a variable in the script called TOKEN=abc.










share|improve this question

























  • @Inian the url is always that. I also want to get everything after /*

    – Freedo
    Oct 16 '17 at 6:51








  • 2





    Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

    – Inian
    Oct 16 '17 at 6:52











  • @Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

    – Freedo
    Oct 16 '17 at 6:54











  • E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

    – Inian
    Oct 16 '17 at 6:57











  • @Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

    – Freedo
    Oct 16 '17 at 6:59


















1















I'm trying to make a bash script that needs to find http://0.0.0.0:3468/*in a bunch of logs in /opt/plex/*.log, display it at the terminal for the user and at same time add it to a variable.



How can I do this?



Example : Print http://0.0.0.0:3468/abc to the terminal, and create a variable in the script called TOKEN=abc.










share|improve this question

























  • @Inian the url is always that. I also want to get everything after /*

    – Freedo
    Oct 16 '17 at 6:51








  • 2





    Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

    – Inian
    Oct 16 '17 at 6:52











  • @Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

    – Freedo
    Oct 16 '17 at 6:54











  • E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

    – Inian
    Oct 16 '17 at 6:57











  • @Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

    – Freedo
    Oct 16 '17 at 6:59
















1












1








1








I'm trying to make a bash script that needs to find http://0.0.0.0:3468/*in a bunch of logs in /opt/plex/*.log, display it at the terminal for the user and at same time add it to a variable.



How can I do this?



Example : Print http://0.0.0.0:3468/abc to the terminal, and create a variable in the script called TOKEN=abc.










share|improve this question
















I'm trying to make a bash script that needs to find http://0.0.0.0:3468/*in a bunch of logs in /opt/plex/*.log, display it at the terminal for the user and at same time add it to a variable.



How can I do this?



Example : Print http://0.0.0.0:3468/abc to the terminal, and create a variable in the script called TOKEN=abc.







bash shell awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 mins ago









Inian

5,3001530




5,3001530










asked Oct 16 '17 at 6:48









FreedoFreedo

450520




450520













  • @Inian the url is always that. I also want to get everything after /*

    – Freedo
    Oct 16 '17 at 6:51








  • 2





    Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

    – Inian
    Oct 16 '17 at 6:52











  • @Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

    – Freedo
    Oct 16 '17 at 6:54











  • E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

    – Inian
    Oct 16 '17 at 6:57











  • @Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

    – Freedo
    Oct 16 '17 at 6:59





















  • @Inian the url is always that. I also want to get everything after /*

    – Freedo
    Oct 16 '17 at 6:51








  • 2





    Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

    – Inian
    Oct 16 '17 at 6:52











  • @Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

    – Freedo
    Oct 16 '17 at 6:54











  • E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

    – Inian
    Oct 16 '17 at 6:57











  • @Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

    – Freedo
    Oct 16 '17 at 6:59



















@Inian the url is always that. I also want to get everything after /*

– Freedo
Oct 16 '17 at 6:51







@Inian the url is always that. I also want to get everything after /*

– Freedo
Oct 16 '17 at 6:51






2




2





Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

– Inian
Oct 16 '17 at 6:52





Can you give some actual examples to come after /* and exactly say what you want to add it to a variable? By showing an example?

– Inian
Oct 16 '17 at 6:52













@Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

– Freedo
Oct 16 '17 at 6:54





@Inian everything after /* is alphanumeric characters, I want to find that in the logs, print it to the user, and create variable TOKEN=/*

– Freedo
Oct 16 '17 at 6:54













E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

– Inian
Oct 16 '17 at 6:57





E.g. if you have http://0.0.0.0:3468/abc, you just want to see abc?

– Inian
Oct 16 '17 at 6:57













@Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

– Freedo
Oct 16 '17 at 6:59







@Inian want to print everything, including the url and what comes after /*, but make a variable only with the output after /*

– Freedo
Oct 16 '17 at 6:59












1 Answer
1






active

oldest

votes


















2














Based on supposed requirements from the comments, you need something like below



#!/usr/bin/env bash

token=()
while IFS= read -r line; do
printf '%sn' "$line"
token+=( "${line##*/}" )
done< <(awk '$0 ~ "http://0.0.0.0:3468/"' /opt/plex/*.log)


This will print all the matching lines containing the URL to the console. Instead of using a variable to store the token output you can use an array in bash to append the contents after / by using parameter expansion syntax of type ${word##*} which removes the string up to the last occurrence of / and prints the remaining string. So once your script is completed, you can print the token list as just



printf '%sn' "${token[@]}"


and access the individual token by looping over the array



for ((i=0; i< ${#token[@]}; i++ )); do 
printf '%sn' "${token[i]}"
done


(or) as just using array indices ${token[0]}, ${token[1]} etc.





If your requirement just boils down to get a single string value from the multiple set of files, just use grep or Awk as



token=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)


(or) with GNU grep as



token=$(grep -oP 'http://0.0.0.0:3468/K.*) /opt/plex_autoscan/*.log





share|improve this answer


























  • Let us continue this discussion in chat.

    – Freedo
    Oct 16 '17 at 7:35












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%2f398338%2fhow-to-find-text-in-a-file-in-bash-and-echo-the-output-and-add-it-to-a-variable%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









2














Based on supposed requirements from the comments, you need something like below



#!/usr/bin/env bash

token=()
while IFS= read -r line; do
printf '%sn' "$line"
token+=( "${line##*/}" )
done< <(awk '$0 ~ "http://0.0.0.0:3468/"' /opt/plex/*.log)


This will print all the matching lines containing the URL to the console. Instead of using a variable to store the token output you can use an array in bash to append the contents after / by using parameter expansion syntax of type ${word##*} which removes the string up to the last occurrence of / and prints the remaining string. So once your script is completed, you can print the token list as just



printf '%sn' "${token[@]}"


and access the individual token by looping over the array



for ((i=0; i< ${#token[@]}; i++ )); do 
printf '%sn' "${token[i]}"
done


(or) as just using array indices ${token[0]}, ${token[1]} etc.





If your requirement just boils down to get a single string value from the multiple set of files, just use grep or Awk as



token=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)


(or) with GNU grep as



token=$(grep -oP 'http://0.0.0.0:3468/K.*) /opt/plex_autoscan/*.log





share|improve this answer


























  • Let us continue this discussion in chat.

    – Freedo
    Oct 16 '17 at 7:35
















2














Based on supposed requirements from the comments, you need something like below



#!/usr/bin/env bash

token=()
while IFS= read -r line; do
printf '%sn' "$line"
token+=( "${line##*/}" )
done< <(awk '$0 ~ "http://0.0.0.0:3468/"' /opt/plex/*.log)


This will print all the matching lines containing the URL to the console. Instead of using a variable to store the token output you can use an array in bash to append the contents after / by using parameter expansion syntax of type ${word##*} which removes the string up to the last occurrence of / and prints the remaining string. So once your script is completed, you can print the token list as just



printf '%sn' "${token[@]}"


and access the individual token by looping over the array



for ((i=0; i< ${#token[@]}; i++ )); do 
printf '%sn' "${token[i]}"
done


(or) as just using array indices ${token[0]}, ${token[1]} etc.





If your requirement just boils down to get a single string value from the multiple set of files, just use grep or Awk as



token=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)


(or) with GNU grep as



token=$(grep -oP 'http://0.0.0.0:3468/K.*) /opt/plex_autoscan/*.log





share|improve this answer


























  • Let us continue this discussion in chat.

    – Freedo
    Oct 16 '17 at 7:35














2












2








2







Based on supposed requirements from the comments, you need something like below



#!/usr/bin/env bash

token=()
while IFS= read -r line; do
printf '%sn' "$line"
token+=( "${line##*/}" )
done< <(awk '$0 ~ "http://0.0.0.0:3468/"' /opt/plex/*.log)


This will print all the matching lines containing the URL to the console. Instead of using a variable to store the token output you can use an array in bash to append the contents after / by using parameter expansion syntax of type ${word##*} which removes the string up to the last occurrence of / and prints the remaining string. So once your script is completed, you can print the token list as just



printf '%sn' "${token[@]}"


and access the individual token by looping over the array



for ((i=0; i< ${#token[@]}; i++ )); do 
printf '%sn' "${token[i]}"
done


(or) as just using array indices ${token[0]}, ${token[1]} etc.





If your requirement just boils down to get a single string value from the multiple set of files, just use grep or Awk as



token=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)


(or) with GNU grep as



token=$(grep -oP 'http://0.0.0.0:3468/K.*) /opt/plex_autoscan/*.log





share|improve this answer















Based on supposed requirements from the comments, you need something like below



#!/usr/bin/env bash

token=()
while IFS= read -r line; do
printf '%sn' "$line"
token+=( "${line##*/}" )
done< <(awk '$0 ~ "http://0.0.0.0:3468/"' /opt/plex/*.log)


This will print all the matching lines containing the URL to the console. Instead of using a variable to store the token output you can use an array in bash to append the contents after / by using parameter expansion syntax of type ${word##*} which removes the string up to the last occurrence of / and prints the remaining string. So once your script is completed, you can print the token list as just



printf '%sn' "${token[@]}"


and access the individual token by looping over the array



for ((i=0; i< ${#token[@]}; i++ )); do 
printf '%sn' "${token[i]}"
done


(or) as just using array indices ${token[0]}, ${token[1]} etc.





If your requirement just boils down to get a single string value from the multiple set of files, just use grep or Awk as



token=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)


(or) with GNU grep as



token=$(grep -oP 'http://0.0.0.0:3468/K.*) /opt/plex_autoscan/*.log






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 16 '17 at 7:54

























answered Oct 16 '17 at 7:05









InianInian

5,3001530




5,3001530













  • Let us continue this discussion in chat.

    – Freedo
    Oct 16 '17 at 7:35



















  • Let us continue this discussion in chat.

    – Freedo
    Oct 16 '17 at 7:35

















Let us continue this discussion in chat.

– Freedo
Oct 16 '17 at 7:35





Let us continue this discussion in chat.

– Freedo
Oct 16 '17 at 7:35


















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%2f398338%2fhow-to-find-text-in-a-file-in-bash-and-echo-the-output-and-add-it-to-a-variable%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

CARDNET

Boot-repair Failure: Unable to locate package grub-common:i386

濃尾地震