How to grep the words with space between?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep the words with space between?
# 1  
Old 05-27-2014
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"'
This User Gave Thanks to netbanker For This Post:
# 2  
Old 05-27-2014
Better
Code:
<text awk '$3 == "4"'

or
Code:
awk '$3 == "4"' text

# 3  
Old 05-28-2014
cat filename.txt | grep -w "4"
# 4  
Old 05-28-2014
Quote:
Originally Posted by 24ajay
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:
Code:
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:
Code:
grep -w 4 file
    and
grep -w "4" file

will match all three of the following lines:
Code:
 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:
Code:
awk '$3 == 4' file
    and
awk '$3 == "4"' file

will only match the 1st line (which is what I believe the original poster wanted).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and repllace of strings with space between words

Dear all, I have gone through all the search and replace requests but none of them meet my particular need. I have a huge file in which all Unicode characters are stored as Names. A sample is given below. I want to replace strings in that file with a mapper from another file termed as master.dic. ... (4 Replies)
Discussion started by: gimley
4 Replies

2. UNIX for Dummies Questions & Answers

Grep words with X doubles only

Hi! I'm trying to figure out how to find words with X number of doubles, only. I'm searching a dictionary, (one word per line). For instance, if you want to find words containing only one pair of double letters, you could do something like this: egrep '(.)\1' wordlist.txt |egrep -v '(.)\1.*(.)\2'... (3 Replies)
Discussion started by: sudon't
3 Replies

3. Shell Programming and Scripting

Split a free form text delimited by space to words with other fields

Hi, I need your help for below with shell scripting or perl I/P key, Sentence customer1, I am David customer2, I am Taylor O/P Key, Words Customer1,I Customer1,am Customer1,David Customer2,I Customer2,am Customer2,Taylor (4 Replies)
Discussion started by: monishathampi
4 Replies

4. Shell Programming and Scripting

grep words from txt

Queue on node in domain description : type : local max message len : 104857600 max queue depth : 5000 queue depth max event : enabled persistent msgs : yes backout threshold : 0 msg delivery seq :... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

5. UNIX for Dummies Questions & Answers

Eliminate Hyphenated Words in Grep

How do you negate a literal hyphen/dash in a regex? If it's the first character inside the brackets, then it is read literally. But if you stick a caret to the left of it, to negate it, then it seems it is no longer read literally. Or whatever, it doesn't work. Nor does escaping it seem to... (3 Replies)
Discussion started by: sudon't
3 Replies

6. Shell Programming and Scripting

grep for words in file

Hi Please can you help me on this: How to grep for multiple words in a file, BUT for every word found output it to a new line. regards FR (8 Replies)
Discussion started by: fretagi
8 Replies

7. Shell Programming and Scripting

Remove all words after first space from each line

My file looks like: asd absjdd sdff vczxs wedssx c dasx ccc I need to keep asd sdff wedssx dasx How do I do that experts?:wall::wall: (1 Reply)
Discussion started by: hakermania
1 Replies

8. Shell Programming and Scripting

Insert space between two words

Hi, I need to insert space between words on my output in UNIX other than the single space given by the space bar on my keyboard, e.g when are you going. (There should be 4 spaces between each of these words) rather than when are you going Can anyone help me with... (3 Replies)
Discussion started by: divroro12
3 Replies

9. UNIX for Dummies Questions & Answers

Grep Three Words

I have been trying to find files containing the words AAA, BBB and CCC. I tried: grep AAA `grep BBB files*` grep CCC files* but is does not work I tried several ways this is an easy one but I am a dummy, Does anyone can help me? Thanks :( (12 Replies)
Discussion started by: murbina
12 Replies

10. Shell Programming and Scripting

find words with grep....

I have a .txt file which contains several lines of text. I need to write a script program using grep or any other unix tool so as to detect part of the text (words) between / / that begin with the symbol ~. For example if somewhere in the text appears a webpage address like... (8 Replies)
Discussion started by: chrisxgr
8 Replies
Login or Register to Ask a Question