全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 626|回复: 7

shell 脚本大佬进

[复制链接]
发表于 2023-2-15 14:07:48 | 显示全部楼层 |阅读模式
  1. 1231231
  2.    \
  3.           \
  4. 32345sdgf
  5. 023jlj \
  6. adsasd \
  7. ljd  \sad \\\ad   \
复制代码


我想删除第2和第3(不限于这 2 行,不能用行数确定)这种只包括‘空格’和 '\' 反斜杠的这种行,用什么办法好呢?
发表于 2023-2-15 14:41:15 | 显示全部楼层
chatgpt来给你回答

如果您要删除只包含空格和反斜杠的行,可以使用以下命令:


sed -i '/^[ \\\]*$/d' filename
该命令使用正则表达式匹配只包含空格和反斜杠的行,并将其删除。具体来说,^表示行的开头,[ \\\]*表示0个或多个空格或反斜杠,$表示行的结尾。d表示删除匹配的行。filename是您要操作的文件名。
发表于 2023-2-15 14:16:59 | 显示全部楼层
shell只能简单操作一下linux系统,你这种需要用编程语言的正则表达式去匹配吧
发表于 2023-2-15 14:27:42 | 显示全部楼层
vim::g/^[\s|\t]*\\/d    没有测试,自己再改改。
发表于 2023-2-15 14:28:45 | 显示全部楼层
hihandbag 发表于 2023-2-15 14:27
vim::g/^[\s|\t]*\\/d    没有测试,自己再改改。

把* 去掉。要不然后面2行也删除了。
发表于 2023-2-15 14:40:25 | 显示全部楼层
本帖最后由 mmc199 于 2023-2-15 14:49 编辑

你这个问题,问chatgpt就能解决他会帮你写出脚本
Write a bash script that can delete lines containing only backslashes or spaces in text files
  1. #!/bin/bash

  2. # loop through each text file in the directory
  3. for file in *.txt; do
  4.     # create a temporary file to store the updated content
  5.     tmp_file=$(mktemp)

  6.     # read each line of the file and delete lines that only contain backslashes or spaces
  7.     while read -r line; do
  8.         if [[ "$line" =~ ^[\\[:space:]]*$ ]]; then
  9.             continue
  10.         else
  11.             echo "$line" >> "$tmp_file"
  12.         fi
  13.     done < "$file"

  14.     # overwrite the original file with the updated content
  15.     mv "$tmp_file" "$file"
  16. done
复制代码
 楼主| 发表于 2023-2-15 14:53:40 | 显示全部楼层
哎呀 发表于 2023-2-15 14:41
chatgpt来给你回答

如果您要删除只包含空格和反斜杠的行,可以使用以下命令:

哈哈,我咋把 chatgpt 忘了呢
  1. sed -n '/^[ \]*[ \]$/p'
复制代码

自己想了一个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-1-22 16:05 , Processed in 0.063038 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表