Linux: Shell prints even lines, odd lines, line numbers

Source: Internet
Author: User



awk command:



1.



Print line number and content:



awk ' {print NR ': ' $ A} '



2.



Output: Even lines and odd lines to file



awk ' {print $0.txt > nr%2.txt} ' File



3.



Print surprisingly few lines of content: (three equivalent)



awk ' nr%2==1 ' file



awk ' nr%2 ' all_file.txt



awk ' i=!i ' file



Print out the contents of even rows: (three equivalent)



awk ' nr%2==0 ' file



awk '! (nr%2) ' File



awk '! (i=!i) ' File



SED command:



Print surprisingly few lines:



Sed-n ' 1~2p ' file



Print out even lines:



Sed-n ' 2~2p ' file






Extended content:









One. Line spacing:



1. Add a blank line to each row






awk ' 1; {print ""} ' filname.ext #Output a blank line
awk ' 1 {print} {print ' "} ' Filname.ext
awk ' {print} {print ' "} ' Filname.ext
2.1 Another way to achieve this:
You can try to set ors to other to see how the effect
3. Output only non-blank lines, and each line is added with a blank row
Awk 'NF {print $' \ n '}' filname.ext ා $a is the current line, and a newline is finally added
4. Double-spaced; two blank lines between rows
Awk '1; {print "\ n"}' filname.ext ා output will wrap after default output, output \ n, output two blank lines
Equivalent to:
awk ' {print; print ' \ n '} ' Filname.ext
5. Displays the line number in the file where the current line is located
awk ' {print FNR ' \ t ' $ Filname.ext ' #FNR that represents the line number of the current row in the file
6. Displays the line number of the current line during this process
awk ' {print NR ' \ t ' $ Filname.ext ' #NR that represents the line number of the current row in this process
Little doubt: Why is there a difference between FNR and nr? The effect is not all the same? If you give such as: Filname1.ext Filname2.ext, you will see the difference. Original: FNR, is in each file, changed a file, will be zeroed, and NR will be added to each file.
7. Use simple styles to output
Awk '{printf ("]:% s \ n", NR, $)}' filname.ext ා line number takes 5 digits, less space to fill
8. Display non-blank lines
awk ' NF {$0=++a ': ' $ A}; {print} ' Filname.ext
#NF said that represents the line number of the current row, where he is used as a condition, if it is a blank line, NF is 0, skipped; otherwise, the number of non empty rows is stored with dynamic variable a
9. Count rows: effects similar to Wc-l
Awk 'end {print NR}' filname.ext ා end means at which time NR is the line number of the last line, which is the total number of rows
10. Calculate each row's and
awk ' {s = 0; for (i = 1; I <= NF; i++) s = s+ $i; print s} ' Filname.ext
#From 1 to NF (the total number of fields per row), accumulate
11. Calculate all the fields in the file and
awk ' {for (i = 1; I <= NF; i++) s = s+ $i}; END {print S} ' Filname.ext
#S is the sum of the sum, each line is processed and then output s; note that in contrast to 10, there is no zero per line, so it is cumulative. Variable not set, default is null, but will automatically change to 0 based on context numerical calculation
12. Replace each field with its absolute value
awk‘{ for (i = 1; i <= NF; i++) if ($i < 0) $i = -$i; print }‘filname.ext
#$I represents the field in the current row, $0 represents the current row, and the value of $I can be changed
13. Calculate the total fields in the file and (for example, count the number of words)
awk‘{ total = total + NF }; END { print total }‘filname.ext
14. Calculate the total number of rows matching the specified information
awk‘/Linux/ { n++ }; END { print n+0 }‘filname.ext
15. Find the maximum number of the first field in each row in the file, and the row where it is located
awk‘$1 > max { max=$1; maxline=$0 }; END { print max, maxline }‘filname.ext
#Max is used to store the maximum number, and Maxine is used to store the row where the maximum number is, and output at the end
16. Displays the number of fields in the current row and outputs the forward
awk‘{ print NF ":" $0 } ‘filname.ext
17. Display the contents of the last field in each row
Awk '{print $NF}' filname.ext ා NF indicates the number of fields in the current row. For example, if it is 3, then $NF is $3, which is the third field
18. Show last field of last line
awk‘{ field = $NF }; END { print field }‘filname.ext
#There is no output for each line of processing. The output is at the end. Field is the temporary variable of the last line of each line
19. Show rows with fewer than 4 fields
Awk 'NF < 4' filname. Ext {}. As a condition, if there is no {}, the current line will be output by default
20. Show rows with the last field less than 4 per line
Awk '$NF < 4' filname. Ext ා note and 19 comparison
Linux: Shell prints even lines, odd lines, line numbers


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.