Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perform action when pattern not found

Status
Not open for further replies.

elcid2k3

Technical User
Jun 8, 2007
9
RO
Hi,

I have the following problem: I want to search for a pattern in a file, if it is found the line will be replaced with something else; if it is not found I want to insert a line at the end of file.

Can you please give me some suggestions?

Thanks in advance!
 
And what have you tried so far ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would have tried something if I had the knowledge...
What I would like to know is how to perform an action when the pattern is not found. If it is too trivial I apologize... I will try to find a good awk book.
 
A starting point:
awk '
/pattern/{print "something else";f=1;next}
{print}
END{if(!f)print "line at the end"}
' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top