shell对文件的操作

shell脚本编写中,时常会用到对文件的相关操作,比如增加内容,修改内容,删除部分内容,查看部分内容等,但是上述举例的这些操作一般都是需要在文本编辑器中才能操作,常用的文本编辑器如:gedit、vim、nano等又是交互式文本编辑器,脚本无法自己独立完成,必须有人参与才可以完成。如果这样的话又违背了我们编写脚本的意愿(全部由机器来完成,减少人的工作压力,提升工作效率)。emm…如何才能让这些操作全部脚本自己就搞定,而不需要人的参与,而且又能按照我们的脚本预案来完成呢?

为了解决上述问题,linux为大家提供了一些命令,比如Perl、sed等命令,今天我就着重为大家介绍一下sed命令。

一、sed介绍

sed是linux中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别

代码语言:javascript代码运行次数:0运行复制

区别是:文本编辑器: 编辑对象是文件行编辑器:编辑对象是文件中的行

也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否者可能无法理解sed的运行原理和使用精髓。

sed数据处理原理

shell对文件的操作

二、sed语法

sed 命令语法:

sed [options] ‘{command}[flags]’ [filename]

代码语言:javascript代码运行次数:0运行复制

#命令选项-e script 将脚本中指定的命令添加到处理输入时执行的命令中  多条件,一行中要有多个操作-f script 将文件中指定的命令添加到处理输入时执行的命令中-n        抑制自动输出-i        编辑文件内容-i.bak    修改时同时创建.bak备份文件。-r        使用扩展的正则表达式!         取反 (跟在模式条件后与shell有所区别)#command   对文件干什么sed常用内部命令a   在匹配后面添加i   在匹配前面添加d   删除s   查找替换  字符串c   更改y   转换   N D P p   打印#flags数字             表示新文本替换的模式g:             表示用新文本替换现有文本的全部实例p:             表示打印原始的内容w filename:     将替换的结果写入文件

2.1)sed内部命令说明

演示实例文档

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# cat data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容增加操作,将数据追加到某个位置之后,使用命令a。

演示案例

代码语言:javascript代码运行次数:0运行复制

在data1的每行后追加一行新数据内容: append data "haha"[root@zutuanxue ~]# sed 'aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.append data "haha"2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.append data "haha"5 the quick brown fox jumps over the lazy dog.append data "haha"在第二行后新开一行追加数据: append data "haha"[root@zutuanxue ~]# sed '2aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.在第二到四行每行后新开一行追加数据: append data "haha"[root@zutuanxue ~]# sed '2,4aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.append data "haha"3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.append data "haha"5 the quick brown fox jumps over the lazy dog.匹配字符串追加: 找到包含"3 the"的行,在其后新开一行追加内容: append data "haha"[root@zutuanxue ~]# sed '/3 the/aappend data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.append data "haha"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.//开启匹配模式  /要匹配的字符串/

文件内容增加操作,将数据插入到某个位置之前,使用命令i。

演示案例

代码语言:javascript代码运行次数:0运行复制

在data1的每行前插入一行新数据内容: insert data "haha"[root@zutuanxue ~]# sed 'iinsert data "haha"' data1insert data "haha"1 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.insert data "haha"4 the quick brown fox jumps over the lazy dog.insert data "haha"5 the quick brown fox jumps over the lazy dog.在第二行前新开一行插入数据: insert data "haha"[root@zutuanxue ~]# sed '2iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.在第二到四行每行前新开一行插入数据: insert data "haha"[root@zutuanxue ~]# sed '2,4iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.insert data "haha"2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.insert data "haha"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.匹配字符串插入: 找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha"[root@zutuanxue ~]# sed '/3 the/iinsert data "haha"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.insert data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–替换,将一行中匹配的内容替换为新的数据,使用命令s。

演示案例

代码语言:javascript代码运行次数:0运行复制

