Delete lines that contain 3 or more words?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines that contain 3 or more words?
# 1  
Old 12-11-2007
Delete lines that contain 3 or more words?

How can I delete lines that contain 3 or more words?
I have a file, old.txt, that has multi-word phrases in it and I want to remove the lines with 3 words or more and send the output to new.txt.

I've tried the following using sed but it doesn't seem to work:

Code:
sed '/(\b\w+\b){3,}/d' old.txt > new.txt

Any help would be appreciated.
# 2  
Old 12-11-2007
Code:
 sed -n -e "s/^[ ]*[^ ]* [^ ]* [^ ]*[ ]$/&/p file.txt > new.txt

# 3  
Old 12-11-2007
Hi , many ways to do it, other with sed:
Code:
> sed -e "/^[^ ]\{1,\} \{1,\}[^ ]\{1,\} \{1,\}[^ ]\{1,\}/d" file     

> awk 'NF<=2' file

# 4  
Old 12-11-2007
Thanks. Seems like awk is easier for this task. Smilie
# 5  
Old 12-11-2007
Depends on your definition of word,
with GNU grep it could be something like this:

Code:
grep -E '(\w+\b\W+){2}\b\w+' data>new_data


Last edited by radoulov; 12-11-2007 at 09:42 AM..
# 6  
Old 12-11-2007
Quote:
Originally Posted by Klashxx
awk 'NF<=2' file [/CODE]
Hi.
I've learned something new - I didn't know that awk had an implied print.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines containing key words dynamically

Hi Frens, I have a requirement where I need to delete lines having key words and am using the below command to do that sed '/UNIX/d' inputfile > output But now I have one more requirement where in there will be one reference file which has the ID's to be deleted from the master file. ... (3 Replies)
Discussion started by: weknowd
3 Replies

2. UNIX for Dummies Questions & Answers

Delete between two words

So;C951;1;2;0;100;true;SNetwork=ORM_RO_MO_R,MeCext=C5951,ManagedElement=1,vsDaFunction=1;473;12;EEE So;C951;2;2;0;100;true;SNetwork=ORM_RO_MO_R,MeCext=L5921,ManagedElement=1,vsDaFunction=4;481;12;EEE Output:- So;C951;1;2;0;100;true;1;473;12;EEE So;C951;2;2;0;100;true;4;481;12;EEE Output... (7 Replies)
Discussion started by: pareshkp
7 Replies

3. Shell Programming and Scripting

Delete some words

hi, i have a fasta file like this: >contig00003 length=363 numreads=45 gene=isogroup00001 status=it_thresh GATTTTTTACCCTGGGAGTGAGGAGGACGAGGTTGAGGATGAAGAAAAGAGAAAGATGAAGAGGTTGAGGATGTT GTAGTCGGCGGTGGAATTAGGGGGAGCCGGCGAGCCCAAGTATTTTGCAGAGGTGTCTTCATCATCCAAACAACA... (3 Replies)
Discussion started by: the_simpsons
3 Replies

4. UNIX for Dummies Questions & Answers

Delete lines according to a key words in that line

HI, I have a file A like this: c 1 length 14432 width 3434 temp 34 c 2 length 3343 width 0923 height 9383 hm 902 temp34 c 3 length 938 height 982 hm 9292 temp 23 ... (2 Replies)
Discussion started by: the_simpsons
2 Replies

5. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

6. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

7. Shell Programming and Scripting

Delete between two words

Hi, I wanted to delete data between two words. Input: I read gihoihsahkjlk write goal hard read hsakdjhkh write work read hlkhlkhlkh write Desired Output: I write goal hard write work write We have to replace the data that comes between 'read' and 'write' with... (3 Replies)
Discussion started by: mahish20
3 Replies

8. Shell Programming and Scripting

Need to delete words in a file

Hi All, I have an input file a.txt which contains the following :: 08-08-09 1:00 PM 763763762 f00_unix1_server.txt i Just need to delete all the words which is before f Output :: f00_unix1_server.txt Thanks (4 Replies)
Discussion started by: raghav1982
4 Replies

9. UNIX for Advanced & Expert Users

How to delete first 10 words from file

Hi, Could you please let me know, how to delete first 10 words from text files using vi? 10dw will delete it from current line, how to do it for all the lines from file? Thanks (6 Replies)
Discussion started by: sentak
6 Replies

10. UNIX for Dummies Questions & Answers

sed [delete everything between two words]

Hi, I have the following codes below that aims to delete every words between two pattern word. Say I have the files To delete every word between WISH_LIST=" and " I used the below codes (but its not working): #!/bin/sh sed ' /WISH_LIST=\"/ { N /\n.*\"/ {... (3 Replies)
Discussion started by: Orbix
3 Replies
Login or Register to Ask a Question