Linux替换文件字符串和合并文件命令

心已赠人 2024-04-08 09:22 148阅读 0赞

一、Linux 替换文件中指定的字符串命令

  1. sed -i 's/原字符串s1/新字符串s2/g' defult.cfg 替换当前行所有s1s2
  2. sed -i 's/原字符串s1/新字符串s2/' defult.cfg 替换当前行第一个s1s2
  3. 场景:将code目录下面所有java后缀文件,把文件内容里面含有 tttt 的,换成 kkkk
(1)找到code目录,发现里面既有java文件,也有xml和目录
  1. -bash-4.1# cd code/
  2. -bash-4.1# ll
  3. total 184
  4. drwxr-xr-x 8 root root 4096 Aug 16 18:06 coreProcess
  5. drwxr-xr-x 4 root root 4096 Aug 16 18:06 disasterRecovery
  6. -rw-r--r-- 1 root root 4981 Aug 17 09:30 EmergencyNodeInfoController.java
  7. -rw-r--r-- 1 root root 4090 Aug 17 09:30 EmergencyNodeInfo.java
  8. -rw-r--r-- 1 root root 1572 Aug 17 09:30 EmergencyNodeInfoMapper.java
  9. -rw-r--r-- 1 root root 5567 Aug 16 18:06 EmergencyNodeInfoMapper.xml
  10. -rw-r--r-- 1 root root 17590 Aug 17 09:30 EmergencyNodeInfoServiceImpl.java
  11. -rw-r--r-- 1 root root 7604 Aug 17 09:30 EmergencyNodeOperateServiceImpl.java
  12. drwxr-xr-x 2 root root 4096 Aug 16 18:06 emergencyProcess
  13. -rw-r--r-- 1 root root 3969 Aug 17 09:30 EmergencyProcessStateController.java
  14. -rw-r--r-- 1 root root 1351 Aug 17 09:30 EmergencyProcessState.java
  15. -rw-r--r-- 1 root root 1591 Aug 17 09:30 EmergencyProcessStateMapper.java
  16. -rw-r--r-- 1 root root 2637 Aug 16 18:06 EmergencyProcessStateMapper.xml
  17. -rw-r--r-- 1 root root 2862 Aug 17 09:30 EmergencyProcessStateServiceImpl.java
  18. -rw-r--r-- 1 root root 4563 Aug 17 09:30 EmergencySwitchProcessInfoController.java
  19. -rw-r--r-- 1 root root 3593 Aug 17 09:30 EmergencySwitchProcessInfo.java
  20. -rw-r--r-- 1 root root 1852 Aug 17 09:30 EmergencySwitchProcessInfoMapper.java
  21. -rw-r--r-- 1 root root 5376 Aug 16 18:06 EmergencySwitchProcessInfoMapper.xml
  22. -rw-r--r-- 1 root root 12033 Aug 17 09:30 EmergencySwitchProcessInfoServiceImpl.java
  23. -rw-r--r-- 1 root root 4441 Aug 17 09:30 EmergencyTaskLogController.java
  24. -rw-r--r-- 1 root root 4484 Aug 17 09:30 EmergencyTaskLog.java
  25. -rw-r--r-- 1 root root 1947 Aug 17 09:30 EmergencyTaskLogMapper.java
  26. -rw-r--r-- 1 root root 6737 Aug 16 18:06 EmergencyTaskLogMapper.xml
  27. -rw-r--r-- 1 root root 6347 Aug 17 09:30 EmergencyTaskLogServiceImpl.java
  28. -rw-r--r-- 1 root root 322 Aug 17 09:30 ExecState.java
  29. drwxr-xr-x 2 root root 4096 Aug 16 18:06 flowChart
  30. -rw-r--r-- 1 root root 2364 Aug 17 09:30 IEmergencyNodeInfoService.java
  31. -rw-r--r-- 1 root root 323 Aug 17 09:30 IEmergencyNodeOperateService.java
  32. -rw-r--r-- 1 root root 1674 Aug 17 09:30 IEmergencyProcessStateService.java
  33. -rw-r--r-- 1 root root 2082 Aug 17 09:30 IEmergencySwitchProcessInfoService.java
  34. -rw-r--r-- 1 root root 2575 Aug 17 09:30 IEmergencyTaskLogService.java
  35. -bash-4.1#
