Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何对具有不同字母的文件进行grep?_Linux_Bash_Shell - Fatal编程技术网

Linux 如何对具有不同字母的文件进行grep?

Linux 如何对具有不同字母的文件进行grep?,linux,bash,shell,Linux,Bash,Shell,我在一个名为:abc.txt srr.txt eek.txt abb.txt等目录中有数千个文件。我只想grep那些最后两个字母不同的文件。例如: 输出良好:abc.txt eek.txt 输出错误:ekk.txt dee.txt。 以下是我试图做的: #/bin/bash ls-l目录| grep.txt 它会对每个包含.txt的文件进行greps 如何对具有两个不同最后字母的文件进行grep?解析ls的结果不是一个好主意(阅读本文了解原因)。以下是您可以在纯Bash中执行的操作,无需使用任何

我在一个名为:
abc.txt srr.txt eek.txt abb.txt
等目录中有数千个文件。我只想grep那些最后两个字母不同的文件。例如:

输出良好:
abc.txt eek.txt
输出错误:
ekk.txt dee.txt
。 以下是我试图做的:

#/bin/bash
ls-l目录| grep.txt

它会对每个包含
.txt
的文件进行greps


如何对具有两个不同最后字母的文件进行grep?

解析
ls
的结果不是一个好主意(阅读本文了解原因)。以下是您可以在纯Bash中执行的操作,无需使用任何外部命令:

#!/bin/bash

shop -s nullglob                                                  # make sure glob yields nothing if there are no matches
for file in *.txt; do                                             # grab all .txt files
  [[ -f $file ]] || continue                                      # skip if not a regular file
  last6="${file: -6}"                                             # get the last 6 characters of file name
  [[ "${last6:1:1}" != "${last6:2:1}" ]] && printf '%s\n' "$file" # pick the files that match the criteria
  # change printf to mv "$file" "$target_dir" above if you want to move the files
done

解析
ls
的结果不是一个好主意(阅读本文了解原因)。以下是您可以在纯Bash中执行的操作,无需使用任何外部命令:

#!/bin/bash

shop -s nullglob                                                  # make sure glob yields nothing if there are no matches
for file in *.txt; do                                             # grab all .txt files
  [[ -f $file ]] || continue                                      # skip if not a regular file
  last6="${file: -6}"                                             # get the last 6 characters of file name
  [[ "${last6:1:1}" != "${last6:2:1}" ]] && printf '%s\n' "$file" # pick the files that match the criteria
  # change printf to mv "$file" "$target_dir" above if you want to move the files
done

我似乎通过使用以下工具实现了我想要的:

ls-l | awk'{print$9}'| grep-vE“(..?\1.?\”

  • awk'{print$9}'
    仅打印
    .txt
    文件
  • grep-vE'(..?\1.?\。
    过滤句点前三个字符不唯一的任何名称:
    aaa.txt
    aab.txt
    aba.txt
    baa.txt

我似乎通过使用以下工具实现了我想要的:

ls-l | awk'{print$9}'| grep-vE“(..?\1.?\”

  • awk'{print$9}'
    仅打印
    .txt
    文件
  • grep-vE'(..?\1.?\。
    过滤句点前三个字符不唯一的任何名称:
    aaa.txt
    aab.txt
    aba.txt
    baa.txt

我会使用
查找
列出
*.txt
文件,并使用
grep
筛选出最后两个字母相同的文件(使用反向引用):

它基本上拾取一个字符,然后立即尝试在
.txt
之前反向引用该字符,
-v
提供反向匹配,只留下最后两个字符不相同的文件

更新:要移动找到的文件,可以将
mv
链接到命令:

find . -type f -name '*.txt' | grep -v '\(.\)\1\.txt$' | xargs -i -t mv {} DESTINATION

我将使用
find
列出
*.txt
文件,并使用
grep
筛选出最后两个字母相同的文件(使用反向引用):

它基本上拾取一个字符,然后立即尝试在
.txt
之前反向引用该字符,
-v
提供反向匹配,只留下最后两个字符不相同的文件

更新:要移动找到的文件,可以将
mv
链接到命令:

find . -type f -name '*.txt' | grep -v '\(.\)\1\.txt$' | xargs -i -t mv {} DESTINATION

你到底想做什么?这些文件来自哪里?为什么需要特定的子集?对我来说,这听起来像是你在试图解决一个问题,你可以通过一个更大的系统更好的设计来绕过这个问题!没什么特别的,只是我和格雷普在一起玩。我在一个目录中创建了数千个文件,我想将它们移动到另一个目录。但我只想移动那些最后两个字母不同的文件。示例:
abc.txt aab.txt
。坏例子:
abb.txt err.txt
等等。你到底想做什么?这些文件来自哪里?为什么需要特定的子集?对我来说,这听起来像是你在试图解决一个问题,你可以通过一个更大的系统更好的设计来绕过这个问题!没什么特别的,只是我和格雷普在一起玩。我在一个目录中创建了数千个文件,我想将它们移动到另一个目录。但我只想移动那些最后两个字母不同的文件。示例:
abc.txt aab.txt
。坏例子:
abb.txt err.txt
等等。看起来不错。也许您可以打电话告诉我如何使用此命令将这些文件移动到另一个目录(已创建)中?我们是否需要在
grep
中使用这些额外的反斜杠?不仅仅是
grep-v“\(.\)\1\.txt$”
do?@codeforester-公平点,我的一个老习惯是在键入双引号时避开每个反斜杠,因为一些shell(编程语言)会将反斜杠后面的字符作为“特殊”来计算,不管它是否特殊。本应使用单引号,tho,但我已经输入了
find
部分,我讨厌混合字符串文字,所以我就这么做了;)为了安全起见,单引号可能是最好的选择,所以我更新了我的答案。谢谢你指出。看起来不错。也许您可以打电话告诉我如何使用此命令将这些文件移动到另一个目录(已创建)中?我们是否需要在
grep
中使用这些额外的反斜杠?不仅仅是
grep-v“\(.\)\1\.txt$”
do?@codeforester-公平点,我的一个老习惯是在键入双引号时避开每个反斜杠,因为一些shell(编程语言)会将反斜杠后面的字符作为“特殊”来计算,不管它是否特殊。本应使用单引号,tho,但我已经输入了
find
部分,我讨厌混合字符串文字,所以我就这么做了;)为了安全起见,单引号可能是最好的选择,所以我更新了我的答案。谢谢你指出这一点。