How to separate the filenames and contents matching a particular pattern in various separate files

旧时模样 提交于 2020-01-15 05:24:52

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/55606127/how-to-separate-the-filenames-and-contents-matching-a-particular-pattern-in-vari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!