(2)打开某一个java文件,看一下里面的内容,使用more命令,我们发现里面确实有很多的含有tttt的字符串
  1. -bash-4.1# more EmergencyNodeInfoController.java
  2. package com.hidata.devops.emergency.controller;
  3. import com.hidata.devops.emergency.domain.EmergencyNodeInfo;
  4. import com.hidata.devops.emergency.service.IEmergencyNodeInfoService;
  5. import com.tttt.common.annotation.Log;
  6. import com.tttt.common.core.controller.BaseController;
  7. import com.tttt.common.core.domain.AjaxResult;
  8. import com.tttt.common.core.page.TableDataInfo;
  9. import com.tttt.common.enums.BusinessType;
  10. import com.tttt.common.utils.SecurityUtils;
  11. import com.tttt.common.utils.poi.ExcelUtil;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.web.bind.annotation.*;
  15. import java.util.List;
  16. .....................此处省略一万字
(3)接下来,我们使用替换字符串命令 :sed -i ‘s/tttt/kkkk/g’ *.java ,具体操作如下:
  1. -bash-4.1# sed -i 's/tttt/kkkk/g' *.java
  2. -bash-4.1# more EmergencyNodeInfoController.java
  3. package com.hidata.devops.emergency.controller;
  4. import com.hidata.devops.emergency.domain.EmergencyNodeInfo;
  5. import com.hidata.devops.emergency.service.IEmergencyNodeInfoService;
  6. import com.kkkk.common.annotation.Log;
  7. import com.kkkk.common.core.controller.BaseController;
  8. import com.kkkk.common.core.domain.AjaxResult;
  9. import com.kkkk.common.core.page.TableDataInfo;
  10. import com.kkkk.common.enums.BusinessType;
  11. import com.kkkk.common.utils.SecurityUtils;
  12. import com.kkkk.common.utils.poi.ExcelUtil;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.List;

这样字符串tttt就都被替换成kkkk了。

二、将多个文件内容合并到一个新文件

  1. 场景:接着上面的说,刚刚已经把java后缀文件里面内容都已经替换完毕了,现在想把所有的java后缀文件,合并到一个新的文件中,具体操作如下:
(1)首先我们要创建一个新的文件,用来存放合并的内容:touch aaaa.txt
  1. drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis
  2. drwxr-xr-x 6 root root 4096 Aug 17 09:42 code
  3. drwxr-xr-x 2 root root 24576 Aug 12 15:53 download
  4. drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue
  5. -rw------- 1 root root 40314 Dec 28 2021 nohup.out
  6. drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub
  7. drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload
  8. -bash-4.1# touch aaaa.txt
  9. -bash-4.1# ll
  10. total 84
  11. -rw-r--r-- 1 root root 0 Aug 17 09:47 aaaa.txt
  12. drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis
  13. drwxr-xr-x 6 root root 4096 Aug 17 09:42 code
  14. drwxr-xr-x 2 root root 24576 Aug 12 15:53 download
  15. drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue
  16. -rw------- 1 root root 40314 Dec 28 2021 nohup.out
  17. drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub
  18. drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload
  19. -bash-4.1#
(2)执行合并命令:cat code/*.java >> aaaa.txt
  1. -bash-4.1# ll
  2. total 84
  3. -rw-r--r-- 1 root root 0 Aug 17 09:47 aaaa.txt
  4. drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis
  5. drwxr-xr-x 6 root root 4096 Aug 17 09:42 code
  6. drwxr-xr-x 2 root root 24576 Aug 12 15:53 download
  7. drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue
  8. -rw------- 1 root root 40314 Dec 28 2021 nohup.out
  9. drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub
  10. drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload
  11. -bash-4.1# cat code/*.java >> aaaa.txt
  12. -bash-4.1# ll
  13. total 180
  14. -rw-r--r-- 1 root root 94210 Aug 17 09:48 aaaa.txt
  15. drwxr-xr-x 4 root root 4096 Jul 15 10:54 amis
  16. drwxr-xr-x 6 root root 4096 Aug 17 09:42 code
  17. drwxr-xr-x 2 root root 24576 Aug 12 15:53 download
  18. drwxr-xr-x 4 root root 4096 Aug 15 15:57 hidbm-vue
  19. -rw------- 1 root root 40314 Dec 28 2021 nohup.out
  20. drwxr-xr-x 3 root root 4096 Jan 26 2022 srvhub
  21. drwxr-xr-x 2 root root 4096 Aug 12 08:57 upload
  22. -bash-4.1#

我们发现原来的aaaa.txt文件的大小为0,执行完合并命令后,他的大小变成了94210 ,说明合并成功了

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux——远程替换文件

      在维护项目的过程中,如果修改文件也是难免的,修改有些文件需要重启服务,有些不需要,如后缀是.jsp的文件是不需要重启服务的,将修改后的.jsp文件远程替换一下即可。