awk - Search for multiple command line arguments in text file
So I have a file called data.dat which contains data about some real estate separated by ';' (which district they're located in, area, price and a brief description).
For example:
13;78;44;garage, balcony, multiple floors
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I am writing a shell script which lists the apartments that fit the search criteria which is given in command line arguments. So for example if I run the program like this:
bash real.sh -search alarm system unfurnished
The output should be:
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I have the following code which can check for the first word after -search:
if test "$1" = "-search"
then awk -v word="$2" 'BEGIN{FS=";"} {if ($4 ~ word) {print $0}}' data.dat;
exit 1;
fi
But I need this program to be able to check for any amount of words after search. How could I do that? I tried using the regex "${@:2}" instead of "$2" but that didn't work.
regex bash shell awk
add a comment |
So I have a file called data.dat which contains data about some real estate separated by ';' (which district they're located in, area, price and a brief description).
For example:
13;78;44;garage, balcony, multiple floors
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I am writing a shell script which lists the apartments that fit the search criteria which is given in command line arguments. So for example if I run the program like this:
bash real.sh -search alarm system unfurnished
The output should be:
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I have the following code which can check for the first word after -search:
if test "$1" = "-search"
then awk -v word="$2" 'BEGIN{FS=";"} {if ($4 ~ word) {print $0}}' data.dat;
exit 1;
fi
But I need this program to be able to check for any amount of words after search. How could I do that? I tried using the regex "${@:2}" instead of "$2" but that didn't work.
regex bash shell awk
for thealarm system
, you want to search the line containsalarm[space]system
oralarm or system
?
– Kent
Nov 16 '18 at 20:12
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56
add a comment |
So I have a file called data.dat which contains data about some real estate separated by ';' (which district they're located in, area, price and a brief description).
For example:
13;78;44;garage, balcony, multiple floors
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I am writing a shell script which lists the apartments that fit the search criteria which is given in command line arguments. So for example if I run the program like this:
bash real.sh -search alarm system unfurnished
The output should be:
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I have the following code which can check for the first word after -search:
if test "$1" = "-search"
then awk -v word="$2" 'BEGIN{FS=";"} {if ($4 ~ word) {print $0}}' data.dat;
exit 1;
fi
But I need this program to be able to check for any amount of words after search. How could I do that? I tried using the regex "${@:2}" instead of "$2" but that didn't work.
regex bash shell awk
So I have a file called data.dat which contains data about some real estate separated by ';' (which district they're located in, area, price and a brief description).
For example:
13;78;44;garage, balcony, multiple floors
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I am writing a shell script which lists the apartments that fit the search criteria which is given in command line arguments. So for example if I run the program like this:
bash real.sh -search alarm system unfurnished
The output should be:
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
I have the following code which can check for the first word after -search:
if test "$1" = "-search"
then awk -v word="$2" 'BEGIN{FS=";"} {if ($4 ~ word) {print $0}}' data.dat;
exit 1;
fi
But I need this program to be able to check for any amount of words after search. How could I do that? I tried using the regex "${@:2}" instead of "$2" but that didn't work.
regex bash shell awk
regex bash shell awk
asked Nov 16 '18 at 19:36
Dóra LászlóDóra László
112
112
for thealarm system
, you want to search the line containsalarm[space]system
oralarm or system
?
– Kent
Nov 16 '18 at 20:12
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56
add a comment |
for thealarm system
, you want to search the line containsalarm[space]system
oralarm or system
?
– Kent
Nov 16 '18 at 20:12
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56
for the
alarm system
, you want to search the line contains alarm[space]system
or alarm or system
?– Kent
Nov 16 '18 at 20:12
for the
alarm system
, you want to search the line contains alarm[space]system
or alarm or system
?– Kent
Nov 16 '18 at 20:12
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56
add a comment |
2 Answers
2
active
oldest
votes
As you commented, you want exactly foo bar
instead of foo or bar
, then you may want to change the argument format, for example, let them be comma separated: foo bar,blah
, this searches foo bar
and blah
. Otherwise you cannot distinguish foo[space]bar
or foo or bar
. Thus, you can do:
awk -F';' -v p="alarm system,unfurnished" 'BEGIN{gsub(",","|",p)} $4~p' file
Sure you can let the p
variable be assigned by a shell variable:
awk -F';' -v p="$search" 'BEGIN{....
add a comment |
searching all the words regardless of order in the fourth field
$ awk -F';' -v words="alarm system unfurnished" '
BEGIN {n=split(words,ks," ")}
{for(i=1;i<=n;i++) if($4!~ks[i]) next}1' file
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
if you're looking for any of the words matched, change the last part to
... if($4~ks[i]) {print; next}}' file
not sure which one you want since the example has no partial match case.
if you want to use the user input directly change to
... words="${*:2}" ...
keeping the rest the same. However not clear you're looking for all words to match or any one.
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be${*:2}
Otherwise, you can dotemp="${@:2}"
and use"$temp"
as script variable.
– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53344293%2fawk-search-for-multiple-command-line-arguments-in-text-file%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
As you commented, you want exactly foo bar
instead of foo or bar
, then you may want to change the argument format, for example, let them be comma separated: foo bar,blah
, this searches foo bar
and blah
. Otherwise you cannot distinguish foo[space]bar
or foo or bar
. Thus, you can do:
awk -F';' -v p="alarm system,unfurnished" 'BEGIN{gsub(",","|",p)} $4~p' file
Sure you can let the p
variable be assigned by a shell variable:
awk -F';' -v p="$search" 'BEGIN{....
add a comment |
As you commented, you want exactly foo bar
instead of foo or bar
, then you may want to change the argument format, for example, let them be comma separated: foo bar,blah
, this searches foo bar
and blah
. Otherwise you cannot distinguish foo[space]bar
or foo or bar
. Thus, you can do:
awk -F';' -v p="alarm system,unfurnished" 'BEGIN{gsub(",","|",p)} $4~p' file
Sure you can let the p
variable be assigned by a shell variable:
awk -F';' -v p="$search" 'BEGIN{....
add a comment |
As you commented, you want exactly foo bar
instead of foo or bar
, then you may want to change the argument format, for example, let them be comma separated: foo bar,blah
, this searches foo bar
and blah
. Otherwise you cannot distinguish foo[space]bar
or foo or bar
. Thus, you can do:
awk -F';' -v p="alarm system,unfurnished" 'BEGIN{gsub(",","|",p)} $4~p' file
Sure you can let the p
variable be assigned by a shell variable:
awk -F';' -v p="$search" 'BEGIN{....
As you commented, you want exactly foo bar
instead of foo or bar
, then you may want to change the argument format, for example, let them be comma separated: foo bar,blah
, this searches foo bar
and blah
. Otherwise you cannot distinguish foo[space]bar
or foo or bar
. Thus, you can do:
awk -F';' -v p="alarm system,unfurnished" 'BEGIN{gsub(",","|",p)} $4~p' file
Sure you can let the p
variable be assigned by a shell variable:
awk -F';' -v p="$search" 'BEGIN{....
edited Nov 16 '18 at 20:31
answered Nov 16 '18 at 20:20
KentKent
144k25153214
144k25153214
add a comment |
add a comment |
searching all the words regardless of order in the fourth field
$ awk -F';' -v words="alarm system unfurnished" '
BEGIN {n=split(words,ks," ")}
{for(i=1;i<=n;i++) if($4!~ks[i]) next}1' file
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
if you're looking for any of the words matched, change the last part to
... if($4~ks[i]) {print; next}}' file
not sure which one you want since the example has no partial match case.
if you want to use the user input directly change to
... words="${*:2}" ...
keeping the rest the same. However not clear you're looking for all words to match or any one.
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be${*:2}
Otherwise, you can dotemp="${@:2}"
and use"$temp"
as script variable.
– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
add a comment |
searching all the words regardless of order in the fourth field
$ awk -F';' -v words="alarm system unfurnished" '
BEGIN {n=split(words,ks," ")}
{for(i=1;i<=n;i++) if($4!~ks[i]) next}1' file
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
if you're looking for any of the words matched, change the last part to
... if($4~ks[i]) {print; next}}' file
not sure which one you want since the example has no partial match case.
if you want to use the user input directly change to
... words="${*:2}" ...
keeping the rest the same. However not clear you're looking for all words to match or any one.
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be${*:2}
Otherwise, you can dotemp="${@:2}"
and use"$temp"
as script variable.
– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
add a comment |
searching all the words regardless of order in the fourth field
$ awk -F';' -v words="alarm system unfurnished" '
BEGIN {n=split(words,ks," ")}
{for(i=1;i<=n;i++) if($4!~ks[i]) next}1' file
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
if you're looking for any of the words matched, change the last part to
... if($4~ks[i]) {print; next}}' file
not sure which one you want since the example has no partial match case.
if you want to use the user input directly change to
... words="${*:2}" ...
keeping the rest the same. However not clear you're looking for all words to match or any one.
searching all the words regardless of order in the fourth field
$ awk -F';' -v words="alarm system unfurnished" '
BEGIN {n=split(words,ks," ")}
{for(i=1;i<=n;i++) if($4!~ks[i]) next}1' file
23;100;25;garden, alarm system, unfurnished
16;97;60;balcony, unfurnished, alarm system
if you're looking for any of the words matched, change the last part to
... if($4~ks[i]) {print; next}}' file
not sure which one you want since the example has no partial match case.
if you want to use the user input directly change to
... words="${*:2}" ...
keeping the rest the same. However not clear you're looking for all words to match or any one.
edited Nov 16 '18 at 20:31
answered Nov 16 '18 at 19:51
karakfakarakfa
48.4k52738
48.4k52738
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be${*:2}
Otherwise, you can dotemp="${@:2}"
and use"$temp"
as script variable.
– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
add a comment |
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be${*:2}
Otherwise, you can dotemp="${@:2}"
and use"$temp"
as script variable.
– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
Thank yout for your answer! Maybe I wasn't clear enough in my description, but I need it to work with any word I input after the -search parameter, not just "alarm system unfurnished". I'll post another example for a better understanding: Input: bash real.sh -search unfurnished balcony Output: 16;97;60;balcony, unfurnished, alarm system I tried your solution replacing the part after 'words=' with "${@:2}" and it works if I only input 1 word, otherwise I get an error: "cannot open file `BEGIN{FS=";"} {n=split(words,ks," ")} {for(i=1;i<=n;i++) if($4~ks[i]) {print; next}}' for reading"
– Dóra László
Nov 16 '18 at 20:15
sorry, it should be
${*:2}
Otherwise, you can do temp="${@:2}"
and use "$temp"
as script variable.– karakfa
Nov 16 '18 at 20:32
sorry, it should be
${*:2}
Otherwise, you can do temp="${@:2}"
and use "$temp"
as script variable.– karakfa
Nov 16 '18 at 20:32
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
Yes, it works perfectly now! Thank you for your patience and help! :)
– Dóra László
Nov 16 '18 at 20:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53344293%2fawk-search-for-multiple-command-line-arguments-in-text-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
for the
alarm system
, you want to search the line containsalarm[space]system
oralarm or system
?– Kent
Nov 16 '18 at 20:12
The alarm[space]system one
– Dóra László
Nov 16 '18 at 20:16
BTW, read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 16 '18 at 20:37
All right, sorry, I'm kind of new here and didn't see that page.
– Dóra László
Nov 17 '18 at 10:56