Confusing of some basic awk

  1. increase file space
    first, double space a file:
awk '1;{print ""}'

I probably can understand it:print a blank line every time.But when I read triple space a file I am confused:

awk '1;{print "\n"}'

doesn't it meaning print a blank line every time too?

  1. number each line of file, but only print numbers if line is not blank:
awk 'NF{$0=++a":"$0};1'

what the a mean here?? does that just a avirable with default value of 0?and what the difference of the position of the 1:at the beginning and at the end?

3.IN DOS environment convert Unix newlines to DOS format

awk 1

I am confused because in Unix exchange format like this:

awk '{sub(/\r$/),""};1'
awk '{sub(/$/,"\r")};1'

those make sense, but awk 1 doesn't make sense..

4.remove duplicate,consecutive lines:

awk 'a !~ $0;{a=$0}'

I can't understand it at all.
remove duplicate,nonconsecutive lines:

awk '!a[$0]++'

this seems very useful,but can't understand it either!
hope you can help:D

So for number 1, by default the print command within awk prints a new line at the end of the line it is printing. As the line here is blank (nothing enclosed within the quotes) it simply prints a blank line, with a new line at the end. When you put \n within the print quotes, the line to print becomes a new line - as well as the new line printed by default at the end!

For number 2 - firstly what is happening here is the code within the action block enclosed within curly braces is being executed only when NF is greater than 0 (i.e. When there is data on the line!). The value of $0 (which contains the entire line) is being re-evaluated to: The value of "a" (the line number), then a single colon and then the actual value of $0 (the entire line). The ++ in front of "a" means the value of "a" will be incremented each time the action block is executed.

To address your point about the position of the number 1 within both awk statements, this is a little trick to invoke awks default action of printing all lines. So putting it before an action block will print the lines prior to the action block being executed and putting it at the end of an action block will print the lines after the action block has been executed. Try doing the below and you will see what I mean:

awk 1 filename

For number 3, as you can see from the above mention about 'awk 1' - all this will do is invoke awk's default action of printing all of the lines within a file.

For number 4, the first awk statement is simply checking to see if the value of "a" is NOT like the value of $0 (which contains the entire line), if this is true then perform awk's default action of printing the line. After this is done, it will then set a to the value of $0, ready to test the next line.
The second awk statement appends to an array called "a" for every line read. It sets the index for the array as $0 (the line) and the value increments by 1. It works as if it reads a line that has already been added into the array, the value will become greater than 1. The exclamation mark will negate the default action of printing the line being performed as a result as the value is not 0.

Hope that helps, sorry if my explanation is long winded and more confusing!

1 Like

Are you asking for someone to explain the logic of your examples?
Do you have any knowledge or experience with awk?

I just want to know is there a difference between the position of 1 .
Are

1;{}

and

{}1;

the same? If you can help,I will be very thankful:)
Maybe it's just the print order, I am just not very sure,I need some expert say yes or no.

Hello hhdzhu,

If I understood correctly your requirement then you wanted to know difference between awk '1;{print "\n"}' Input_file and awk '{print "\n"};1' Input_file .
Off course there is a difference in case of awk '1;{print "\n"}' Input_file first complete line of Input_file will be printed and then new line. In case of awk '{print "\n"};1' Input_file , first new line will be printed and then the Input_file's line.

Also in awk works on the method of condition and action, so in case when we are mentioning here only 1 which means we are making condition part as TRUE, so when condition is TRUE we should give some action because we haven't given any action here so awk is performing default action which is printing the line, hope this helps you.

Thanks,
R. Singh

1 Like

That depends highly on the contents between the braces. As given, the two samples are identical. Should there be any statements inside the braces that either print something or modify any of the fields in the line, the output wil be different.

1 Like

The 1 (outside braces) is a true condition, and its default action is {print} (and that is {print $0} ).

1 Like