从标准输出流中做替换,将test替换为text[root@zutuanxue ~]# echo "this is a test" |sed 's/test/text/'this is a text将data1中每行的dog替换为cat[root@zutuanxue ~]# sed 's/dog/cat/' data11 the quick brown fox jumps over the lazy cat.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy cat.5 the quick brown fox jumps over the lazy cat.将data1中第二行的dog替换为cat[root@zutuanxue ~]# sed '2s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.将data1中第二到第四行的dog替换为cat[root@zutuanxue ~]# sed '2,4s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy cat.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy cat.5 the quick brown fox jumps over the lazy dog.匹配字符串替换:将包含字符串"3 the"的行中的dog替换为cat[root@zutuanxue ~]# sed '/3 the/s/dog/cat/' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy cat.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–更改,将一行中匹配的内容替换为新的数据,使用命令c。

演示案例

代码语言:javascript代码运行次数:0运行复制

将data1文件中的所有行的内容更改为: change data "data"[root@zutuanxue ~]# sed 'cchange data "haha"' data1change data "haha"change data "haha"change data "haha"change data "haha"change data "haha"将data1文件第二行的内容更改为: change data "haha"[root@zutuanxue ~]# sed '2cchange data "haha"' data11 the quick brown fox jumps over the lazy dog.change data "haha"3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.将data1文件中的第二、三、四行的内容更改为:change data "haha"[root@zutuanxue ~]# sed '2,4cchange data "haha"' data11 the quick brown fox jumps over the lazy dog.change data "haha"5 the quick brown fox jumps over the lazy dog.将data1文件中包含"3 the"的行内容更改为: change data "haha"[root@zutuanxue ~]# sed '/3 the/cchange data "data"' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.change data "data"4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

演示案例

代码语言:javascript代码运行次数:0运行复制

将data1中的a b c字符转换为对应的 A  B  C字符[root@zutuanxue ~]# sed 'y/abc/ABC/' data11 the quiCk Brown fox jumps over the lAzy dog.2 the quiCk Brown fox jumps over the lAzy dog.3 the quiCk Brown fox jumps over the lAzy dog.4 the quiCk Brown fox jumps over the lAzy dog.5 the quiCk Brown fox jumps over the lAzy dog.

文件内容删除,将文件中的指定数据删除,使用命令d。

演示案例

代码语言:javascript代码运行次数:0运行复制

删除文件data1中的所有数据[root@zutuanxue ~]# sed 'd' data1删除文件data1中的第三行数据[root@zutuanxue ~]# sed '3d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.删除文件data1第三到第四行的数据[root@zutuanxue ~]# sed '3,4d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.删除文件data1中包含字符串"3 the"的行[root@zutuanxue ~]# sed '/3 the/d' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

文件内容查看,将文件内容输出到屏幕,使用命令p。

讯飞写作 讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

讯飞写作 56 查看详情 讯飞写作

演示案例

代码语言:javascript代码运行次数:0运行复制

打印data1文件内容[root@zutuanxue ~]# sed 'p' data11 the quick brown fox jumps over the lazy dog.1 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件第三行的内容[root@zutuanxue ~]# sed '3p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件第二、三、四行内容[root@zutuanxue ~]# sed '2,4p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.打印data1文件包含字符串"3 the"的行[root@zutuanxue ~]# sed '/3 the/p' data11 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog. 可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果,如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。

2.2)命令选项说明

sed语法 在sed命令中,命令选项是对sed中的命令的增强

在命令行中使用多个命令 -e

代码语言:javascript代码运行次数:0运行复制

将brown替换为green dog替换为cat[root@zutuanxue ~]# sed -e 's/brown/green/;s/dog/cat/' data11 the quick green fox jumps over the lazy cat.2 the quick green fox jumps over the lazy cat.3 the quick green fox jumps over the lazy cat.4 the quick green fox jumps over the lazy cat.5 the quick green fox jumps over the lazy cat.

从文件读取编辑器命令 -f 适用于日常重复执行的场景

代码语言:javascript代码运行次数:0运行复制

1)将命令写入文件[root@zutuanxue ~]# vim abcs/brown/green/s/dog/cat/s/fox/elephant/2)使用-f命令选项调用命令文件[root@zutuanxue ~]# sed -f abc data1 1 the quick green elephant jumps over the lazy cat.2 the quick green elephant jumps over the lazy cat.3 the quick green elephant jumps over the lazy cat.4 the quick green elephant jumps over the lazy cat.5 the quick green elephant jumps over the lazy cat.

