grep lines containing a word only twice.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep lines containing a word only twice.
# 1  
Old 11-04-2009
Question grep lines containing a word only twice.

Hi,

I have a query on using grep options. I tried with several options but unable to do it.

lanite:52> cat note
123 456 ab 123
ab cv 234 4566 67
ab gh tij ab 12 34 ab
ab cv dfgv ab cv ab kjhk ab ghj
sdf
dfg ab jljklj ab

Now, I need to use grep to find the line which contains the word "ab" exactly twice. Please help me.Smilie
# 2  
Old 11-04-2009
Code:
$ awk '{for (i=1;i<=NF;i++) if ($i=="ab") A++ } {if (A=="2") print $0} A=0' urfile
dfg ab jljklj ab

# 3  
Old 11-04-2009
Hammer & Screwdriver

Thanks a lot.

Is it not possible to do it using grep or egrep....? Smilie
[As I need to do it using grep only....Smilie]
# 4  
Old 11-04-2009
This sounds like homework. Please repost over in the homework forums.
# 5  
Old 11-04-2009
Data

Smilie..It's not a home work..but rest of all the code uses grep to find other combinations...Just wanted to add this too using grep.
# 6  
Old 11-04-2009
gawk
Code:
$ awk -F"ab" 'NF==3'  file

# 7  
Old 11-05-2009
perl:

Code:
open FH,"<a.txt";
while(<FH>){
  chomp;
  my @tmp =split;
  my @t = grep { $_ eq "ab" } @tmp;
  print $_,"\n" if $#t eq 1;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. UNIX for Dummies Questions & Answers

How to grep a word and the two lines before?

Hi I have this txt file getting from a lpstat command. XA40 XA40 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1 2 0 Unknown specified 0 0 1 1 3 XA99 @spip READY : (FATAL ERROR) 0781-233 Unknown host spiprs01.mon.local. XA01 @xs00 READY XA01 XA01 0 Unknown 0 0 1 1 1 0 Unknown LPD 0 0 1 1... (5 Replies)
Discussion started by: npatao71
5 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. Shell Programming and Scripting

GREP:Output all lines containing word

Output all lines in the file temp that contain the word dog using GREP only and in one line!!! I tried grep ']dog]' temp but it doesnt catch word dog when is at beginning or end, like: Our dog is nice /this OK Nice dog /this NOT dog good /this NOT Thank... (3 Replies)
Discussion started by: ljubayuu
3 Replies
Login or Register to Ask a Question