Save the matches of grep as an array
I have a file with the following contents:
FILETYPE=A:B:C:D
How can I grep out A, B, C and D and save them as an array in a ksh script on Linux? I tried
FILETYPES=`grep "FILETYPE" ${CONF_FILE} | awk -F: '{print $NF}'`
But that only gets the last one.
grep
add a comment |
I have a file with the following contents:
FILETYPE=A:B:C:D
How can I grep out A, B, C and D and save them as an array in a ksh script on Linux? I tried
FILETYPES=`grep "FILETYPE" ${CONF_FILE} | awk -F: '{print $NF}'`
But that only gets the last one.
grep
im using shel ksh
– user548682
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41
add a comment |
I have a file with the following contents:
FILETYPE=A:B:C:D
How can I grep out A, B, C and D and save them as an array in a ksh script on Linux? I tried
FILETYPES=`grep "FILETYPE" ${CONF_FILE} | awk -F: '{print $NF}'`
But that only gets the last one.
grep
I have a file with the following contents:
FILETYPE=A:B:C:D
How can I grep out A, B, C and D and save them as an array in a ksh script on Linux? I tried
FILETYPES=`grep "FILETYPE" ${CONF_FILE} | awk -F: '{print $NF}'`
But that only gets the last one.
grep
grep
edited 1 hour ago
Sparhawk
10.2k74398
10.2k74398
asked Jun 15 '16 at 8:36
user548682user548682
3225
3225
im using shel ksh
– user548682
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41
add a comment |
im using shel ksh
– user548682
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41
im using shel ksh
– user548682
Jun 15 '16 at 8:40
im using shel ksh
– user548682
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41
add a comment |
2 Answers
2
active
oldest
votes
On Linux you can do:
filetypes=( $(grep -Po 'FILETYPE=K.*' ${CONF_FILE} | tr ':' ' ') )
Or, more idiomatically:
IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=K.*' file))
Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array is:
array=( `command` )
or
array=( $( command) )
Then, the command itself is grep with Perl Compatible Regular Expressions (-P), these give us K which means "discard anything matched up to this point. The -o causes grep to only print the matched portion of the line and, combined with the K, makes it only print A:B:C:D here. Finally, the tr replaces the : with spaces which allows the shell to interpret the result as an array:
$ echo ${filetypes[0]}
A
$ echo ${filetypes[3]}
D
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ))
– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. TryGPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).
– terdon♦
Jun 15 '16 at 9:11
|
show 8 more comments
Short answer:
array=( `grep -Po '(?<=FILETYPE=).*$' $CONFIG_FILE | tr ':' ' '` )
Explanation: grep uses a "look behind" assertion to return whatever follows "FILETYPE=".
It'll end up declaring the array as:
array=(A B C D)
Then test:
echo "${array[0]} ${array[1]} ${array[2]} ${array[3]}"
Prints:
A B C D
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%2f289858%2fsave-the-matches-of-grep-as-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
On Linux you can do:
filetypes=( $(grep -Po 'FILETYPE=K.*' ${CONF_FILE} | tr ':' ' ') )
Or, more idiomatically:
IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=K.*' file))
Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array is:
array=( `command` )
or
array=( $( command) )
Then, the command itself is grep with Perl Compatible Regular Expressions (-P), these give us K which means "discard anything matched up to this point. The -o causes grep to only print the matched portion of the line and, combined with the K, makes it only print A:B:C:D here. Finally, the tr replaces the : with spaces which allows the shell to interpret the result as an array:
$ echo ${filetypes[0]}
A
$ echo ${filetypes[3]}
D
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ))
– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. TryGPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).
– terdon♦
Jun 15 '16 at 9:11
|
show 8 more comments
On Linux you can do:
filetypes=( $(grep -Po 'FILETYPE=K.*' ${CONF_FILE} | tr ':' ' ') )
Or, more idiomatically:
IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=K.*' file))
Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array is:
array=( `command` )
or
array=( $( command) )
Then, the command itself is grep with Perl Compatible Regular Expressions (-P), these give us K which means "discard anything matched up to this point. The -o causes grep to only print the matched portion of the line and, combined with the K, makes it only print A:B:C:D here. Finally, the tr replaces the : with spaces which allows the shell to interpret the result as an array:
$ echo ${filetypes[0]}
A
$ echo ${filetypes[3]}
D
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ))
– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. TryGPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).
– terdon♦
Jun 15 '16 at 9:11
|
show 8 more comments
On Linux you can do:
filetypes=( $(grep -Po 'FILETYPE=K.*' ${CONF_FILE} | tr ':' ' ') )
Or, more idiomatically:
IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=K.*' file))
Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array is:
array=( `command` )
or
array=( $( command) )
Then, the command itself is grep with Perl Compatible Regular Expressions (-P), these give us K which means "discard anything matched up to this point. The -o causes grep to only print the matched portion of the line and, combined with the K, makes it only print A:B:C:D here. Finally, the tr replaces the : with spaces which allows the shell to interpret the result as an array:
$ echo ${filetypes[0]}
A
$ echo ${filetypes[3]}
D
On Linux you can do:
filetypes=( $(grep -Po 'FILETYPE=K.*' ${CONF_FILE} | tr ':' ' ') )
Or, more idiomatically:
IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=K.*' file))
Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array is:
array=( `command` )
or
array=( $( command) )
Then, the command itself is grep with Perl Compatible Regular Expressions (-P), these give us K which means "discard anything matched up to this point. The -o causes grep to only print the matched portion of the line and, combined with the K, makes it only print A:B:C:D here. Finally, the tr replaces the : with spaces which allows the shell to interpret the result as an array:
$ echo ${filetypes[0]}
A
$ echo ${filetypes[3]}
D
edited Jun 15 '16 at 15:27
answered Jun 15 '16 at 8:48
terdon♦terdon
131k32258437
131k32258437
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ))
– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. TryGPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).
– terdon♦
Jun 15 '16 at 9:11
|
show 8 more comments
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ))
– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. TryGPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).
– terdon♦
Jun 15 '16 at 9:11
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
I failed to save it as array. Is it ok to give a direct answer?
– user548682
Jun 15 '16 at 8:59
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
@user548682 I don't understand. If you want to post your own answer, yes, it is perfectly OK. I don't see why this would have failed though.
– terdon♦
Jun 15 '16 at 9:01
GPGDIRLISTS=(
( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ) )– user548682
Jun 15 '16 at 9:07
GPGDIRLISTS=(
( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ) )– user548682
Jun 15 '16 at 9:07
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
Wonder why it failed.I used for loop like this, but it couldnt treat it as array. It still list out all items inside. for GPGDIRLIST in $GPGDIRLISTS
– user548682
Jun 15 '16 at 9:09
@user548682 because you added an extra set of parentheses. Try
GPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).– terdon♦
Jun 15 '16 at 9:11
@user548682 because you added an extra set of parentheses. Try
GPGDIRLISTS=( $(grep -Po 'GPGDIRLIST=K.*' ${CONF_FILE} | tr ':' ' ') ).– terdon♦
Jun 15 '16 at 9:11
|
show 8 more comments
Short answer:
array=( `grep -Po '(?<=FILETYPE=).*$' $CONFIG_FILE | tr ':' ' '` )
Explanation: grep uses a "look behind" assertion to return whatever follows "FILETYPE=".
It'll end up declaring the array as:
array=(A B C D)
Then test:
echo "${array[0]} ${array[1]} ${array[2]} ${array[3]}"
Prints:
A B C D
add a comment |
Short answer:
array=( `grep -Po '(?<=FILETYPE=).*$' $CONFIG_FILE | tr ':' ' '` )
Explanation: grep uses a "look behind" assertion to return whatever follows "FILETYPE=".
It'll end up declaring the array as:
array=(A B C D)
Then test:
echo "${array[0]} ${array[1]} ${array[2]} ${array[3]}"
Prints:
A B C D
add a comment |
Short answer:
array=( `grep -Po '(?<=FILETYPE=).*$' $CONFIG_FILE | tr ':' ' '` )
Explanation: grep uses a "look behind" assertion to return whatever follows "FILETYPE=".
It'll end up declaring the array as:
array=(A B C D)
Then test:
echo "${array[0]} ${array[1]} ${array[2]} ${array[3]}"
Prints:
A B C D
Short answer:
array=( `grep -Po '(?<=FILETYPE=).*$' $CONFIG_FILE | tr ':' ' '` )
Explanation: grep uses a "look behind" assertion to return whatever follows "FILETYPE=".
It'll end up declaring the array as:
array=(A B C D)
Then test:
echo "${array[0]} ${array[1]} ${array[2]} ${array[3]}"
Prints:
A B C D
edited Jun 15 '16 at 19:38
answered Jun 15 '16 at 17:10
Bruno Negrão ZicaBruno Negrão Zica
1514
1514
add a comment |
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%2f289858%2fsave-the-matches-of-grep-as-an-array%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
im using shel ksh
– user548682
Jun 15 '16 at 8:40
What OS are you using? Is it Linux or something else?
– terdon♦
Jun 15 '16 at 8:40
Linux, ksh script.
– user548682
Jun 15 '16 at 8:41