一、Linux 替换文件中指定的字符串命令
sed -i 's/原字符串s1/新字符串s2/g' defult.cfg 替换当前行所有s1为s2 sed -i 's/原字符串s1/新字符串s2/' defult.cfg 替换当前行第一个s1为s2
场景:将code目录下面所有java后缀文件,把文件内容里面含有 tttt 的,换成 kkkk
(1)找到code目录,发现里面既有java文件,也有xml和目录
-bash-4.1# cd code/ -bash-4.1# ll total 184 drwxr-xr-x 8 root root 4096 Aug 16 18:06 coreProcess drwxr-xr-x 4 root root 4096 Aug 16 18:06 disasterRecovery -rw-r--r-- 1 root root 4981 Aug 17 09:30 EmergencyNodeInfoController.java -rw-r--r-- 1 root root 4090 Aug 17 09:30 EmergencyNodeInfo.java -rw-r--r-- 1 root root 1572 Aug 17 09:30 EmergencyNodeInfoMapper.java -rw-r--r-- 1 root root 5567 Aug 16 18:06 EmergencyNodeInfoMapper.xml -rw-r--r-- 1 root root 17590 Aug 17 09:30 EmergencyNodeInfoServiceImpl.java -rw-r--r-- 1 root root 7604 Aug 17 09:30 EmergencyNodeOperateServiceImpl.java drwxr-xr-x 2 root root 4096 Aug 16 18:06 emergencyProcess -rw-r--r-- 1 root root 3969 Aug 17 09:30 EmergencyProcessStateController.java -rw-r--r-- 1 root root 1351 Aug 17 09:30 EmergencyProcessState.java -rw-r--r-- 1 root root 1591 Aug 17 09:30 EmergencyProcessStateMapper.java -rw-r--r-- 1 root root 2637 Aug 16 18:06 EmergencyProcessStateMapper.xml -rw-r--r-- 1 root root 2862 Aug 17 09:30 EmergencyProcessStateServiceImpl.java -rw-r--r-- 1 root root 4563 Aug 17 09:30 EmergencySwitchProcessInfoController.java -rw-r--r-- 1 root root 3593 Aug 17 09:30 EmergencySwitchProcessInfo.java -rw-r--r-- 1 root root 1852 Aug 17 09:30 EmergencySwitchProcessInfoMapper.java -rw-r--r-- 1 root root 5376 Aug 16 18:06 EmergencySwitchProcessInfoMapper.xml -rw-r--r-- 1 root root 12033 Aug 17 09:30 EmergencySwitchProcessInfoServiceImpl.java -rw-r--r-- 1 root root 4441 Aug 17 09:30 EmergencyTaskLogController.java -rw-r--r-- 1 root root 4484 Aug 17 09:30 EmergencyTaskLog.java -rw-r--r-- 1 root root 1947 Aug 17 09:30 EmergencyTaskLogMapper.java -rw-r--r-- 1 root root 6737 Aug 16 18:06 EmergencyTaskLogMapper.xml -rw-r--r-- 1 root root 6347 Aug 17 09:30 EmergencyTaskLogServiceImpl.java -rw-r--r-- 1 root root 322 Aug 17 09:30 ExecState.java drwxr-xr-x 2 root root 4096 Aug 16 18:06 flowChart -rw-r--r-- 1 root root 2364 Aug 17 09:30 IEmergencyNodeInfoService.java -rw-r--r-- 1 root root 323 Aug 17 09:30 IEmergencyNodeOperateService.java -rw-r--r-- 1 root root 1674 Aug 17 09:30 IEmergencyProcessStateService.java -rw-r--r-- 1 root root 2082 Aug 17 09:30 IEmergencySwitchProcessInfoService.java -rw-r--r-- 1 root root 2575 Aug 17 09:30 IEmergencyTaskLogService.java -bash-4.1#
(2)打开某一个java文件,看一下里面的内容,使用more命令,我们发现里面确实有很多的含有tttt的字符串
-bash-4.1# more EmergencyNodeInfoController.java package com.hidata.devops.emergency.controller; import com.hidata.devops.emergency.domain.EmergencyNodeInfo; import com.hidata.devops.emergency.service.IEmergencyNodeInfoService; import com.tttt.common.annotation.Log; import com.tttt.common.core.controller.BaseController; import com.tttt.common.core.domain.AjaxResult; import com.tttt.common.core.page.TableDataInfo; import com.tttt.common.enums.BusinessType; import com.tttt.common.utils.SecurityUtils; import com.tttt.common.utils.poi.ExcelUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.List; .....................此处省略一万字
(3)接下来,我们使用替换字符串命令 :sed -i ‘s/tttt/kkkk/g’ *.java ,具体操作如下:
-bash-4.1# sed -i 's/tttt/kkkk/g' *.java -bash-4.1# more EmergencyNodeInfoController.java package com.hidata.devops.emergency.controller; import com.hidata.devops.emergency.domain.EmergencyNodeInfo; import com.hidata.devops.emergency.service.IEmergencyNodeInfoService; import com.kkkk.common.annotation.Log; import com.kkkk.common.core.controller.BaseController; import com.kkkk.common.core.domain.AjaxResult; import com.kkkk.common.core.page.TableDataInfo; import com.kkkk.common.enums.BusinessType; import com.kkkk.common.utils.SecurityUtils; import com.kkkk.common.utils.poi.ExcelUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.List;
这样字符串tttt就都被替换成kkkk了。
二、将多个文件内容合并到一个新文件
场景:接着上面的说,刚刚已经把java后缀文件里面内容都已经替换完毕了,现在想把所有的java后缀文件,合并到一个新的文件中,具体操作如下:
(1)首先我们要创建一个新的文件,用来存放合并的内容:touch aaaa.txt
drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis drwxr-xr-x 6 root root 4096 Aug 17 09:42 code drwxr-xr-x 2 root root 24576 Aug 12 15:53 download drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue -rw------- 1 root root 40314 Dec 28 2021 nohup.out drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload -bash-4.1# touch aaaa.txt -bash-4.1# ll total 84 -rw-r--r-- 1 root root 0 Aug 17 09:47 aaaa.txt drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis drwxr-xr-x 6 root root 4096 Aug 17 09:42 code drwxr-xr-x 2 root root 24576 Aug 12 15:53 download drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue -rw------- 1 root root 40314 Dec 28 2021 nohup.out drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload -bash-4.1#
(2)执行合并命令:cat code/*.java >> aaaa.txt
-bash-4.1# ll total 84 -rw-r--r-- 1 root root 0 Aug 17 09:47 aaaa.txt drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis drwxr-xr-x 6 root root 4096 Aug 17 09:42 code drwxr-xr-x 2 root root 24576 Aug 12 15:53 download drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue -rw------- 1 root root 40314 Dec 28 2021 nohup.out drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload -bash-4.1# cat code/*.java >> aaaa.txt -bash-4.1# ll total 180 -rw-r--r-- 1 root root 94210 Aug 17 09:48 aaaa.txt drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis drwxr-xr-x 6 root root 4096 Aug 17 09:42 code drwxr-xr-x 2 root root 24576 Aug 12 15:53 download drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue -rw------- 1 root root 40314 Dec 28 2021 nohup.out drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload -bash-4.1#
我们发现原来的aaaa.txt文件的大小为0,执行完合并命令后,他的大小变成了94210 ,说明合并成功了