I have a set of words which looks like:
fruits.txt
APPLE,2
apple,3
APPLE-ORANGE,3
APPLEORANGE-GRAPE,5
and trying to print "apples" only.
here is what i've tried and almost all the commands with similar problem return the same output as follows:
grep -iE "apple\\b" fruits.txt
APPLE,2
apple,3
APPLE-ORANGE,3 ##this is the extra line which i want to ignore
also i've tried the following, but didn't return anything.
grep -i '^apple$' fruits.txt
EXPECTED OUTPUT
APPLE,2
apples,3
PS: Please don't suggest on grep -iE 'apple\\b' fruits.txt | grep -v "-"
grep -iE 'apple\\b' fruits.txt | grep -v "-"
I can't use this in a loop in case of greping "APPLE-ORANGE", again It'll be a problem.
Try:
grep -i 'apple[ ,]' fruits.txt
grep -iP 'apple\b(?=[\s,])' fruits.txt
from the
APPLE,2
apple,3
APPLE-ORANGE,3
APPLEORANGE-GRAPE,5
applE , 8
AAple900
both prints
APPLE,2
apple,3
applE , 8
Both regexes search for case insensitive apple followed by at least one (space) or
,
.
if you don't want accept (space) just remove the character groups...
Or, you should say to grep
what isn't allowed to follow the apple
, like:
grep -iP 'apple[^\w-]' fruits.txt
grep -iE 'apple[^[:alnum:]-]' fruits.txt
search for apple
what isn't followed by any word character or -
.
You could use awk
for this task:
$ awk -F, -v IGNORECASE=1 '$1 ~ /^apple$/' fruits.txt
APPLE,2
apple,3
If you want to pass awk
a shell variable to be used, you could do this:
$ fruit=apple
$ awk -F, -v IGNORECASE=1 -v FRUIT="^$fruit\$" '$1 ~ FRUIT' fruits.txt
APPLE,2
apple,3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.