How to grep the words with space between?

see I have a text like:

27-MAY 14:00 4 aaa 5.30 0.01
27-MAY 14:00 3 aaa 0.85 0.00
27-MAY 14:00 2 aaa 1.09 0.00
27-MAY 14:00 5 aaa 0.03 0.00
27-MAY 14:00 1 aaa 0.00 0.00
27-MAY 14:15 2 aaa 99.11 0.28
27-MAY 14:15 4 aaa 1.06 0.00
27-MAY 14:15 3 aaa 0.55 0.00
27-MAY 14:15 1 aaa 0.00 0.00
27-MAY 14:15 5 aaa 0.01 0.00
27-MAY 14:30 2 aaa 798.64 10.57 ***
27-MAY 14:30 4 aaa 1.48 0.00
27-MAY 14:30 3 aaa 0.16 0.00
27-MAY 14:30 5 aaa 0.03 0.00
27-MAY 14:30 1 aaa 0.00 0.00

I would like the output to be, only the 4columns with 4:

27-MAY 14:00 4 aaa 5.30 0.01
27-MAY 14:15 4 aaa 1.06 0.00
27-MAY 14:30 4 aaa 1.48 0.00

please help, thanks

---------- Post updated at 05:04 PM ---------- Previous update was at 05:01 PM ----------

figured it out

cat text| awk '$3 == "4"'

1 Like

Better

<text awk '$3 == "4"'

or

awk '$3 == "4"' text

cat filename.txt | grep -w "4"

No. The point of MadeInGermany's post is that creating a pipeline using cat to feed input into a utility that can open files directly is a waste of resources. And, even if the utility doesn't open files directly, you can use redirection in the shell instead of creating an unneeded process and reading and writing a file ( cat ) and reading the file again ( grep in this case).

So, rather than using a pipeline for this, use one of the following instead:

grep -w "4" filename.txt
    or
grep -w "4" < filename.txt

which only read the contents of filename.txt once.

Note also that the standards do not specify a -w option for grep , so this suggestion will not work on many systems. And, on systems that do have grep -w , this command will give false positives if the format of the day in the date field doesn't use a leading 0 on the first nine days of the month. That is, both of the commands:

grep -w 4 file
    and
grep -w "4" file

will match all three of the following lines:

 4-MAY 14:00 4 aaa 5.30 0.01 
 4-MAY 14:00 3 aaa 0.85 0.00 
4-MAY 14:00 2 aaa 1.09 0.00

while the commands:

awk '$3 == 4' file
    and
awk '$3 == "4"' file

will only match the 1st line (which is what I believe the original poster wanted).