7 added 233 characters in body
source | link

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\(ab\|cd\)$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\(ab\|cd\)$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\(ab\|cd\)$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file
6 Rollback to Revision 4
source | link

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep -x "\"^\(ab\|cd\)"$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or:

grep -x "\(ab\|cd\)" file

The same with sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\(ab\|cd\)$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file
5 added 234 characters in body
source | link

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\-x "\(ab\|cd\)$"" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Output will be:

ab
cd
ab

Or (assuming GNU grep or compatible as \| is not a standard BRE operator):

grep "^\(ab\|cd\)$" file

The same with GNU sed:

sed '/^\(ab\|cd\)$/!d' file

You get this result because loop first execute grep ab file and return all occurances of ab in first iteration and after this loop execute grep cd file and return all cd occurances in file.

You do not need a for loop. Try this:

grep -e "^ab$" -e "^cd$" file

Or use -x option to select only those matches that exactly match the whole line (from man grep, thx to Kusalananda):

grep -x -e "ab" -e "cd" file

Output will be:

ab
cd
ab

Or:

grep -x "\(ab\|cd\)" file

The same with sed:

sed '/^\(ab\|cd\)$/!d' file
4 added 79 characters in body
source | link
3 added 59 characters in body
source | link
2 added 59 characters in body
source | link
1
source | link