抑制内存输出 -n

代码语言:javascript代码运行次数:0运行复制

打印data1文件的第二行到最后一行内容  $最后的意思[root@zutuanxue ~]# sed -n '2,$p' data1 2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

使用正则表达式 -r

代码语言:javascript代码运行次数:0运行复制

打印data1中以字符串"3 the"开头的行内容[root@zutuanxue ~]# sed -n  -r '/^(3 the)/p' data13 the quick brown fox jumps over the lazy dog.

从上述的演示中,大家可以看出,数据处理只是在缓存中完成的,并没有实际修改文件内容,如果需要修改文件内容可以直接使用-i命令选项。在这里我需要说明的是-i是一个不可逆的操作,一旦修改,如果想复原就很困难,几乎不可能,所以建议大家在操作的时候可以备份一下源文件。-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。

代码语言:javascript代码运行次数:0运行复制

1)查看文件列表,没有发现data1.bak[root@zutuanxue ~]# lsabc  apache  data1  Dobby  file  node-v10.14.1  Python-3.7.1  soft1  vimset2)执行替换命令并修改文件[root@zutuanxue ~]# sed -i.bak 's/brown/green/' data13)发现文件夹中多了一个data1.bak文件[root@zutuanxue ~]# lsabc     data1      Dobby  node-v10.14.1  soft1apache  data1.bak  file   Python-3.7.1   vimset4)打印比较一下,发现data1已经被修改,data1.bak是源文件的备份。[root@zutuanxue ~]# cat data11 the quick green fox jumps over the lazy dog.2 the quick green fox jumps over the lazy dog.3 the quick green fox jumps over the lazy dog.4 the quick green fox jumps over the lazy dog.5 the quick green fox jumps over the lazy dog.[root@zutuanxue ~]# cat data1.bak 1 the quick brown fox jumps over the lazy dog.2 the quick brown fox jumps over the lazy dog.3 the quick brown fox jumps over the lazy dog.4 the quick brown fox jumps over the lazy dog.5 the quick brown fox jumps over the lazy dog.

2.3)标志

在sed命令中,标志是对sed中的内部命令做补充说明

代码语言:javascript代码运行次数:0运行复制

演示文档[root@zutuanxue ~]# cat data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy dog . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog

数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。

代码语言:javascript代码运行次数:0运行复制

替换一行中的第二处dog为cat[root@zutuanxue ~]# sed 's/dog/cat/2' data21 the quick brown fox jumps over the lazy dog . cat2 the quick brown fox jumps over the lazy dog . cat3 the quick brown fox jumps over the lazy dog . cat4 the quick brown fox jumps over the lazy dog . cat5 the quick brown fox jumps over the lazy dog . cat

g标志:将一行中的所有符合的字符串全部执行替换

代码语言:javascript代码运行次数:0运行复制

将data1文件中的所有dog替换为cat[root@zutuanxue ~]# sed 's/dog/cat/g' data21 the quick brown fox jumps over the lazy cat . cat2 the quick brown fox jumps over the lazy cat . cat3 the quick brown fox jumps over the lazy cat . cat4 the quick brown fox jumps over the lazy cat . cat5 the quick brown fox jumps over the lazy cat . cat

p标志:打印文本内容,类似于-p命令选项

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# sed  '3s/dog/cat/p' data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy cat . dog3 the quick brown fox jumps over the lazy cat . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog

w filename标志:将修改的内容存入filename文件中

代码语言:javascript代码运行次数:0运行复制

[root@zutuanxue ~]# sed  '3s/dog/cat/w text' data21 the quick brown fox jumps over the lazy dog . dog2 the quick brown fox jumps over the lazy dog . dog3 the quick brown fox jumps over the lazy cat . dog4 the quick brown fox jumps over the lazy dog . dog5 the quick brown fox jumps over the lazy dog . dog可以看出,将修改的第三行内容存在了text文件中[root@zutuanxue ~]# cat text 3 the quick brown fox jumps over the lazy cat . dog

