INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Your help is appreciated

Your help is appreciated

(OP)
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

RE: Your help is appreciated

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 print ( meaning output the current records and output record separator ). So 1 is a true pattern, triggering the default action.

CODE

# pattern action
/foo/     { count++; if (count=1){ sub("foo","foo1") } }
1        
# equivalent of the second statement
1         { print }
1         { print $0 }
          { print } 
( By the way, are you sure that code is correct ? The assignment in the if condition looks strange. )

Feherke.
http://feherke.github.com/

RE: Your help is appreciated

(OP)
@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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close