如何在各种单独的文件中分离与特定模式匹配的文件名和内容 [英] How to separate the filenames and contents matching a particular pattern in various separate files

查看:67
本文介绍了如何在各种单独的文件中分离与特定模式匹配的文件名和内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试使用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"应位于单独的文件中,依此类推.

解决方案

我不确定下面的代码会完成您想要的事情,但是我相信,它将接近于此,请告诉我,我会相应地进行更新. /p>

下面的文件将与您在脚本中使用的其他文件位于同一目录中,并且当它们以.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'

输入文件:

File1.txt

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

File2.txt

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

输出文件:

header.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

I am trying to segregate filenames matching a particular into a separate file and its contents into different files matching particular patterns.I have the filenames with special characters included like '|'

I tried using grep command. Grep Ril and Grep -H to print the filenames,but it is not working.

#!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

I have the filenames in this format:

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

The 'fill' remains the same in all files. The final file for this should have values like this

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

Then, each file contains different contents.

File1 contains data similar to this pattern:

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

File 2 contains data similar to this pattern:

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

Now the final output should be in such a way that all the data containing 'ball' should be in a separate file, 'cat' in separate file, 'fire' in separate file and so on.

解决方案

I not sure the below code will do the thing you want, but it will be close to it I beleve, let me know and I update is accordingly.

the files below will be in the same directory as the other files you use in the script and as they end .txt as well next script run will read them as well.

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'

Input files:

File1.txt

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

File2.txt

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

Output files:

header.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

这篇关于如何在各种单独的文件中分离与特定模式匹配的文件名和内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