Get the App
SLTechnology News&Howtos  ›  Servers  › 

20 useful AWK single-line statements

Shulou Source: shulou.com Published: 2022-06-02 07:39:53 10月25日 Update

20 useful AWK single-line statements

1. END {print NR} # output total number of lines

2. NR = = 10 # output line 10

3. {print $NF} # output the last column

4. {field = $NF} END {print field} # output the last column of the last line

5. NF > 4 # output all rows with more than 4 columns in input line (this is not in action, so it is a filtering function)

6. $NF > 4 # output all rows with values greater than 4 in the last column of the input line, which is also a filter

7. All cumulative total fields of {nf = nf + NF} END {print nf} # input line

8. / Beth/ {nlines = nlines + 1} END {print nlines} # the total number of lines containing the word Beth

9. $1 > max {max = $1; maxline = $0} END {print max, maxline} # output the largest row in the first column

10. NF > 0 # field outputs non-empty lines

11. Length ($0) > 80 # filter out rows with a length of more than 80

12. {print NF, $0} # output how many columns there are in front of the line

13. {print $2, $1} # output the second column, space, and first column of each row

14. {temp = $1; $1 = $2; $2 = temp; print} # output all rows after the first and second columns swap positions

15. {$1 = NR; print} # replace the first column with the line number and output the line

16. {$2 = "; print} # clear the second column and output all rows

17. {for (I = NF; I > 0; I = I-1) printf ("% s", $I); printf ("\ n")}

# output the whole line in reverse order

18, {sum = 0; for (I = 1; I

Tags: Output one line sum total line find useful statement in line maximum position function word line positive number space negative number reverse order length ten lines line number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Shulou Information Shulou Tech Info NVidia