How to search each occurrence in a text file Linux?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have 23 strings to search, I want it returns those that are in the file.
I got the code below:
users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'
Desired output:
User1 is in the file
User2 is not in the file
...
User 23 is in the file
I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.
linux text-processing awk grep
add a comment |Â
up vote
1
down vote
favorite
I have 23 strings to search, I want it returns those that are in the file.
I got the code below:
users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'
Desired output:
User1 is in the file
User2 is not in the file
...
User 23 is in the file
I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.
linux text-processing awk grep
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have 23 strings to search, I want it returns those that are in the file.
I got the code below:
users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'
Desired output:
User1 is in the file
User2 is not in the file
...
User 23 is in the file
I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.
linux text-processing awk grep
I have 23 strings to search, I want it returns those that are in the file.
I got the code below:
users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'
Desired output:
User1 is in the file
User2 is not in the file
...
User 23 is in the file
I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.
linux text-processing awk grep
linux text-processing awk grep
asked Aug 21 at 16:54
Mareyes
13611
13611
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
Try this,
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done
From man
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
1
Change that for loop index from$i
to justi
, and also quote the array"$users[@]"
OTW liable to have issues with variable expansion. Plus thegrep
can be made more robust by invoking it with-w
option to preventgrep
from selecting user11 when it really was looking for user1.
â Rakesh Sharma
Aug 21 at 19:20
add a comment |Â
up vote
4
down vote
Use an array:
users=(User1 User2 User3 User4) # et cetera
for i in "$users[@]"; do
echo -n "$user is "
if grep -q "$user" inputfile; then
echo "present"
else
echo "not present"
fi
done
grep -q
will execute the search but not return any output, allowing you to use it silently in an if
test.
Alternatively, you can put each user on its on line in a file called Users
, and then:
grep -o -f Users inputfile
This will output a list of all users seen. If you want to see both present and absent users, you could:
echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
add a comment |Â
up vote
1
down vote
A further tweak.
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
add a comment |Â
up vote
1
down vote
With only a single scan through the file: this is bash
# the array of user names
users=( User1..23 )
# an array of grep options: ( -e User1 -e User2 ...)
for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "$users[@]"; do
[[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the filen" "$users_present[@]"
printf "%s is NOT in the filen" "$users_absent[@]"
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Try this,
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done
From man
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
1
Change that for loop index from$i
to justi
, and also quote the array"$users[@]"
OTW liable to have issues with variable expansion. Plus thegrep
can be made more robust by invoking it with-w
option to preventgrep
from selecting user11 when it really was looking for user1.
â Rakesh Sharma
Aug 21 at 19:20
add a comment |Â
up vote
2
down vote
accepted
Try this,
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done
From man
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
1
Change that for loop index from$i
to justi
, and also quote the array"$users[@]"
OTW liable to have issues with variable expansion. Plus thegrep
can be made more robust by invoking it with-w
option to preventgrep
from selecting user11 when it really was looking for user1.
â Rakesh Sharma
Aug 21 at 19:20
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Try this,
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done
From man
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
Try this,
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
done
From man
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
edited Aug 21 at 19:22
answered Aug 21 at 17:01
msp9011
3,46643862
3,46643862
1
Change that for loop index from$i
to justi
, and also quote the array"$users[@]"
OTW liable to have issues with variable expansion. Plus thegrep
can be made more robust by invoking it with-w
option to preventgrep
from selecting user11 when it really was looking for user1.
â Rakesh Sharma
Aug 21 at 19:20
add a comment |Â
1
Change that for loop index from$i
to justi
, and also quote the array"$users[@]"
OTW liable to have issues with variable expansion. Plus thegrep
can be made more robust by invoking it with-w
option to preventgrep
from selecting user11 when it really was looking for user1.
â Rakesh Sharma
Aug 21 at 19:20
1
1
Change that for loop index from
$i
to just i
, and also quote the array "$users[@]"
OTW liable to have issues with variable expansion. Plus the grep
can be made more robust by invoking it with -w
option to prevent grep
from selecting user11 when it really was looking for user1.â Rakesh Sharma
Aug 21 at 19:20
Change that for loop index from
$i
to just i
, and also quote the array "$users[@]"
OTW liable to have issues with variable expansion. Plus the grep
can be made more robust by invoking it with -w
option to prevent grep
from selecting user11 when it really was looking for user1.â Rakesh Sharma
Aug 21 at 19:20
add a comment |Â
up vote
4
down vote
Use an array:
users=(User1 User2 User3 User4) # et cetera
for i in "$users[@]"; do
echo -n "$user is "
if grep -q "$user" inputfile; then
echo "present"
else
echo "not present"
fi
done
grep -q
will execute the search but not return any output, allowing you to use it silently in an if
test.
Alternatively, you can put each user on its on line in a file called Users
, and then:
grep -o -f Users inputfile
This will output a list of all users seen. If you want to see both present and absent users, you could:
echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
add a comment |Â
up vote
4
down vote
Use an array:
users=(User1 User2 User3 User4) # et cetera
for i in "$users[@]"; do
echo -n "$user is "
if grep -q "$user" inputfile; then
echo "present"
else
echo "not present"
fi
done
grep -q
will execute the search but not return any output, allowing you to use it silently in an if
test.
Alternatively, you can put each user on its on line in a file called Users
, and then:
grep -o -f Users inputfile
This will output a list of all users seen. If you want to see both present and absent users, you could:
echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Use an array:
users=(User1 User2 User3 User4) # et cetera
for i in "$users[@]"; do
echo -n "$user is "
if grep -q "$user" inputfile; then
echo "present"
else
echo "not present"
fi
done
grep -q
will execute the search but not return any output, allowing you to use it silently in an if
test.
Alternatively, you can put each user on its on line in a file called Users
, and then:
grep -o -f Users inputfile
This will output a list of all users seen. If you want to see both present and absent users, you could:
echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
Use an array:
users=(User1 User2 User3 User4) # et cetera
for i in "$users[@]"; do
echo -n "$user is "
if grep -q "$user" inputfile; then
echo "present"
else
echo "not present"
fi
done
grep -q
will execute the search but not return any output, allowing you to use it silently in an if
test.
Alternatively, you can put each user on its on line in a file called Users
, and then:
grep -o -f Users inputfile
This will output a list of all users seen. If you want to see both present and absent users, you could:
echo "Users present:"
grep -o -f Users inputfile
echo "Users absent:"
grep -vo -f Users inputfile
edited Aug 21 at 17:53
answered Aug 21 at 17:07
DopeGhoti
41.1k55080
41.1k55080
add a comment |Â
add a comment |Â
up vote
1
down vote
A further tweak.
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
add a comment |Â
up vote
1
down vote
A further tweak.
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
add a comment |Â
up vote
1
down vote
up vote
1
down vote
A further tweak.
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
A further tweak.
users=( User1 User2 User3 User4 )
for i in "$users[@]"
do
echo "$i is" $(grep -qw $i file || echo "not") "in the file"
done
answered Aug 21 at 20:06
steve
12.9k22149
12.9k22149
add a comment |Â
add a comment |Â
up vote
1
down vote
With only a single scan through the file: this is bash
# the array of user names
users=( User1..23 )
# an array of grep options: ( -e User1 -e User2 ...)
for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "$users[@]"; do
[[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the filen" "$users_present[@]"
printf "%s is NOT in the filen" "$users_absent[@]"
add a comment |Â
up vote
1
down vote
With only a single scan through the file: this is bash
# the array of user names
users=( User1..23 )
# an array of grep options: ( -e User1 -e User2 ...)
for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "$users[@]"; do
[[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the filen" "$users_present[@]"
printf "%s is NOT in the filen" "$users_absent[@]"
add a comment |Â
up vote
1
down vote
up vote
1
down vote
With only a single scan through the file: this is bash
# the array of user names
users=( User1..23 )
# an array of grep options: ( -e User1 -e User2 ...)
for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "$users[@]"; do
[[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the filen" "$users_present[@]"
printf "%s is NOT in the filen" "$users_absent[@]"
With only a single scan through the file: this is bash
# the array of user names
users=( User1..23 )
# an array of grep options: ( -e User1 -e User2 ...)
for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
# scan the input file and save the user names that are present in the file
readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
# find the user names absent from the file
# this assumes there are no spaces in any of the user names.
for u in "$users[@]"; do
[[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
done
# and print out the results
printf "%s is in the filen" "$users_present[@]"
printf "%s is NOT in the filen" "$users_absent[@]"
answered Aug 21 at 20:31
glenn jackman
47.8k365105
47.8k365105
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463918%2fhow-to-search-each-occurrence-in-a-text-file-linux%23new-answer', 'question_page');
);
Post as a guest
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
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
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