Using grep to find multiple whitespaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using grep to find multiple whitespaces
# 1  
Old 12-15-2009
Using grep to find multiple whitespaces

Hi All,

Have a query on finding the lines with at least one whitespace. Have following lines in my file myfile.txt

Code:
I
I LOVE
I LOVE FREEDOM
I  LOVE  FREEDOM
I LOVE FREEDOM AND PEACE

I ran the following query to get the lines with at least 3 words.
Code:
grep '\([^ ]* +\)\{3,\}' myfile.txt

But it does not written any lines. Can someone please tell me the issue here


Regards,
sh_kk

Last edited by sh_kk; 12-15-2009 at 01:28 PM.. Reason: use code tags please
# 2  
Old 12-15-2009
Hi

Your post is a little confusing.

Subject: Using grep to find multiple whitespaces (I presume between words)
Code:
grep "  \+" myfile.txt (two spaces there)

Body: Have a query on finding the lines with at least one whitespace (I presume anywhere)
Code:
grep " " myfile.txt

If you show the desired output, then it would be clear.
# 3  
Old 12-15-2009
Code:
grep "  *.* " file_in

because the regexp character * means "0 or more repeats of the previous character", you need to put 2 spaces in the regexp.

even better, for 3 and 3 only words:

Code:
grep "^[^ ]*  *[^ ]*  *[^ ]*$" file_in


Last edited by quirkasaurus; 12-15-2009 at 03:27 PM.. Reason: oopsy. one space too many. exactly 3 words only.
# 4  
Old 12-16-2009
Code:
gawk 'NF>=3' file

# 5  
Old 12-21-2009
Quote:
Originally Posted by ichigo
Code:
gawk 'NF>=3' file

winner.
# 6  
Old 12-21-2009
Simple you missed to escape +. Nothing else.,

Code:
grep '\([^ ]* \+\)\{3,\}' t
I  LOVE  FREEDOM
I LOVE FREEDOM AND PEACE


Else, use extended grep, as
Code:
egrep  '([^ ]* +){3,}' t
I  LOVE  FREEDOM
I LOVE FREEDOM AND PEACE

# 7  
Old 12-21-2009
Code:
egrep '[^ ]+ [^ ]+ [^ ]+' file

Code:
egrep '([^ ]+ ){2}[^ ]+' file


Last edited by Scrutinizer; 12-21-2009 at 03:54 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

4. UNIX Desktop Questions & Answers

How do you [e]grep for multiple values within multiple files?

Hi I'm sure there's a way to do this, but I ran out of caffeine/talent before getting the answer in a long winded alternate way (don't ask ;) ) The task I was trying to do was scan a directory of files and show only files that contained 3 values: I940 5433309 2181 I tried many variations... (4 Replies)
Discussion started by: callumw
4 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

7. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question