Displaying only First Occurrence of a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying only First Occurrence of a String
# 1  
Old 01-20-2009
Displaying only First Occurrence of a String

Hi,

My requirement is that I should search for a particular string and display the string only once if there is more occurrence of the same string in a file.

e.g In the given below log file,the string AMQ5037 comes twice.I want to search for this string and display it only once.grep will display both the occurrence...How can i do that ?

-------------------------------------------------------------------------
11/27/08 12:33:45 - Process(23911.3) User(mqm) Program(amqzmuc0)
AMQ5037: The Queue Manager task 'LOGGER-IO' has started.

EXPLANATION:
The Utility Task Manager, processId(0) type(23911), has started the LOGGER-IO
task.

EXPLANATION:
The Utility Task Manager, processId(0) type(23911), has started the LOGGER-IO
task.
ACTION:
None.
-------------------------------------------------------------------------------
11/27/08 12:33:46 - Process(23911.1) User(mqm) Program(amqzmuc0)
AMQ5041: The Queue Manager task 'LOGGER-IO' has ended.

EXPLANATION:
The Queue Manager task LOGGER-IO has ended.
ACTION:
None.
-------------------------------------------------------------------------------
11/27/08 12:33:46 - Process(23914.3) User(mqm) Program(amqzmuc0)
AMQ5037: The Queue Manager task 'LOGGER-IO' has started.

EXPLANATION:
The Utility Task Manager, processId(0) type(23914), has started the LOGGER-IO
task.
ACTION:
None.
-------------------------------------------------------------------------------
11/27/08 12:33:46 - Process(23912.1) User(mqm) Program(amqzxma0_nd)
AMQ7229: 4 log records accessed on queue manager 'SUMIT' during the log replay
phase.

EXPLANATION:
4 log records have been accessed so far on queue manager SUMIT during the log
replay phase in order to bring the queue manager back to a previously known
state.
ACTION:
None.
-------------------------------------------------------------------------

Thanks in advance !!
# 2  
Old 01-21-2009
How about piping the grep output with

Code:
head -1

or 

sed '1 !d'

# 3  
Old 01-21-2009
or, you can use sed
Code:
$ sed -n '/AMQ5037/{p;q;}' file

# 4  
Old 01-21-2009
Hi ,
Thanks for the reply. I have to print lines containing the pattern AMQ in the log file but the repeated AMQ codes sholud be printed only once.
# 5  
Old 01-21-2009
Code:
awk '/AMQ/ && ++a[$1] == 1' "$FILE"

# 6  
Old 01-21-2009
Thanks alot !!

Problem is solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append a particular string after occurrence of particular string.

Hi Friends, Good morning. Appended a particular string after occurrence of particular string in a file. my file abc.sql as below create or replace function f1(p_cust_no IN VARCHAR) RETURN number IS DECLARE v_country country.customer_tbl%TYPE; begin begin select... (4 Replies)
Discussion started by: ram0106
4 Replies

2. Shell Programming and Scripting

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

[Solved] Last occurrence of a string

I apologize if it was asked before but I couldn't find something related. I want to replace 2 strings in a file e.g pwddb=Lar1wod (need to replace string after =) pwdapp=Wde2xe (need to replace string after =) AND in same file want to find last occurrence of a string (SR2-134561),... (2 Replies)
Discussion started by: J_ang
2 Replies

4. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

5. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

6. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

7. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

greping last occurrence of the searched string

Hello, I have active log file i want to grep the last occurrence of the word in that log file the log file gets on increasing and increasing i want to fetch it from live file. Please guide me, Thanks in advance (4 Replies)
Discussion started by: vidurmittal
4 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. UNIX for Dummies Questions & Answers

Search and replace to first occurrence of string

Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average... (7 Replies)
Discussion started by: gilmord
7 Replies
Login or Register to Ask a Question