三、练习案例

3.1、写一个初始化系统的脚本 案例需求 1)自动修改主机名(如:ip是192.168.0.88,则主机名改为server88.zutuanxue.cc)

a. 更改文件非交互式 sed

/etc/sysconfig/network

b.将本主机的IP截取出来赋值给一个变量ip;再然后将ip变量里以.分割的最后一位赋值给另一个变量ip1

2)自动配置可用的yum源

3)自动关闭防火墙和selinux

代码语言:javascript代码运行次数:0运行复制

1、关闭防火墙2、关闭selinux3、配置yum源4、ntp时间服务器 5、更新系统软件包

3.2、写一个搭建ftp服务的脚本,要求如下: 案例需求 1)不支持本地用户登录local_enable=NO 2) 匿名用户可以上传 新建 删除 anon_upload_enable=YES anon_mkdir_write_enable=YES 3) 匿名用户限速500KBps anon_max_rate=500000

代码语言:javascript代码运行次数:0运行复制

仅供参考:#!/bin/bashipaddr=`ifconfig eth0|sed -n '2p'|sed -e 's/.*inet addr:(.*) Bcast.*/1/g'`iptail=`echo $ipaddr|cut -d'.' -f4`ipremote=192.168.1.10#修改主机名hostname server$iptail.zutuanxue.comsed -i "/HOSTNAME/cHOSTNAME=server$iptail.zutuanxue.com" /etc/sysconfig/networkecho "$ipaddr server$iptail.zutuanxue.cc" >>/etc/hosts#关闭防火墙和selinuxservice iptables stopsetenforce 0 >/dev/null 2>&1sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config#配置yum源(一般是内网源)#test networkping -c 1 $ipremote > /dev/null 2>&1if [ $? -ne 0 ];thenecho "你的网络不通,请先检查你的网络"exit 1elseecho "网络ok."ficat > /etc/yum.repos.d/server.repo </dev/null#备份配置文件conf=/etc/vsftpd/vsftpd.confcp $conf $conf.default#根据需求修改配置文件sed -ir '/^#|^$/d' $confsed -i '/local_enable/clocal_enable=NO' $confsed -i '$a anon_upload_enable=YES' $confsed -i '$a anon_mkdir_write_enable=YES' $confsed -i '$a anon_other_write_enable=YES' $confsed -i '$a anon_max_rate=512000' $conf#启动服务service vsftpd restart &>/dev/null && echo"vsftpd服务启动成功"#测试验证chmod 777 /var/ftp/pubcp /etc/hosts /var/ftp/pub#测试下载cd /tmplftp $ipaddr <<endcd pubget hostsexitendif [ -f /tmp/hosts ];thenecho "匿名用户下载成功"rm -f /tmp/hostselseecho "匿名用户下载失败"fi#测试上传、创建目录、删除目录等cd /tmplftp $ipaddr << endcd pubmkdir test1mkdir test2put /etc/grouprmdir test2exitendif [ -d /var/ftp/pub/test1 ];then    echo "创建目录成功"if [ ! -d /var/ftp/pub/test2 ];then    echo "文件删除成功"        fielseif [ -f /var/ftp/pub/group ];thenecho "文件上传成功"        else        echo "上传、创建目录删除目录部ok"        fi fi   [ -f /var/ftp/pub/group ] && echo "上传文件成功"

以上就是shell对文件的操作的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/459767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
安迪不在家,不知火舞只能靠他了
上一篇 2025年11月8日 03:03:36
Windows暂停更新方法
下一篇 2025年11月8日 03:03:42

