I was reading this awk script

awk -F"=" '{OFS="=";gsub(",",";",$2)}1'

I want to know what is the function of 1 at the end of it

  • You could also do awk -F"=" '{OFS="=";gsub(",",";",$2)}{print}' – GypsyCosmonaut Aug 18 '18 at 5:57

An awk program is a series of condition-action pairs, conditions being outside of curly braces and actions being enclosed in them. A condition is considered false if it evaluates to zero or the empty string, anything else is true (uninitialized variables are zero or empty string, depending on context, so they are false). Either a condition or an action can be implied; braces without a condition (as yours begins) are considered to have a true condition and are always executed if they are hit, and any condition without an action will print the line if and only if the condition is met.

The 1 at the end of your script is a condition (always true) with no action, so it executes the default action for every line, printing the line (which may have been modified by the previous action in braces).


I really dislike these types of shortcuts because it obfuscates and misleads how it's being parsed. When you read something like awk -F"=" '{OFS="=";gsub(",",";",$2)}1', you might think that the 1 is modifying the previous statement. Or you might think 1 is an alias for {print}, which is not technically correct.

In actually, 1 is a completely separate statement. You can separate the statements like this:

awk -F"=" '
{OFS="="; gsub(",",";",$2)}

Each awk statement format is condition {action}. If there is no {action} given, then the default action is {print}. (If there is no condition given, then the default condition is true.)

So here, 1 is the condition, which always evaluates to true since it is nonzero. The action is omitted, so the default {print} is performed. You can think of it this way:

awk -F"=" '
{OFS="="; gsub(",",";",$2)}
1!=0 {print}

1 means to print every line.

The awk statement is same as writing:

awk -F"=" '{OFS="=";gsub(",",";",$2);print $0;}'

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.