Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the linux command line tool for text line filtering

Shulou Source: shulou.com Published: 2022-06-01 18:41:16 09月12日 Update

Editor to share with you how to use the linux command line tool for text line filtering, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Common command line tools

Next, we mainly use head,tail,sed and awk, these tools. Among them, sed and awk tools are very powerful, using them to filter text may be a bit overqualified, but basically all tasks can be done through head and tail commands, and head and tail are more convenient for daily use.

Usage example

Preparatory work

Type the following command to generate a test file.

$seq-f "Line No g" 10 > lines.txt

Show only the first three lines of the text

This can be done through the head command, with the following command and output:

$head-n 3 lines.txt

Line No 1

Line No 2

Line No 3

Or through the sed command, the specific command and output are as follows:

$sed-n '1jue 3p' lines.txt

Line No 1

Line No 2

Line No 3

The awk command can also be completed.

$awk 'NR=5' lines.txt

Line No 5

-- slightly--

Line No 10

Display only the penultimate lines 3 to 1 of the text

To do this with head:

$head-n-2 lines.txt

Line No 1

Line No 2

Line No 3

Line No 4

Line No 5

Line No 6

Line No 7

Line No 8

Show only lines 6 to 8 of the text

Use head and tail tools:

$

< lines.txt head -n 8 | tail -n 3 Line No 6 Line No 7 Line No 8 sed的方法: $ sed -n '6,8p' lines.txt Line No 6 Line No 7 Line No 8 awk的方法: $ awk '(NR>

= 6) & & (NR

Tags: Commands text tools generation even odd method article output this line two content can be passed powerful overqualified and then not much tasks most commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Xiaomi OPPO Reno Shulou Tech Info