相关推荐

  • composer require-dev和require有什么不同_Composer Require与Require-Dev区别解析

    require用于声明项目运行必需的依赖,如框架、数据库组件和第三方SDK,这些包会随项目部署到生产环境;2. require-dev用于声明仅在开发和测试阶段需要的工具,如PHPUnit、PHPStan、Faker等,不会默认部署到生产环境;3. 安装时composer install根据环境决定…

    2026年5月10日
    1000
  • Matplotlib 地图中多类型图例的创建与优化

    Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化

    本教程旨在解决matplotlib地图可视化中,如何在一个图例中同时展示颜色块(如区域分类)和自定义标记(如特定兴趣点)的问题。文章详细介绍了当传统`patch`对象无法正确显示标记时,如何利用`matplotlib.lines.line2d`创建标记图例句柄,并将其与颜色块图例句柄合并,从而生成一…

    2026年5月10日 用户投稿
    100
  • Golang JSON序列化:控制敏感字段暴露的最佳实践

    本教程探讨golang中如何高效控制结构体字段在json序列化时的可见性。当需要将包含敏感信息的结构体数组转换为json响应时,通过利用`encoding/json`包提供的结构体标签,特别是`json:”-“`,可以轻松实现对特定字段的忽略,从而避免敏感数据泄露,确保api…

    2026年5月10日
    000
  • 利用海象运算符简化条件赋值:Python教程与最佳实践

    本文旨在探讨Python中海象运算符(:=)在条件赋值场景下的应用。通过对比传统if/else语句与海象运算符,以及条件表达式,分析海象运算符在简化代码、提高可读性方面的优势与局限性。并通过具体示例,展示如何在列表推导式等场景下合理使用海象运算符,同时强调其潜在的复杂性及替代方案,帮助开发者更好地掌…

    2026年5月10日
    100
  • 怎么在PHP代码中实现图片上传功能_PHP图片上传功能实现与安全处理教程

    首先创建含enctype的HTML表单,再用PHP接收文件,检查目录、移动临时文件,验证类型与大小,生成唯一文件名,并调整php.ini限制以确保上传成功。 如果您尝试在PHP项目中添加图片上传功能,但服务器无法正确接收或保存文件,则可能是由于表单配置、文件处理逻辑或安全限制的问题。以下是实现该功能…

    2026年5月10日
    100
  • 比特币新手教程 比特币交易平台有哪些

    比特币是一种去中心化的数字货币,基于区块链技术实现点对点交易,具有匿名性、有限发行和不可篡改等特点;新手可通过交易所购买,P2P交易获得比特币,常用平台包括Binance、OKX和Huobi;交易流程包括注册账户、实名认证、绑定支付方式、充值法币并下单购买,可选择市价单或限价单;比特币存储方式有交易…

    2026年5月10日
    000
  • c++中的SFINAE技术是什么_c++模板编程中的SFINAE原理与应用

    SFINAE 是“替换失败不是错误”的原则,指模板实例化时若参数替换导致错误,只要存在其他合法候选,编译器不报错而是继续重载决议。它用于条件启用模板、类型检测等场景,如通过 decltype 或 enable_if 控制函数重载,实现类型特征判断。尽管 C++20 引入 Concepts 简化了部分…

    2026年5月10日
    000
  • Go语言mgo查询构建:深入理解bson.M与日期范围查询的正确实践

    本文旨在解决go语言mgo库中构建复杂查询时,特别是涉及嵌套`bson.m`和日期范围筛选的常见错误。我们将深入剖析`bson.m`的类型特性,解释为何直接索引`interface{}`会导致“invalid operation”错误,并提供一种推荐的、结构清晰的代码重构方案,以确保查询条件能够正确…

    2026年5月10日
    100
  • RichHandler与Rich Progress集成:解决显示冲突的教程

    在使用rich库的`richhandler`进行日志输出并同时使用`progress`组件时,可能会遇到显示错乱或溢出问题。这通常是由于为`richhandler`和`progress`分别创建了独立的`console`实例导致的。解决方案是确保日志处理器和进度条组件共享同一个`console`实例…

    2026年5月10日
    000
  • Golang goroutine与channel调试技巧

    使用go run -race检测数据竞争,结合runtime.NumGoroutine监控协程数量,通过pprof分析阻塞调用栈,利用select超时避免永久阻塞,有效排查goroutine泄漏、死锁和数据竞争问题。 Go语言的goroutine和channel是并发编程的核心,但它们也带来了调试上…

    2026年5月10日
    000
  • 使用 Jupyter Notebook 进行探索性数据分析

    Jupyter Notebook通过单元格实现代码与Markdown结合,支持数据导入(pandas)、清洗(fillna)、探索(matplotlib/seaborn可视化)、统计分析(describe/corr)和特征工程,便于记录与分享分析过程。 Jupyter Notebook 是进行探索性…

    2026年5月10日
    000
  • 《魔兽世界》将于6月11日开启国服回归技术测试

    《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试

    《%ign%ignore_a_1%re_a_1%》官方宣布,将于6月11日开启国服回归技术测试,时间为7天,并称可以在6月内正式开服,玩家们可以访问官网下载战网客户端并预下载“巫妖王之怒”客户端,技术测试详情见下图。 WordAi WordAI是一个AI驱动的内容重写平台 53 查看详情 以上就是《…

    2026年5月10日 用户投稿
    200
  • php常量怎么用_PHP常量(define/const)定义与使用方法

    PHP中可通过define函数和const关键字定义常量,用于存储不可变值。define适用于全局作用域,支持动态名称和条件定义,如define(‘SITE_NAME’, ‘MyWebsite’);const在编译时生效,语法简洁但限制多,只能在类或全…

    2026年5月10日
    000
  • 如何在HTML中插入表单元素_HTML表单控件与输入类型使用指南

    HTML表单通过标签构建,包含action和method属性定义数据提交目标与方式,常用input类型如text、password、email等适配不同输入需求,配合label、required、placeholder提升可用性,结合textarea、select、button等控件实现完整交互,是…

    2026年5月10日
    100
  • 创建指定大小并填充特定数据的Golang文件教程

    本文将介绍如何使用Golang创建一个指定大小的文件,并用特定数据填充它。我们将使用 `os` 包提供的函数来创建和截断文件,从而实现快速生成大文件的目的。示例代码展示了如何创建一个10MB的文件,并将其填充为全零数据。掌握这些方法,可以方便地在例如日志系统或磁盘队列等场景中,预先创建测试文件或初始…

    2026年5月10日
    000
  • Python命令怎样使用profile分析脚本性能 Python命令性能分析的基础教程

    使用Python的cProfile模块分析脚本性能最直接的方式是通过命令行执行python -m cProfile your_script.py,它会输出每个函数的调用次数、总耗时、累积耗时等关键指标,帮助定位性能瓶颈;为进一步分析,可将结果保存为文件python -m cProfile -o ou…

    2026年5月10日
    000
  • 使用 WebCodecs VideoDecoder 实现精确逐帧回退

    本文档旨在解决在使用 WebCodecs VideoDecoder 进行视频解码时,实现精确逐帧回退的问题。通过比较帧的时间戳与目标帧的时间戳,可以避免渲染中间帧,从而提高用户体验。本文将提供详细的解决方案和示例代码,帮助开发者实现精确的视频帧控制。 在使用 WebCodecs VideoDecod…

    2026年5月10日
    000
  • 如何插入查询结果数据_SQL插入Select查询结果方法

    如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法

    使用INSERT INTO…SELECT语句可高效插入数据,通过NOT EXISTS、LEFT JOIN、MERGE语句或唯一约束避免重复;表结构不一致时可通过别名、类型转换、默认值或计算字段处理;结合存储过程可提升可维护性,支持参数化与动态SQL。 将查询结果数据插入到另一个表中,可以…

    2026年5月10日 用户投稿
    000
  • Discord.py 交互按钮超时与持久化解决方案

    本教程旨在解决Discord.py中交互按钮在一段时间后出现“This Interaction Failed”错误的问题。我们将深入探讨视图(View)的超时机制,并提供通过正确设置timeout参数以及利用bot.add_view()方法实现按钮持久化的具体方案,确保您的机器人交互功能稳定可靠,即…

    2026年5月10日
    000
  • Debian Copilot的社区活跃度如何

    debian copilot是codeberg社区维护的ai助手,旨在为debian用户提供服务。尽管搜索结果中没有直接提供关于debian copilot社区支持活跃度的具体数据,但我们可以通过debian社区的整体活跃度和特点来推断其活跃性。 Debian社区的一般情况: Debian拥有详尽的…

    2026年5月10日
    000

发表回复

登录后才能评论
关注微信