{
for (i=1; i <= NF; i++)
{
print $i
}
printf "\n"
}
BEGIN {
FS = "\n"
RS = ""
}
{
for (i=1; i<=NF; i++) {
printf "%s\t", $i
}
printf "\n"
}
{
{gsub(/John/, "Jon Smith")}
print
}
2 is an even prime number. 3 is a crowd. 1 is next to a prime. 5 is first odd number after 4. 4 is just added here for completeness sake.Let us suppose we want to sort the lines so that the line about number 1 is first, then the line about number 2, and so on. Here's a gawk script for this purpose:
{
store[$1] = $0
}
END {
for (i=1; i<=NR; i++)
print store[i]
}
Here we are using an associative array store to store
each line (in $0) alongside an index (which is the first field,
or $1). At the end of the day, we print all lines in sorted order
by going through the array store linearly. NR is
the number of records read thus far.
sort <filename>sorts <filename> by default according to field 1. Default sorting is ascending order of character. If you want a numeric sort, append n after the field numbers, e.g., sort -n <filename>.
number 2 is an even prime number. number 3 is a crowd. number 1 is next to a prime. number 5 is first odd number after 4. number 4 is just added here for completeness sake.And you want the file sorted the same way as before. You can do sort -n -k 2 <filename>. Note that -k 2 tells sort to use the second field for sorting. Now try sort -n -r -k 2 <filename>. What happens?