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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Your help is appreciated

Status
Not open for further replies.

ulcifer

Technical User
Oct 21, 2012
2
PH
Hi guys, I've been googling it for several hours now. However, I'm unfortunate enough to find the answer I've been looking for.

This is my problem,

Code:
awk '/foo/{count++;if(count=1){sub("foo","foo1")}}1' file.txt


I can find the mean of the number 1 after the {action}, I just want to know what is the meaning of that.

Your answer is much appreciated... thanks
 
Hi

Awk script consists on pattern-action pairs. If the pattern gets evaluated to true, the action is executed. Either the pattern or the action can miss, but not both. The default pattern is true ( meaning always ), the default action is [tt]print[/tt] ( meaning output the current records and output record separator ). So [tt]1[/tt] is a true pattern, triggering the default action.
Code:
[gray][highlight #fcc]# pattern[/highlight] [highlight #ccf]action[/highlight][/gray]
[highlight #fcc][fuchsia]/foo/[/fuchsia]    [/highlight] [highlight #ccf][teal]{[/teal] count[teal]++;[/teal] [b]if[/b] [teal]([/teal]count[teal]=[/teal][purple]1[/purple][teal])[/teal][teal]{[/teal] [COLOR=chocolate]sub[/color][teal]([/teal][green][i]"foo"[/i][/green][teal],[/teal][green][i]"foo1"[/i][/green][teal])[/teal] [teal]}[/teal] [teal]}[/teal][/highlight]
[highlight #fcc][purple]1[/purple]        [/highlight]
[gray]# equivalent of the second statement[/gray]
[highlight #fcc][purple]1[/purple]        [/highlight] [highlight #ccf][teal]{[/teal] [COLOR=chocolate]print[/color] [teal]}[/teal][/highlight]
[highlight #fcc][purple]1[/purple]        [/highlight] [highlight #ccf][teal]{[/teal] [COLOR=chocolate]print[/color] [navy]$0[/navy] [teal]}[/teal][/highlight]
[highlight #fcc]         [/highlight] [highlight #ccf][teal]{[/teal] [COLOR=chocolate]print[/color] [teal]}[/teal][/highlight]
( By the way, are you sure that code is correct ? The assignment in the [tt]if[/tt] condition looks strange. )

Feherke.
[link feherke.github.com/][/url]
 
@feherke

Thank you very much for your response. Now I know what this code means before I use it in my future scripting.

About the condition part, I noticed that I made a typo error there. Thanks for notifying me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top