What is the difference between sed and awk?

sed is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two “variables”: pattern space and hold space. Readability of scripts can be difficult. Mathematical operations are … Read more

Use of `NF` in awk command

-v var=val–assign var=val Set the variable var to the value val before execution of the program begins. NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. In your first program, you execute {NF=3} after each … Read more

Printing the last column of a line in a file

You don’t see anything, because of buffering. The output is shown, when there are enough lines or end of file is reached. tail -f means wait for more input, but there are no more lines in file and so the pipe to grep is never closed. If you omit -f from tail the output is shown immediately: @EdMorton is right of course. Awk can search for A1 as well, … Read more

What are NR and FNR and what does “NR==FNR” imply?

In awk, FNR refers to the record number (typically the line number) in the current file and NR refers to the total record number. The operator == is a comparison operator, which returns true when the two surrounding operands are equal. This means that the condition NR==FNR is only true for the first file, as FNR resets back to 1 for the first line of … Read more