Home > Operation and Maintenance > Linux Operation and Maintenance > How to use commands related to Linux file content

How to use commands related to Linux file content

王林
Release: 2023-05-22 08:53:11
forward
1140 people have browsed it

cat Merge files or view file contents

1. Introduction

cat is the abbreviation of the word concatenate, or it can be understood as a cat, "meow" the file content, that is, the file content is displayed.

Can be used to display the content of a single file. Several files can be connected together to display the content. Read the content from the standard input and display it. Use it with redirection or append symbols. For example, view the file content cat xiezhr. txt View the contents of xiezhr.txt and merge multiple files into one cat xiezhr1.txt xiezhr2.txt >newxiezhr.txt Edit or append content to the end of the file cat >>xiezhr.txtxiezhr.txt command can clear the content of the xiezhr.txt file, but the file still exists

2. Syntax format

cat [参数选项] [文件]
Copy after login

3. Parameter Description

Parameter Parameter Description -n Number all output contents by line starting from 1 -b and -n have similar functions, but ignore the display of blank line line numbers -s When the file content contains multiple When there are blank lines, for easier reading, -s can replace multiple blank lines with one blank line -E Display the $ symbol at the end of each line -T Display the Tab (tab) character as ^I

4. Practical operation

① Directly execute cat without adding parameters to view the file content

[root@xiezhr test]# cat xiezhr.txt 
个人公众号:XiezhrSpace


个人博客:www.xiezhrspace.cn



个人微信号:xie_zhr

欢迎您的关注!
Copy after login

② Edit the xiezhr.txt file through non-interactive creation
The above xiezhr.txt File We can first create a blank file by touching xiezhr.txt as mentioned before,
and then edit the contents of xiezhr.txt through vi/vim (we will talk about its usage in detail in the next issue).

Naturally, this is what we did before. Today we are trying to create the xiezhr.txt file and modify its content in a new way.

[root@xiezhr test]# ls
a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  test.txt  tt.txt
[root@xiezhr test]# cat >xiezhr.txt<<EOF
> 个人公众号:XiezhrSpace
> 
> 
> 个人博客:www.xiezhrspace.cn
> 
> 
> 
> 个人微信号:xie_zhr
> 
> 欢迎您的关注!
> EOF  #注意,这里需要按回车结束,EOF为标签,我们可以用其他标签替代,但是必须成对出现。
[root@xiezhr test]# ls
a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  test.txt  tt.txt  xiezhr.txt
Copy after login

③ Use [-n] [-b] to display the serial number before the file created above

[root@xiezhr test]# cat -n xiezhr.txt 
     1  个人公众号:XiezhrSpace
     2
     3
     4  个人博客:www.xiezhrspace.cn
     5
     6
     7
     8  个人微信号:xie_zhr
     9
    10  欢迎您的关注!
[root@xiezhr test]# cat -b xiezhr.txt 
     1  个人公众号:XiezhrSpace


     2  个人博客:www.xiezhrspace.cn



     3  个人微信号:xie_zhr

     4  欢迎您的关注!
Copy after login

④ Use the [-E] parameter to display $

at the end of each line
[root@xiezhr test]# cat -E xiezhr.txt 
个人公众号:XiezhrSpace$
$
$
个人博客:www.xiezhrspace.cn$
$
$
$
个人微信号:xie_zhr$
$
欢迎您的关注!$
Copy after login

⑤Use the [-s] parameter to turn multiple blank lines in the file content into one blank line, which is convenient for us to read

[root@xiezhr test]# cat -s xiezhr.txt 
个人公众号:XiezhrSpace

个人博客:www.xiezhrspace.cn

个人微信号:xie_zhr

欢迎您的关注!
Copy after login

The multiple blank lines in the above file become one , so that when the file contains a lot of content, it will be more friendly to read.

Of course, you can also remove all blank lines, which we will say in the following commands

⑥ Connect multiple files and display the content

