Skip to content

Instantly share code, notes, and snippets.

@jf-parent
Last active September 4, 2018 05:51
Show Gist options
  • Save jf-parent/e27c3b14dcc5a1ba551700243bac081f to your computer and use it in GitHub Desktop.
Save jf-parent/e27c3b14dcc5a1ba551700243bac081f to your computer and use it in GitHub Desktop.

Print the total number of input lines:

Print the tenth input line:

Print the last field of every input line:

Print the last field of the last input line:

Print every input line with more than four fields:

Print every input line in which the last field is more than 4:

Print the total number of fields in all input lines:

Print the total number of lines that contain Beth:

Print the largest first field and the line that contains it (assumes some $1 is positive):

Print every line that has at least one field:

Print every line longer than 80 characters:

Print the number of fields in every line followed by the line itself:

Print the first two fields, in opposite order, of every line:

Exchange the first two fields of every line and then print the line:

Print every line with the first field replaced by the line number:

Print every line after erasing the second field:

Print in reverse order the fields of every line:

Print the sums of the fields of every line:

Add up all fields in all lines and print the sum:

Print every line after replacing each field by its absolute value:

Print the total number of input lines:

END { print NR } 

Print the tenth input line:

NR == 10 

Print the last field of every input line:

{ print $NF } 

Print the last field of the last input line:

{ field = $NF}
END { print field } 

Print every input line with more than four fields:

NF > 4 

Print every input line in which the last field is more than 4:

$NF > 4 

Print the total number of fields in all input lines:

{ nf = nf + NF }
END { print nf } 

Print the total number of lines that contain Beth:

/Beth/ { nlines = nlines + 1 }
END { print nlines } 

Print the largest first field and the line that contains it (assumes some $1 is positive):

$1 > max { max = $1; maxline = $0 }
END { print max, maxline } 

Print every line that has at least one field:

NF > 0 

Print every line longer than 80 characters:

length($0) > 80

Print the number of fields in every line followed by the line itself:

{ print NF, $0 } 

Print the first two fields, in opposite order, of every line:

{ print $2, $1 } 

Exchange the first two fields of every line and then print the line:

{ temp = $1; $1 = $2; $2 = temp; print }

Print every line with the first field replaced by the line number:

{ $1 = NR; print } 

Print every line after erasing the second field:

{ $2 = ""; print }

Print in reverse order the fields of every line:

{for (i = NF; i > 0; i = i - 1) printf("%s " $i)
printf ( "\n" ) 
}

Print the sums of the fields of every line:

{ sum= 0
for (i = 1; i <= NF; i = i + 1) sum = sum + $i
print sum
}

Add up all fields in all lines and print the sum:

{ for (i = 1; i <= NF; i = i + 1) sum= sum+ $i }
END { print sum } 

Print every line after replacing each field by its absolute value:

{
for (i = 1; i <= NF; i = i + 1) if ($i < 0) $i = -$i
print 
}
USSR 8649 275 Asia
Canada 3852 25 North America
China 3705 1032 Asia
USA 3615 237 North America
Brazil 3286 134 South America
India 1267 746 Asia
Mexico 762 78 North America
France 211 55 Europe
Japan 144 120 Asia
Germany 96 61 Europe
England 94 56 Europe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment