linux - 如何在各种单独的文件中分隔与特定模式匹配的文件名和内容

标签 linux bash shell unix ksh

我正在尝试将与特定匹配的文件名分离到一个单独的文件中,并将其内容分离到与特定模式匹配的不同文件中。我有包含特殊字符的文件名,如“|”

我尝试使用 grep 命令。 Grep Ril 和 Grep -H 打印文件名,但它不起作用。

#!bin/bash
cd home/test
let "x = 1"
for file in $(find home/test/* -type f -name "*.txt") ; 
do
var=$(echo "${x}|fill|${file##*/}")
echo "${var}" | grep -n "*|fill|*.txt" >header.txt
myvar=$(sed 's/^/'${x}'|/g' ${file})
echo "${myvar}" |grep -n "*|Ball|*" >Ball.txt
echo "${myvar}" |grep -n "*|Fire|*" >Fire.txt
let x=x+1
done
unset 'x'
let x=x+1
done
unset 'x

我有这种格式的文件名:

1|fill|abc.txt
2|fill|def.txt

“填充”在所有文件中保持不变。这个的最终文件应该有这样的值

1|fill|abc.txt
2|fill|def.txt
3...
4...
5...
etc...

然后,每个文件包含不同的内容。

File1 包含类似于此模式的数据:

1|Ball|202029|
1|Cat|202029|
1|fire|202898
...

文件 2 包含类似于此模式的数据:

2|Bat|202029|
2|Ball|202029|
2|cat|202898

现在最终的输出应该是这样的,所有包含“ball”的数据都应该在一个单独的文件中,“cat”在单独的文件中,“fire”在单独的文件中等等。

最佳答案

我不确定下面的代码会做你想做的事,但我相信它会接近它,让我知道,我会相应地更新。

下面的文件将与您在脚本中使用的其他文件位于同一目录中,并且当它们以 .txt 结尾时,下一次运行的脚本也会读取它们。

header.txt
B.txt
C.txt
F.txt
#!/bin/bash


# i put the directory in variable, so it can be changed at a single place.
dir='/home/test'

#if cd failed , print erron on standard error output and terminate script.
if ! cd "${dir}" ;then
        echo "cd failed into ${dir}" >&2
        exit 1
fi

# set counter to 1
let "x = 1"

# Null file contents or create new file
# without this file content will be preserved from earlier script runs.
> header.txt
> B.txt
> C.txt
> F.txt

# go trhought every file in ${dir} path that name end with .txt and it is a regular file
for file in $(find ${dir} -type f -name "*.txt") ;
do
        # store basefilename in variable with aditional counter number and text |Fill| front of it.
        filename=$(echo "${x}|fill|${file##*/}")
        echo "${filename}" >> header.txt
        # this can be used as well:
        ##echo "${x}|fill|${file##*/}" >> header.txt
        # only difference is you stored the output into variable.

        # find matching line in files
        grep -i '|Ball|' ${file} | sed 's/^/'${x}'|/g' >> B.txt
        grep -i '|Cat|'  ${file} | sed 's/^/'${x}'|/g' >> C.txt
        grep -i '|Fire|' ${file} | sed 's/^/'${x}'|/g' >> F.txt

        # add 1 to counter
        let "x=x+1"
done

# unset counter
unset 'x'

输入文件:

文件1.txt

1|Ball|202029|
1|Cat|202029|
1|fire|202898

文件2.txt

2|Bat|202029|
2|Ball|202029|
2|cat|202898

输出文件:

标题.txt

1|fill|header.txt
2|fill|B.txt
3|fill|C.txt
4|fill|F.txt
5|fill|File1.txt
6|fill|File2.txt

B.txt

5|1|Ball|202029|
6|2|Ball|202029|

C.txt

5|1|Cat|202029|
6|2|cat|202898

F.txt

5|1|fire|202898

关于linux - 如何在各种单独的文件中分隔与特定模式匹配的文件名和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606127/

相关文章:

linux - 删除未使用的文件夹和需要的副本

linux - 在cygwin中执行kinit时遇到 "fatal error - NtCreateEvent(lock): 0xC0000077"

linux - NFS 实现,ubuntu 发送空的 nfs_fh3?

bash - 在 udev 规则中传递参数

php - 来自 php 网站的 exec bash 脚本,脚本未运行

linux - 如何在一段时间内保持运行一个程序?

linux - Youtube 的 Zend_Gdata 库未导入 Linux 环境中的 cakephp 1.2 应用程序

java - classpath echo 在 linux 中返回空白

linux - linux 下的 shebang 不拆分参数

linux - 如何检测 bash shell 命令后跟逻辑 && AND 或 ||或者?