Bash grep result from command whole linehow to execute lines coming from a grep result?Peculiar piping grep/head behaviorHow to use the grep result in command line?Bash : compare two strings with spaceGrep from a line to a linelimit grep context to a part of the result lineFor a large directory, create a variable of the filenames which include lines which include the text string stored in another variablegrep and output whole wordIssues with using multiple * in the grep commandHow to download a web page content to a text file exactly as the web page is?
Is Big Ben visible from the British museum?
Why use a retrograde orbit?
Why can't I share a one use code with anyone else?
Deleting the same lines from a list
Why is the marginal distribution/marginal probability described as "marginal"?
Single word that parallels "Recent" when discussing the near future
AD: OU for system administrator accounts
Enqueue Queueable class multiple times
What do astronauts do with their trash on the ISS?
Why is vowel phonology represented in a trapezoid instead of a square?
What color to choose as "danger" if the main color of my app is red
Why is it correct to use ~た in this sentence, even though we're talking about next week?
How can we delete item permanently without storing in Recycle Bin?
Omit property variable when using object destructuring
301 Redirects what does ([a-z]+)-(.*) and ([0-9]+)-(.*) mean
Why are there five extra turns in tournament Magic?
Why is the A380’s with-reversers stopping distance the same as its no-reversers stopping distance?
Holding rent money for my friend which amounts to over $10k?
What dog breeds survive the apocalypse for generations?
How could it be that 80% of townspeople were farmers during the Edo period in Japan?
Why does string strummed with finger sound different from the one strummed with pick?
Who is frowning in the sentence "Daisy looked at Tom frowning"?
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
Can a person still be an Orthodox Jew and believe that the Torah contains narratives that are not scientifically correct?
Bash grep result from command whole line
how to execute lines coming from a grep result?Peculiar piping grep/head behaviorHow to use the grep result in command line?Bash : compare two strings with spaceGrep from a line to a linelimit grep context to a part of the result lineFor a large directory, create a variable of the filenames which include lines which include the text string stored in another variablegrep and output whole wordIssues with using multiple * in the grep commandHow to download a web page content to a text file exactly as the web page is?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a script where I want to list usbs, using the command lsblk
.
The command:
$ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb
which results in
sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2
So I want to save the result in a variable to work later, I write
$ usbs=$(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
What I was expecting is that the variable usbs
stores the result in two whole lines like above. But if I run
for i in $usbs[@]; do
echo $i
done
I get the result split into words:
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
Question:
Is there a way in wich, using the grep
command, I can store the result of the command like two whole lines?
Note:
I prefer to know if there's a simple solution instead of dumping the result in a file and then read it.
bash grep lsblk
New contributor
add a comment |
I have a script where I want to list usbs, using the command lsblk
.
The command:
$ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb
which results in
sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2
So I want to save the result in a variable to work later, I write
$ usbs=$(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
What I was expecting is that the variable usbs
stores the result in two whole lines like above. But if I run
for i in $usbs[@]; do
echo $i
done
I get the result split into words:
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
Question:
Is there a way in wich, using the grep
command, I can store the result of the command like two whole lines?
Note:
I prefer to know if there's a simple solution instead of dumping the result in a file and then read it.
bash grep lsblk
New contributor
1
Tryecho "$#usbs[@]"
to see the number of items in theusbs
"array", or"$!usbs[@]"
to list its indices. Or print it withecho "$usbs"
. It is likely storing what you are expecting it to.
– fra-san
3 hours ago
1
Double-quote your variables (and$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.
– roaima
3 hours ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
var=$(...)
is equivalent tovar="$(...)"
– Jesse_b
49 mins ago
add a comment |
I have a script where I want to list usbs, using the command lsblk
.
The command:
$ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb
which results in
sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2
So I want to save the result in a variable to work later, I write
$ usbs=$(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
What I was expecting is that the variable usbs
stores the result in two whole lines like above. But if I run
for i in $usbs[@]; do
echo $i
done
I get the result split into words:
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
Question:
Is there a way in wich, using the grep
command, I can store the result of the command like two whole lines?
Note:
I prefer to know if there's a simple solution instead of dumping the result in a file and then read it.
bash grep lsblk
New contributor
I have a script where I want to list usbs, using the command lsblk
.
The command:
$ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb
which results in
sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2
So I want to save the result in a variable to work later, I write
$ usbs=$(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
What I was expecting is that the variable usbs
stores the result in two whole lines like above. But if I run
for i in $usbs[@]; do
echo $i
done
I get the result split into words:
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
Question:
Is there a way in wich, using the grep
command, I can store the result of the command like two whole lines?
Note:
I prefer to know if there's a simple solution instead of dumping the result in a file and then read it.
bash grep lsblk
bash grep lsblk
New contributor
New contributor
New contributor
asked 3 hours ago
guillermo chamorroguillermo chamorro
1093
1093
New contributor
New contributor
1
Tryecho "$#usbs[@]"
to see the number of items in theusbs
"array", or"$!usbs[@]"
to list its indices. Or print it withecho "$usbs"
. It is likely storing what you are expecting it to.
– fra-san
3 hours ago
1
Double-quote your variables (and$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.
– roaima
3 hours ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
var=$(...)
is equivalent tovar="$(...)"
– Jesse_b
49 mins ago
add a comment |
1
Tryecho "$#usbs[@]"
to see the number of items in theusbs
"array", or"$!usbs[@]"
to list its indices. Or print it withecho "$usbs"
. It is likely storing what you are expecting it to.
– fra-san
3 hours ago
1
Double-quote your variables (and$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.
– roaima
3 hours ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
var=$(...)
is equivalent tovar="$(...)"
– Jesse_b
49 mins ago
1
1
Try
echo "$#usbs[@]"
to see the number of items in the usbs
"array", or "$!usbs[@]"
to list its indices. Or print it with echo "$usbs"
. It is likely storing what you are expecting it to.– fra-san
3 hours ago
Try
echo "$#usbs[@]"
to see the number of items in the usbs
"array", or "$!usbs[@]"
to list its indices. Or print it with echo "$usbs"
. It is likely storing what you are expecting it to.– fra-san
3 hours ago
1
1
Double-quote your variables (and
$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.– roaima
3 hours ago
Double-quote your variables (and
$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.– roaima
3 hours ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
var=$(...)
is equivalent to var="$(...)"
– Jesse_b
49 mins ago
var=$(...)
is equivalent to var="$(...)"
– Jesse_b
49 mins ago
add a comment |
1 Answer
1
active
oldest
votes
This is a good situation to use readarray/mapfile:
readarray -t usbs < <(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
This will create an array with your output where each line is separated into it's own element.
In your case it would make an array like:
usbs=(
'sdb usb Kingston DataTraveler 2.0'
'sdc usb Kingston DT 101 G2'
)
As is you are assigning your entire output to a single variable (not an array) which essentially does this:
usbs='sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2 '
In order to make it an array you would do:
usbs=($(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb))
but this would make each word separated by whitespace into its own element, equivalent to:
usbs=(
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
)
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
);
);
guillermo chamorro is a new contributor. Be nice, and check out our Code of Conduct.
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%2f519140%2fbash-grep-result-from-command-whole-line%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
This is a good situation to use readarray/mapfile:
readarray -t usbs < <(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
This will create an array with your output where each line is separated into it's own element.
In your case it would make an array like:
usbs=(
'sdb usb Kingston DataTraveler 2.0'
'sdc usb Kingston DT 101 G2'
)
As is you are assigning your entire output to a single variable (not an array) which essentially does this:
usbs='sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2 '
In order to make it an array you would do:
usbs=($(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb))
but this would make each word separated by whitespace into its own element, equivalent to:
usbs=(
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
)
add a comment |
This is a good situation to use readarray/mapfile:
readarray -t usbs < <(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
This will create an array with your output where each line is separated into it's own element.
In your case it would make an array like:
usbs=(
'sdb usb Kingston DataTraveler 2.0'
'sdc usb Kingston DT 101 G2'
)
As is you are assigning your entire output to a single variable (not an array) which essentially does this:
usbs='sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2 '
In order to make it an array you would do:
usbs=($(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb))
but this would make each word separated by whitespace into its own element, equivalent to:
usbs=(
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
)
add a comment |
This is a good situation to use readarray/mapfile:
readarray -t usbs < <(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
This will create an array with your output where each line is separated into it's own element.
In your case it would make an array like:
usbs=(
'sdb usb Kingston DataTraveler 2.0'
'sdc usb Kingston DT 101 G2'
)
As is you are assigning your entire output to a single variable (not an array) which essentially does this:
usbs='sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2 '
In order to make it an array you would do:
usbs=($(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb))
but this would make each word separated by whitespace into its own element, equivalent to:
usbs=(
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
)
This is a good situation to use readarray/mapfile:
readarray -t usbs < <(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb)
This will create an array with your output where each line is separated into it's own element.
In your case it would make an array like:
usbs=(
'sdb usb Kingston DataTraveler 2.0'
'sdc usb Kingston DT 101 G2'
)
As is you are assigning your entire output to a single variable (not an array) which essentially does this:
usbs='sdb usb Kingston DataTraveler 2.0
sdc usb Kingston DT 101 G2 '
In order to make it an array you would do:
usbs=($(lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb))
but this would make each word separated by whitespace into its own element, equivalent to:
usbs=(
sdb
usb
Kingston
DataTraveler
2.0
sdc
usb
Kingston
DT
101
G2
)
answered 3 hours ago
Jesse_bJesse_b
15.2k33574
15.2k33574
add a comment |
add a comment |
guillermo chamorro is a new contributor. Be nice, and check out our Code of Conduct.
guillermo chamorro is a new contributor. Be nice, and check out our Code of Conduct.
guillermo chamorro is a new contributor. Be nice, and check out our Code of Conduct.
guillermo chamorro is a new contributor. Be nice, and check out our Code of Conduct.
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%2f519140%2fbash-grep-result-from-command-whole-line%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
1
Try
echo "$#usbs[@]"
to see the number of items in theusbs
"array", or"$!usbs[@]"
to list its indices. Or print it withecho "$usbs"
. It is likely storing what you are expecting it to.– fra-san
3 hours ago
1
Double-quote your variables (and
$(...)
constructs) when you reference them and the shell will keep your whitespace intact. But be aware the shell won't automatically assign array elements based on newlines. It will still be one string, just with a newline in the middle.– roaima
3 hours ago
@Christopher I like your solution but gave me a headache :), because subsecuents comands use the IFS set before... it took me some time to figure what was happening, it does it silently.
– guillermo chamorro
1 hour ago
var=$(...)
is equivalent tovar="$(...)"
– Jesse_b
49 mins ago