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 !!

How about piping the grep output with

head -1

or 

sed '1 !d'

or, you can use sed

$ sed -n '/AMQ5037/{p;q;}' file

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.

awk '/AMQ/ && ++a[$1] == 1' "$FILE"

Thanks alot !!

Problem is solved :slight_smile: