在具有类似文件的目录中的尾部操作

时间:2017-05-31 13:58:50

标签: linux bash ubuntu file-io tail

我正在寻找一种处理这种情况的聪明方法。

cpu-0fhcount目录下,有许多文件,但它们可以按某些名称分组。

以下是鸟瞰目录和文件的视图。

../cpu-0/
        cpu-idle-01-01-2016
        cpu-idle-02-01-2016
        cpu-idle-03-01-2016
        .
        .
        cpu-interrupt-01-01-2016
        cpu-interrupt-02-01-2016
        cpu-interrupt-03-01-2016
        .
        .
        .
        cpu-nice-01-01-2016
        cpu-nice-02-01-2016
        .
        .
../fhcount/
        file_handles-max-01-01-2016
        file_handles-max-02-01-2016
        file_handles-max-03-01-2016
        .
        .
        file_handles-unused-01-01-2016
        file_handles-unused-02-01-2016
        file_handles-unused-03-01-2016
        .
        .
        .
        file_handles-used-01-01-2016
        file_handles-used-02-01-2016
        .
        .

如您所见,有一种模式。我通过硬编码收集它们以便tail相关文件。

curdir="${PWD%}"

tail -q -n +2 $curdir/cpu-0/cpu-idle* > cpu-idle_combined
tail -q -n +2 $curdir/cpu-0/cpu-interrupt* > cpu-interrupt_combined
tail -q -n +2 $curdir/cpu-0/cpu-nice* > cpu-nice_combined

tail -q -n +2 $curdir/fhcount/file_handles-max* > file_handles-max_combined
tail -q -n +2 $curdir/fhcount/file_handles-unused-* > file_handles-unused_combined
tail -q -n +2 $curdir/fhcount/file_handles-used-* > file_handles-unused_combined

我怎么能做同样的事情,但这一次更聪明?

1 个答案:

答案 0 :(得分:1)

这会遍历子目录中的所有文件,收集文件名的公共部分,然后将它们打印到组合输出文件中:

#!/bin/bash

# Required for the +(pattern) glob
shopt -s extglob

# Associative array used as set of unique file name roots
declare -A roots

# Shorten names like cpu-0/cpu-idle-01-01-2016 to cpu-0/cpu-idle
# +([[:digit:]-]) matches digits and hyphens
# ${fname%%pattern) removes the longest match of pattern from the end of fname
for fname in */*; do
    roots["${fname%%+([[:digit:]-])}"]=1
done

# Loop through unique roots, print to output files
for fname in "${!roots[@]}"; do
    tail -q -n +2 "$fname"* > "$fname"_combined
done

关联数组需要Bash 4.0或更高版本。

对于

的示例输入文件结构
.
├── cpu-0
│   ├── cpu-idle-01-01-2016
│   ├── cpu-idle-02-01-2016
│   ├── cpu-idle-03-01-2016
│   ├── cpu-interrupt-01-01-2016
│   ├── cpu-interrupt-02-01-2016
│   ├── cpu-interrupt-03-01-2016
│   ├── cpu-nice-01-01-2016
│   ├── cpu-nice-02-01-2016
│   └── cpu-nice-03-01-2016
└── fhcount
    ├── file_handles-max-01-01-2016
    ├── file_handles-max-02-01-2016
    ├── file_handles-max-03-01-2016
    ├── file_handles-unused-01-01-2016
    ├── file_handles-unused-02-01-2016
    ├── file_handles-unused-03-01-2016
    ├── file_handles-used-01-01-2016
    ├── file_handles-used-02-01-2016
    └── file_handles-used-03-01-2016

结果输出结构是

.
├── cpu-0
│   ├── cpu-idle-01-01-2016
│   ├── cpu-idle-02-01-2016
│   ├── cpu-idle-03-01-2016
│   ├── cpu-idle_combined
│   ├── cpu-interrupt-01-01-2016
│   ├── cpu-interrupt-02-01-2016
│   ├── cpu-interrupt-03-01-2016
│   ├── cpu-interrupt_combined
│   ├── cpu-nice-01-01-2016
│   ├── cpu-nice-02-01-2016
│   ├── cpu-nice-03-01-2016
│   └── cpu-nice_combined
└── fhcount
    ├── file_handles-max-01-01-2016
    ├── file_handles-max-02-01-2016
    ├── file_handles-max-03-01-2016
    ├── file_handles-max_combined
    ├── file_handles-unused-01-01-2016
    ├── file_handles-unused-02-01-2016
    ├── file_handles-unused-03-01-2016
    ├── file_handles-unused_combined
    ├── file_handles-used-01-01-2016
    ├── file_handles-used-02-01-2016
    ├── file_handles-used-03-01-2016
    └── file_handles-used_combined

,例如输入文件内容,如

$ head cpu-idle*
==> cpu-idle-01-01-2016 <==
1cpu-idle-01-01-2016
2cpu-idle-01-01-2016
3cpu-idle-01-01-2016

==> cpu-idle-02-01-2016 <==
1cpu-idle-02-01-2016
2cpu-idle-02-01-2016
3cpu-idle-02-01-2016

==> cpu-idle-03-01-2016 <==
1cpu-idle-03-01-2016
2cpu-idle-03-01-2016
3cpu-idle-03-01-2016

组合输出文件包含

$ cat cpu-idle_combined
2cpu-idle-01-01-2016
3cpu-idle-01-01-2016
2cpu-idle-02-01-2016
3cpu-idle-02-01-2016
2cpu-idle-03-01-2016
3cpu-idle-03-01-2016