将文本文件中的数字与其他文本文件中的数字列表进行比较 [英] Comparing numbers in a text file to a list of numbers in an other textfile

查看:126
本文介绍了将文本文件中的数字与其他文本文件中的数字列表进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4位数字的.txt档案。



有时只包含一个4位数字,有时是多个4位数字,空。



example1.txt文件:

  6304 
6204

example2.txt档案:

  6308 

example3.txt档案:

  6305 

example4.txt档案:

  6300 
6204
6301
pre>

example5.txt档案:

  6302 
6234
6345

我需要做的是检查示例文件在我在其他文本文件中的数字列表。



此列表看起来像这样:( 但有更多数字

  6300 
6301
6302
6303
6304
6305

*用于'example1.txt'文件:



'应该从文件中删除*(因为它不在列表中)*
数字'6304'必须保留在示例文件中(它在列表中) *:










$ b b

该数字保留在示例文件中。



*用于'example4.txt'文件

比例文件中的1个匹配。所以一切都应该删除。



*用于'example5.txt'文件:

文件中只应包含6302。其他两个应该删除,因为他们不在列表中。






所以基本上我想保留有1单匹配。并且那些文件应该只包含与列表中的数字匹配的数字。如果有多个匹配项,则该文件应为空。如果没有匹配的文件应该也是空的



在所有这一切,我想在一个sh脚本。



现在我的问题是:



这是可能吗?或者我需要使用数据库和其他编程语言?



提前感谢。

解决方案

逻辑。我假设你的列表存储在文件 list.txt ,并将以下保存为 marksscript

 #!/ bin / bash 

#首次计数匹配的总数并存储在变量MATCHES

MATCHES = 0
同时读取WORD
do
#计算此词的匹配数
N = $(grep -c $ WORD list.txt)
[$ N -eq 1]&& MATCHEDWORD = $ WORD
echo DEBUG:$ WORD $ N
((MATCHES + = N))
done< $ 1


#现在我们知道匹配的总数,决定做什么

echo DEBUG:总匹配$ MATCHES

if [$ MATCHES -ne 1];然后
echo DEBUG:清零文件 - 不完全一致
> $ 1
else
echo DEBUG:$ MATCHEDWORD保持为单例匹配
echo $ MATCHEDWORD> $ 1
fi

运行方式如下:

  chmod + x marksscript 
./marksscript example1.txt

OUTPUT

  ./ go example1 
DEBUG: 6204 0
调试:6304 1
调试:总计匹配1
DEBUG:6304保持为单一匹配

./go example2
DEBUG:总匹配0
DEBUG:清零文件 - 不完全匹配

./go example3
DEBUG:6305 1
DEBUG:总计匹配1
DEBUG: 6305保持为单独匹配

./go example4
DEBUG:6300 1
DEBUG:6204 0
DEBUG:6301 1
DEBUG:总计匹配2
DEBUG:清零文件 - 不完全一个匹配


I have a .txt file containing 4-digit numbers.

sometimes they only contain one 4-digit number, sometimes multiple 4-digit numbers, sometimes they are empty.

example1.txt file:

6304
6204

example2.txt file:

6308

example3.txt file:

6305

example4.txt file:

6300
6204
6301

example5.txt file:

6302
6234
6345

What I need to do, is to check if the numbers inside the example file are in a list of numbers I have in an other textfile.

this list looks something like this: (but with more numbers)

6300 
6301 
6302 
6303 
6304 
6305

*for the 'example1.txt' file:

the number '6204' should be deleted out of the file*(because it's not in the list.)* the number '6304' must stay in the example file (it is in the list)

*for the 'example2.txt' file:

the number should be deleted and the file should be empty.

*for the 'example3.txt' file:

the number stays in the example file.

*for the 'example4.txt' file:

There is more than 1 match in the example file. so everything should be deleted.

*for the 'example5.txt' file:

Only 6302 should be in the file. the other two should be deleted because they are not in the list.


So basicly I want to keep the files that have 1 single match. and those files should only contain the number that matches a number in the list. If there is more than 1 match, the file should be empty. if there are no matches the file should also be empty

On top of all this, I would like to do it in a sh script.

Now my question is:

Is this even possible and how? or do I need to work with a database and other programming language ?

Thanks in advance.

解决方案

I think I have understood your logic now. I assume your list is stored in file list.txt and that you save the following as marksscript:

#!/bin/bash
#
# First count total number of matches and store in variable MATCHES
#
MATCHES=0
while read WORD
do
   # Count number of matches for this word
   N=$(grep -c $WORD list.txt)
   [ $N -eq 1 ] && MATCHEDWORD=$WORD
   echo DEBUG: $WORD $N
   ((MATCHES+=N))
done < "$1"

#
# Now we know total number of matches, decide what to do
#
echo DEBUG: Total matches $MATCHES

if [ $MATCHES -ne 1 ]; then
    echo DEBUG: Zero out file - not exactly ONE match
    > "$1"
else
    echo DEBUG: $MATCHEDWORD remains as singleton match
    echo $MATCHEDWORD > "$1"
fi

Run like this:

chmod +x marksscript
./marksscript example1.txt

OUTPUT

./go example1
DEBUG: 6204 0
DEBUG: 6304 1
DEBUG: Total matches 1
DEBUG: 6304 remains as singleton match

./go example2
DEBUG: Total matches 0
DEBUG: Zero out file - not exactly ONE match

./go example3
DEBUG: 6305 1
DEBUG: Total matches 1
DEBUG: 6305 remains as singleton match

./go example4
DEBUG: 6300 1
DEBUG: 6204 0
DEBUG: 6301 1
DEBUG: Total matches 2
DEBUG: Zero out file - not exactly ONE one match

这篇关于将文本文件中的数字与其他文本文件中的数字列表进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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