# 创建test1.txt 文件
[root@xiezhr test]# cat >test1.txt <<EOF
> 这个是test1.txt的内容
> EOF
# 创建test2.txt文件
[root@xiezhr test]# cat >test2.txt <<EOF           
> 这个是test2.txt的内容
> EOF
# 将test1 test2 文件合并并输出
[root@xiezhr test]# cat test1.txt test2.txt test3.txt
这个是test1.txt的内容
这个是test2.txt的内容
Copy after login

more Paging display File content

1. Introduction

more Its function is similar to cat. Cat displays the entire file content at once, while more displays the file content page by page. If we read more pages, we can better understand the Chinese meaning and remember them better. ????

2. Syntax format

more [参数选项] [文件]
Copy after login

3. Parameter description

ParameterParameter description
-numSpecify the screen display size to be num lines
numStart from line number num Display

4. Practical operation

After using the more command to open the text, you will enter the vi interactive interface. At this time we can use the vi editor function. So what are some explanation functions?

See the following table for details

SubcommandExplanation
h or? View help
Space bar [space]Scroll down one screen
bReturn to the previous screen
EnterDisplay one line down
/What you are looking forFind the specified text content
=Output the line number of the current line
:f Output the opened file name and line number
vCalling the vi editor
!Call the shell and execute the command
qExit more

① View the file without parameters

[root@xiezhr ~]# more /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
......由于文件内容比较多此处省略n行.....
ssh             22/udp                          # The Secure Shell (SSH) Protocol
telnet          23/tcp
--More--(0%)
Copy after login

② Use the [-num] parameter to display num lines per page

[root@xiezhr ~]# more -5 /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
Copy after login

② Use the [num] parameter to display the file content starting from the num line

[root@xiezhr ~]# more +666 -6
Usage: more [options] file...

Options:
  -d        display help instead of ring bell
  -f        count logical, rather than screen lines
  -l        suppress pause after form feed
  -p        do not scroll, clean screen and display text
  -c        do not scroll, display text and clean line ends
  -u        suppress underlining
  -s        squeeze multiple blank lines into one
  -NUM      specify the number of lines per screenful
  +NUM      display file beginning from line number NUM
  +/STRING  display file beginning from search string match
  -V        output version information and exit
Copy after login

less Display the file content in paging

1. Introduction

Although the Chinese meaning of lss is less, it seems that it has fewer functions, but in fact it has similar functions to more. It is an enhanced version of more, with more functions

2, Grammar format

less [参数选项] [文件]
Copy after login

3, Parameter description

ParameterParameter description
-NDisplay the line number of each line
-mDisplay progress percentage
-sWhen consecutive blank lines appear, they are compressed into one line and displayed
-e Automatically exit when the file is displayed to the end. If the parameters are not applicable, you need to use the shortcut key q to exit less

4、实践操作

使用less命令打开文本之后,会进入vi交互界面。下面列出了一些常用交互式子命令

子命令说明
空格键[space]向后翻一页
b向前翻一页
回车键[Enter]向下滚动一行
向上滚动一行
向下滚动一行
[PgUp]向前翻一页
[PgDn]向后翻一页
/字符串向下搜素“字符串”
?字符串向上搜素”字符串“
n向后查找下一个匹配的文本
N向上查找上一个匹配的文本
v进入vi编辑界面
!调用shell,并执行命令
G移动到最后一行
g移动到第一行
h显示帮助界面
q退出less

① 不带参数查看文件

[root@xiezhr ~]# less /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
......省略n行......
Copy after login

② 带上【-N】参数,显示行号

[root@xiezhr ~]# less -N /etc/services 
      1 # /etc/services:
      2 # $Id: services,v 1.55 2013/04/14 ovasik Exp $
      3 #
      4 # Network services, Internet style
      5 # IANA services version: last updated 2013-04-10
      6 #
      7 # Note that it is presently the policy of IANA to assign a single well-known
