[Shell programming] character interception command cut, printf command

cut command

Function

The cut command cuts bytes, characters, and fields from each line of a file and writes them to standard output.
If you do not specify the File parameter, the cut command reads standard input. One of the -b, -c, or -f flags must be specified.

grammar

cut [options] filename

parameter

parameter illustrate
-b Split in bytes. These byte positions ignore multibyte character boundaries unless the -n flag is also specified.
-c Split by character.
-d Custom delimiter, default is tab.
-f Used with -d, specifies which region to display.
-n Unsplit multibyte characters. Only used with the -b flag.
If the last byte of a character falls within the range indicated by the List parameter of the -b flag, the character will be written; otherwise, the character will be excluded

example

test text

The spaces in the student table text here must use tabs, and the number of tabs must be the same .

insert image description here

Extract names of all rows

Order:cut -f 2 student.txt

Effect picture:
insert image description here

Extract names and ratings for all rows

Order:cut -f 2,4 student.txt

Effect picture:
insert image description here

split text

According to: split, output the first and third columns
Command:cut -d ":" -f 1,3 /etc/passwd

Effect picture:
insert image description here

Extract the username of the user in /etc/passwd

Order:cut /etc/passwd | grep /bin/bash | cut -d ":" -f 1

Effect picture:
insert image description here

Limitations of the cut command

Cannot be used for space separation and the number of tabs must be the same or,. |: Equal symbol

printf command

Function

printf uses quoted text or space-separated parameters, and format strings can be used in printf outside, and the width of the string, left and right alignment, etc. can also be specified. The default printf does not automatically add newlines like echo, we can manually add \n.

grammar

printf "output type output format" output content

parameter

output type

Format illustrate
%ns output string. n is a number indicating how many characters to output
%in Output integers. n is a number to indicate how many numbers to output
%m.nf Output floating point number. m and n are numbers that refer to the number of integer and decimal digits of the output. For example, %8.2f means to output a total of 8 digits, including 2 decimals and 6 integers

output format

Format illustrate
\a output warning sound
\b Output the backspace key, which is Backspace
\f clear screen
\n newline
\r Enter, that is, Enter
\t Horizontal output backspace key, that is, tab
\in Vertical output backspace key, that is, tab

example

Notice:Format characters are enclosed in quotation marks

output in string format

Order:printf %s 1 2 3 4 5 6

Effect picture:
insert image description here

Control column count output

Order:printf '%s %s %s\n' 1 2 3 4 5 6

Effect picture:
insert image description here

Do not adjust output file content

The printf command can be used with cat, similar to chain programming.

Order:printf '%s' $(cat ./test/student.txt)

Effect picture:
insert image description here

Adjust output file content

Order:printf '%s\t %s\t %s\t %s\n' $(cat ./test/student.txt)

Effect picture:
insert image description here

Guess you like

Origin blog.csdn.net/qq_45254369/article/details/126896446