对目录中的文件进行排序,然后打印内容

Sort files in directory and then printing the content

我需要编写一个脚本来根据名称中第一个“0”之后的字符对文件名进行排序。所有文件名至少包含一个0。 然后脚本应该按该顺序打印每个文件的内容。

我知道我需要使用 sort 和 cat。但我不知道是什么类型的。这是我所能得到的。


#!/bin/bash

dir=$(pwd)


for n in $dir `ls | sort -u `  ; do

    cat $n
done;

您可以编写如下所示的脚本:

#/bin/bash

# using "shopt -s nullglob" so that an empty directory won't give you a literal '*'.
shopt -s nullglob

# get a sorted directory listing
filelist=$(for i in .*0* *0*; do echo "$i"; done | sort -t0 -k2)

IFS=$(echo -en "\n\b")
# iterate over your sorted list
for f in $filelist
do
        # just cat text files.
        file $f | grep text > /dev/null 2>&1
        if [ $? = 0 ]
        then
                cat $f
        fi
done

测试:

[plankton@localhost SO_scripts]$ ls -l
total 40
-rw-r--r-- 1 plankton plankton  10 Sep  9 10:56 afile0zzz
-rw-r--r-- 1 plankton plankton  14 Sep  9 10:56 bfile xxx0yyy
-rwxr-xr-x 1 plankton plankton 488 Sep  9 10:56 catfiles.sh
-rw-r--r-- 1 plankton plankton   9 Sep  9 10:56 file0123
-rw-r--r-- 1 plankton plankton   9 Sep  9 10:56 file0124
-rw-r--r-- 1 plankton plankton   7 Sep  9 10:56 file0a
-rw-r--r-- 1 plankton plankton   8 Sep  9 10:56 file0aa
-rw-r--r-- 1 plankton plankton   7 Sep  9 10:56 file0b
-rw-r--r-- 1 plankton plankton   9 Sep  9 10:56 file0bbb
-rw-r--r-- 1 plankton plankton  18 Sep  9 10:56 files*_0asdf
[plankton@localhost SO_scripts]$ ./catfiles.sh
. is not a text file
.. is not a text file
Doing catfiles.sh
#/bin/bash

# using "shopt -s nullglob" so that an empty directory won't give you a literal '*'.
shopt -s nullglob

# get a sorted directory listing

filelist=$(for i in .* *; do echo "$i"; done | sort -t0 -k2)

IFS=$(echo -en "\n\b")
# iterate over your sorted list
for f in $(for i in .* *; do echo "$i"; done | sort -t0 -k2)
do
        # just cat text files.
        file $f | grep text > /dev/null 2>&1
        if [ $? = 0 ]
        then
                echo "Doing $f"
                cat $f
        else
                echo "$f is not a text file"
        fi
done
Doing file0123
file0123
Doing file0124
file0124
Doing file0a
file0a
Doing file0aa
file0aa
Doing files*_0asdf
file with * in it
Doing file0b
file0b
Doing file0bbb
file0bbb
Doing bfile xxx0yyy
bfile xxx0yyy
Doing afile0zzz
afile0zzz

根据 PesaThe .*0* *0* 的建议更新。

dir=$(pwd)

for n in `ls -1 $dir | sort -t0 -k2`; do
   cat $n
done;

假设

  1. 第一个零可以在文件名中的任何位置,
  2. 零后可能有多个同名文件,
  3. 您希望能够处理任何 文件名,包括点文件和包含换行符的名称,以及
  4. 您安装了 GNU CoreUtils(普通发行版的标准配置),

你需要做一些像这样疯狂的事情(未经测试):

find . -mindepth 1 -maxdepth 1 -exec printf '%s[=10=]' {} + | while IFS= read -r -d ''
do
    printf '%s[=10=]' "${REPLY#*0}"
done | sort --unique --zero-terminated | while IFS= read -r -d ''
do
    for file in ./*"$REPLY"
    do
        […]
    done
done

说明:

  1. 打印所有 NUL 分隔的文件名并读回它们以便能够对它们进行变量替换。
  2. 删除文件名中直到并包括第一个零的所有内容并打印出来。
  3. 按文件名的其余部分排序,确保每个唯一的后缀只打印一次。
  4. 处理每个以(现已排序)后缀结尾的文件。

看看这个 find + xargs 可以正确处理 "funny characters" 的文件名:

find . -maxdepth 1 -type f -name '*0*' -print0 | sort -zt0 -k2 | xargs -0 cat