......此处省略n行......
Copy after login

head 显示文件内容头部

1、简介

head 中文意思时头部,相信你也想到了,head的功能就是显示文件内容的头部。默认显示头10行

2、语法格式

head [参数选项] [文件]
Copy after login

3、参数说明

参数选项参数说明
-n显示前n行

4、实践操作

① 不带参数,默认显示前十行

[root@xiezhr ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
Copy after login

②带上【-n】参数,显示文件前n行

[root@xiezhr ~]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Copy after login

tail 显示文件内容尾部

1、简介

"Tail"这个命令的中文意思是"尾巴",你现在应该知道这个命令的作用了。你想的没错,就是显示文件最后10行,和命令head相反

2、语法格式

tail [参数选项] [文件]
Copy after login

3、参数说明

参数参数说明
-n指定显示的行数
-f实时输出文件变化后追加的数据

4、实践操作

① 不带参数,默认显示最后10行

[root@xiezhr ~]# tail /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
syslog:x:996:994::/home/syslog:/bin/false
git:x:995:993:git version control:/home/git:/bin/bash
nginx:x:994:992:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
xiezhr:x:1001:1001::/home/xiezhr:/bin/bash
Copy after login

② 显示文件尾部5行

[root@xiezhr ~]# tail -n 5 /etc/passwd
syslog:x:996:994::/home/syslog:/bin/false
git:x:995:993:git version control:/home/git:/bin/bash
nginx:x:994:992:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
xiezhr:x:1001:1001::/home/xiezhr:/bin/bash
Copy after login

③ 实时跟踪日志的变化(实际工作中经常用到,需要牢靠掌握)

[root@xiezhr /]# tail -f /var/log/nginx/access.log
118.126.124.141 - - [22/Mar/2022:23:11:10 +0800] "GET /categories/%E8%A7%89%E9%86%92%E5%B9%B4%E4%BB%A3/ HTTP/1.1" 200 10085 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" "-"
14.215.156.21 - - [22/Mar/2022:23:11:10 +0800] "GET /medias/logo.png HTTP/1.1" 200 112674 "-" "-" "-"
54.36.148.108 - - [22/Mar/2022:23:12:14 +0800] "GET / HTTP/1.1" 200 14641 "-" "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)" "-"
69.162.124.234 - - [22/Mar/2022:23:13:23 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
216.245.221.91 - - [22/Mar/2022:23:14:10 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
207.46.13.67 - - [22/Mar/2022:23:16:44 +0800] "GET /archives/f454bf8f.html HTTP/2.0" 200 15842 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
40.77.167.21 - - [22/Mar/2022:23:17:00 +0800] "GET /libs/share/js/social-share.min.js HTTP/2.0" 200 9195 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
157.55.39.183 - - [22/Mar/2022:23:17:00 +0800] "GET /libs/share/css/share.min.css HTTP/2.0" 200 1082 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"
69.162.124.234 - - [22/Mar/2022:23:18:23 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
216.245.221.91 - - [22/Mar/2022:23:19:10 +0800] "HEAD / HTTP/1.1" 200 0 "https://www.xiezhrspace.cn" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "-"
Copy after login

tailf 跟踪日志文件

1、简介

tailf 功能与 tail -f 命令基本相同,记住一个即可

diff 比较两个文件的不同

1、简介

diff 命令用于逐行比较两个文件的不同,并输出差异内容

2、语法格式

diff [参数选项] [文件1] [文件2]
Copy after login

3、参数说明

参数参数说明
-y以并列的方式显示两个文件的不同
-W在使用-y参数时,指定显示的宽度
-c使用上下文的输出格式
-u使用统一格式输出

4、实践操作

① 不带参数,比较两个文件

[root@xiezhr test]# cat test1.txt 
1
2
3
4
[root@xiezhr test]# cat test2.txt 
3
4
5
[root@xiezhr test]# diff test1.txt test2.txt 
1,2d0
< 1
< 2
4a3
> 5
Copy after login

以上结果说明

a 代表add 新加 c 代表change 改变 d 代表delete 删除 <代表第一个文件>代表第二个文件

② 带【-y -w】参数比较两个文件

[root@xiezhr test]# diff -y test1.txt test2.txt 
1                                                             <
2                                                             <
3                                                               3
4                                                               4
                                                              > 5
Copy after login

③ 使用【-c】比较两个文件 (个人推荐使用这个,结果显示直观,一看就懂)

[root@xiezhr test]# diff -c test1.txt test2.txt 
*** test1.txt   2022-03-26 10:04:38.400526014 +0800
--- test2.txt   2022-03-26 10:05:50.597539120 +0800
***************
*** 1,4 ****
- 1
- 2
  3
  4
--- 1,3 ----
  3
  4
+ 5
Copy after login

结果说明:

'+' 表示test2 比test1 多的 '_' 表示test2 比test1 少的

④ 使用【-u】比较两个文件

[root@xiezhr test]# diff -u test1.txt test2.txt 
--- test1.txt   2022-03-26 10:04:38.400526014 +0800
+++ test2.txt   2022-03-26 10:05:50.597539120 +0800
@@ -1,4 +1,3 @@
-1
-2
 3
 4
+5
Copy after login

vimdiff 可视化比较工具

1、简介

vimdiff 命令我们可以拆分为vim和diff 由此可知,该命令是调用vim打开文件来可视化分屏比较多个文件

2、语法格式

vimdiff [参数选项] [文件1] [文件2] [文件3]
Copy after login

3、参数说明

一般不加参数

4、实践操作

比较多个文件

[root@xiezhr test]# vimdiff test1.txt test2.txt test3.txt
Copy after login

How to use commands related to Linux file content

退出vimdiff 需要执行两次退出vim的操作(:q)

wc 统计文件的的行数、单词数或字节数

1、简介

wc 命令可以理解为是words count 的缩写,说到缩写,我知道你想到了“厕所”,可真不是厕所的缩写。

用于统计文件的行数、单词数或字节数

2、语法格式

wc [参数选项] [文件]
Copy after login

3、参数说明

参数参数说明
-c统计字节数
-w统计单词数
-l统计行数
-L打印最长行的长度
-m统计字符数

4、实践操作

① 不带参数

[root@xiezhr test]# cat xiezhr.txt 
个人公众号:XiezhrSpace


个人博客:www.xiezhrspace.cn



个人微信号:xie_zhr

欢迎您的关注!
[root@xiezhr test]# wc xiezhr.txt 
 10   4 118 xiezhr.txt
Copy after login

结果说明,不带参数,直接输出的结果四10行,4个单词(按照英文单词方式记的????),118个字节

② 带上参数,查看文件的字数、字节数、字符数

[root@xiezhr test]# cat xiezhr.txt 
个人公众号:XiezhrSpace


个人博客:www.xiezhrspace.cn



个人微信号:xie_zhr

欢迎您的关注!
[root@xiezhr test]# wc -c xiezhr.txt 
118 xiezhr.txt
[root@xiezhr test]# wc -l xiezhr.txt 
10 xiezhr.txt
[root@xiezhr test]# wc -m xiezhr.txt 
70 xiezhr.txt
[root@xiezhr test]# wc -w xiezhr.txt 
4 xiezhr.txt
[root@xiezhr test]# wc -L xiezhr.txt 
28 xiezhr.txt
Copy after login

③ 查看登录系统的用户数

# 查看哪些用户登录系统
[root@xiezhr test]# who
root     pts/0        2022-03-26 10:03 (39.130.60.84)
root     pts/1        2022-03-26 10:36 (39.130.60.84)
root     pts/2        2022-03-26 10:56 (39.130.60.84)
# 查看一共有多少用户登录系统
[root@xiezhr test]# who |wc -l
3
Copy after login

sort 文本排序

1、简介

sort 中文意思就是排序,所以呢该命令用于将输入的文件内容按照指定规则排序

2、语法格式

sort [参数选项] [文件]
Copy after login

3、参数说明

参数参数选项
-b忽略每行开头存在的空格字符
-n按照数值的大小进行排序
-r倒叙排序
-u去除重复行
-t指定分隔符
-k按指定区间排序

4、实践操作

默认是从首字符向后,依次按照ASCII码升序排列

①不带参数进行排序

[root@xiezhr test]# cat a.txt 

192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.21
192.168.205.24

[root@xiezhr test]# sort a.txt 


192.168.205.21
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.24
Copy after login

② 使用【-n】参数,按数字从小到大排序

[root@xiezhr test]# sort -n a.txt 


192.168.205.21
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.24
Copy after login

③ 使用【-r】参数,按降序排序

[root@xiezhr test]# sort -nr a.txt 
192.168.205.24
192.168.205.24
192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.21
Copy after login

④使用【-u】参数,去除重复

[root@xiezhr test]# sort -u a.txt 
192.168.205.21
192.168.205.23
192.168.205.24
Copy after login

⑤使用【-t -k】按指定列排序

[root@xiezhr test]# cat a2.txt 
小谢 18岁
小明 17岁
小林 25岁
晓燕 30岁
小李 17岁
[root@xiezhr test]# sort a2.txt 
小明 17岁
小李 17岁
小林 25岁
小谢 18岁
晓燕 30岁
# -t 后面指定以空格为分隔符, -k 后面参数表示按第二列排序
[root@xiezhr test]# sort -t " " -k2 a2.txt 
小明 17岁
小李 17岁
小谢 18岁
小林 25岁
晓燕 30岁
Copy after login

uniq 去除重复行

1、简介

uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

2、语法格式

uniq [参数选项] [文件或标准输出]
Copy after login

3、参数说明

参数参数说明
-c去除重复行,并计算每行出现的次数
-d只显示重复的行
-u只显示唯一的行

4、实践操作

① 不带参数去重

[root@xiezhr test]# cat test1.txt 
个人公众号:XiezhrSpace
个人公众号:XiezhrSpace
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
个人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq test1.txt 
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
Copy after login

②加上【-c】参数,显示相应出现的次数

[root@xiezhr test]# cat test1.txt 
个人公众号:XiezhrSpace
个人公众号:XiezhrSpace
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
个人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq -c test1.txt 
      3 个人公众号:XiezhrSpace
      2 个人博客:www.xiezhrspace.cn
Copy after login

③ 配合着sort命令一起使用

[root@xiezhr test]# cat test1.txt 
个人公众号:XiezhrSpace
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
个人博客:www.xiezhrspace.cn
[root@xiezhr test]# uniq test1.txt 
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
个人公众号:XiezhrSpace
个人博客:www.xiezhrspace.cn
[root@xiezhr test]# sort -n test1.txt | uniq -c
      3 个人公众号:XiezhrSpace
      3 个人博客:www.xiezhrspace.cn
Copy after login

join 按两个文件的相同字段合并

1、简介

man 命令用于将两个文件中,指定栏位内容相同的行连接起来

2、语法格式

join [参数选项] [文件1] [文件2]
Copy after login

3、参数说明

参数参数选项
-1以第一个文件的指定字段为基础进行合并
-2以第二个文件的指定字段为基础进行合并
-i比较字段时忽略大小写
-a<1或2>除了显示原来的输出内容之外,还显示指令文件中没有相同栏位的行

4、实践操作

合并文本

[root@xiezhr test]# cat a1.txt 
小谢 男
小明 女
小林 男
晓燕 女
[root@xiezhr test]# cat a2.txt 
小谢 18岁
小明 17岁
小林 25岁
晓燕 30岁
[root@xiezhr test]# join a1.txt a2.txt 
小谢 男 18岁
小明 女 17岁
小林 男 25岁
晓燕 女 30岁
Copy after login

paste 合并文件

1、简介

paste 命令能将文件按照行与行进行合并,中间使用tab隔开

2、语法格式

paste [参数选项] [文件]
Copy after login

3、参数说明

参数参数说明
-d指定合并的分隔符,默认是tab
-s每个文件占用一行

4、实践操作

①不带参数合并文件

[root@xiezhr test]# cat a1.txt 
小谢 男
小明 女
小林 男
晓燕 女
[root@xiezhr test]# cat a2.txt 
小谢 18岁
小明 17岁
小林 25岁
晓燕 30岁
小李 17岁
[root@xiezhr test]# paste a1.txt a2.txt 
小谢 男 小谢 18岁
小明 女 小明 17岁
小林 男 小林 25岁
晓燕 女 晓燕 30岁
        小李 17岁
Copy after login

②通过【-d】参数,指定分隔符

[root@xiezhr test]# cat a1.txt 
小谢 男
小明 女
小林 男
晓燕 女
[root@xiezhr test]# cat a2.txt 
小谢 18岁
小明 17岁
小林 25岁
晓燕 30岁
小李 17岁
[root@xiezhr test]# paste -d: a1.txt a2.txt 
小谢 男:小谢 18岁
小明 女:小明 17岁
小林 男:小林 25岁
晓燕 女:晓燕 30岁
:小李 17岁
Copy after login

③使用【-s】参数,合并内容

[root@xiezhr test]# cat a1.txt 
小谢 男
小明 女
小林 男
晓燕 女
[root@xiezhr test]# paste -s a1.txt 
小谢 男 小明 女 小林 男 晓燕 女
Copy after login

split 分割文件

1、简介

split 是分割的意思,按照指定行数或者指定文件大小分割文件,将其分割成多个文件。

2、语法格式

split [参数选项] [输入文件] [输入文件名前缀]
Copy after login

3、参数说明

参数参数说明
-l指定分割后文件的最大行数

4、实践操作

按行数进行分割

[root@xiezhr test]# cat a.txt 

192.168.205.23
192.168.205.23
192.168.205.23
192.168.205.24
192.168.205.21
192.168.205.24
#原来的大文件"a.txt"切割成多个以"x"开头的小文件。而在这些小文件中,每个文件都只有3行内容。
[root@xiezhr test]# split -3 a.txt 
[root@xiezhr test]# ls
a1.txt  a2.txt  a.txt  c.txt  dir  dir2  dir3  dir5  dir6  dir_bak  movie.tar.gz  m.tx  test1.txt  test2.txt  test3.txt  test.txt  tt.txt  xaa  xab  xac  xiezhr.txt
Copy after login

cut 从文本中提取一段文字并输出

1、简介

cut 意思是剪切,所以其功能就是把文件每一行剪切一段文字,并将文字输出

2、语法格式

cut [参数选项] [文件]
Copy after login

3、参数说明

参数参数选项
-c以字符为单位进行分割
-d自定义分割符,默认以tab为分割符
-f与选项-d一起使用,指定显示哪个区域
N第N个字节、字符或字段
N-从第N个字符、字节或字段开始直至行尾
N-M从第N到第M(包含M)个字节、字符或字段
-M从第1到第M(包含M)个字节、字符或字段

4、实践操作

以字符为单位进行剪切

[root@xiezhr dir]# cat a.txt 
I am xiezhr.I love coding.

[root@xiezhr dir]# cut -c 4 a.txt  #输出第4个字符
m

[root@xiezhr dir]# cut -c 3-4 a.txt #输出第3到第4个字符
am

[root@xiezhr dir]# cut -c -4 a.txt  #输出第1到第4个字符
I am

[root@xiezhr dir]# cut -c 4- a.txt  #输出第4个到最后个字符
m xiezhr.I love coding.
Copy after login

tr 替换或删除字符

1、简介

tr 命令用于替换或删除文件中的字符。

2、语法格式

tr [参数选项] [字符1] [字符2]
Copy after login

3、参数说明

参数参数说明
-d删除字符
-s保留连续字符的第一个字符,删去其他字符
-c反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换

4、实践操作

①将文件中的‘is’ 替换为‘ab’ 注意:凡是文中出现的"i"都替换成“a”,"s"均被替换成“b”,而不仅仅是“is”替换为字符串“ab”

[root@xiezhr dir]# clear
[root@xiezhr dir]# cat a.txt 
I am xiezhr.
I love coding. 
My official account is XiezhrSpace.
My blog is www.xiezhrspace.cn.
My QQ is 1666397814.

Welcome to follow me!

We study together and make progress together.

[root@xiezhr dir]# tr &#39;is&#39; &#39;ab&#39; < a.txt 
I am xaezhr.
I love codang. 
My offacaal account ab XaezhrSpace.
My blog ab www.xaezhrbpace.cn.
My QQ ab 1666397814.

Welcome to follow me!

We btudy together and make progrebb together.
Copy after login

②大小写替换

[root@xiezhr dir]# tr &#39;[a-z]&#39; &#39;[A-Z]&#39; < a.txt 
I AM XIEZHR.
I LOVE CODING. 
MY OFFICIAL ACCOUNT IS XIEZHRSPACE.
MY BLOG IS WWW.XIEZHRSPACE.CN.
MY QQ IS 1666397814.

WELCOME TO FOLLOW ME!

WE STUDY TOGETHER AND MAKE PROGRESS TOGETHER.
Copy after login

③ 将数字0-9 替换成a-j

[root@xiezhr dir]# tr &#39;[0-9]&#39; &#39;[a-j]&#39; < a.txt 
I am xiezhr.
I love coding. 
My official account is XiezhrSpace.
My blog is www.xiezhrspace.cn.
My QQ is bgggdjhibe.

Welcome to follow me!

We study together and make progress together.
Copy after login

④删除文中的字符(注:’M‘,'y'每个字符都会被删除,而不仅仅是“My”字符被删除)

[root@xiezhr dir]# tr -d &#39;My&#39; < a.txt 
I am xiezhr.
I love coding. 
 official account is XiezhrSpace.
 blog is www.xiezhrspace.cn.
 QQ is 1666397814.

Welcome to follow me!

We stud together and make progress together.
Copy after login

⑤ 删除文件中换行符“\n”,制表符“\t”

[root@xiezhr dir]# tr -d &#39;\n\t&#39; < a.txt 
I am xiezhr.I love coding. My official account is XiezhrSpace.My blog is www.xiezhrspace.cn.My QQ is 1666397814.Welcome to follow me!We study together and make progress together.[root@xiezhr dir]#
Copy after login

⑥使用【-c】参数取反

# 下面命令将不是‘0-9&#39; 的数字替换为‘*&#39;
[root@xiezhr dir]# tr -c &#39;0-9&#39; &#39;*&#39; < a.txt 
*********************************************************************************************************1666397814*************************************************************************[root@xiezhr dir]#
Copy after login

Linux系统中可以使用多种命令来处理文件内容。cat命令将文件的全部内容输出到控制台,但它不能上下滚动浏览;more命令可以进行分页显示,但只能向下滚动,若要向上则需重新输入命令;less命令集合了cat和more的优点,不仅可以翻页,还支持前后搜索;head和tail命令可以输出文件的头几行或尾几行;grep和find是用于搜索所需信息的强大命令,在数据挖掘和日志分析的应用场景中得到广泛应用。理解和掌握这些命令的特别用途可以使我们在Linux系统中以更高效的方式操作和处理文件内容。

The above is the detailed content of How to use commands related to Linux file content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template