Good day.
美好的一天。
I used Grep to extract readings from a file according to pattern of an ID file.
我使用Grep根据ID文件的模式从文件中提取读数。
grep -w -f idFile.txt readingFile.txt
The ouput of Grep I obtained was:
我获得的Grep的输出是:
F47807 0.00 0.00 8.30
R02218 0.00 0.00 2.07
W74941 0.00 0.00 5.70
C15915 0.00 0.00 3.63
C16638 0.00 0.39 8.82
C16979 0.00 0.39 2.59
Let's say the ID file is:
假设ID文件是:
W74941
F47807
C15915
R02218
C16638
C16979
I would like to have the Grep output same pattern as the ID file like below:
我希望Grep输出的模式与下面的ID文件相同:
W74941 0.00 0.00 5.70
F47807 0.00 0.00 8.30
C15915 0.00 0.00 3.63
R02218 0.00 0.00 2.07
C16638 0.00 0.39 8.82
C16979 0.00 0.39 2.59
I tried with couple of Grep option but I failed to obtain what I want. Thus, could the community kindly please gives me some advice how can I do that?
我尝试了几个Grep选项,但没有得到我想要的。因此,社区能否给我一些建议,我该如何做呢?
Thank you very much for your time.
非常感谢您的时间。
3 个解决方案
#1
1
I don't know if you can do this with grep only, but if you make use of a bit of bash, you can do this:
我不知道你是否可以只使用grep,但是如果你使用一点bash,你可以这样做:
for line in $(cat idFile.txt)
do grep "$line" readingFile.txt
done
This will go over the readingFile once for each id though, so if performance a worry, this won't help you.
这将为每个id检查一次读取文件,所以如果性能令人担心,这对您没有帮助。
Edit: Note that this assumes that the id's contain no spaces.
编辑:注意,这假定id不包含空格。
#2
1
this one-liner should work with your grep output and idfile:
这一行程序应该与您的grep输出和idfile一起工作:
awk 'NR==FNR{a[$1]=$0;next}$1 in a&&$0=a[$1]'
complete line:
完整的线:
awk 'NR==FNR{a[$1]=$0;next}$1 in a&&$0=a[$1]' <(grep -w -f idFile.txt readingFile.txt) idfile
#3
1
With a little bit of awk
you can do something like this -
有一点awk,你可以做类似这样的事情。
awk 'NR==FNR {
a[$1]=$0
next
}
($1 in a) {
print a[$1]
}' <(grep -w -f idFile.txt readingFile.txt) idfile
Explaination:
-
awk
is combination of/pattern/ {action}
statements. Our first patternNR==FNR
ensures that our action is performed only on the first file passed to the awk (grep output in our case). - awk是/pattern/ {action}语句的组合。我们的第一个模式NR==FNR确保我们的操作只在传递给awk的第一个文件上执行(在我们的例子中是grep输出)。
- Our
action
for this is to store it in an array. - 我们的操作是将它存储在一个数组中。
- Once the first file is completed, our pattern will become false and second
pattern-action
statement will come in force. - 一旦第一个文件完成,我们的模式将变为false,第二个模式操作语句将生效。
- Second
pattern-action
statements checks to see if first entry of your idfile is present in the array. If it is, it prints it. As a result you get the output based on the sequence found in idfile. - 第二个模式操作语句检查idfile的第一个条目是否存在于数组中。如果是,就打印出来。因此,您可以根据在idfile中找到的序列获得输出。
#1
1
I don't know if you can do this with grep only, but if you make use of a bit of bash, you can do this:
我不知道你是否可以只使用grep,但是如果你使用一点bash,你可以这样做:
for line in $(cat idFile.txt)
do grep "$line" readingFile.txt
done
This will go over the readingFile once for each id though, so if performance a worry, this won't help you.
这将为每个id检查一次读取文件,所以如果性能令人担心,这对您没有帮助。
Edit: Note that this assumes that the id's contain no spaces.
编辑:注意,这假定id不包含空格。
#2
1
this one-liner should work with your grep output and idfile:
这一行程序应该与您的grep输出和idfile一起工作:
awk 'NR==FNR{a[$1]=$0;next}$1 in a&&$0=a[$1]'
complete line:
完整的线:
awk 'NR==FNR{a[$1]=$0;next}$1 in a&&$0=a[$1]' <(grep -w -f idFile.txt readingFile.txt) idfile
#3
1
With a little bit of awk
you can do something like this -
有一点awk,你可以做类似这样的事情。
awk 'NR==FNR {
a[$1]=$0
next
}
($1 in a) {
print a[$1]
}' <(grep -w -f idFile.txt readingFile.txt) idfile
Explaination:
-
awk
is combination of/pattern/ {action}
statements. Our first patternNR==FNR
ensures that our action is performed only on the first file passed to the awk (grep output in our case). - awk是/pattern/ {action}语句的组合。我们的第一个模式NR==FNR确保我们的操作只在传递给awk的第一个文件上执行(在我们的例子中是grep输出)。
- Our
action
for this is to store it in an array. - 我们的操作是将它存储在一个数组中。
- Once the first file is completed, our pattern will become false and second
pattern-action
statement will come in force. - 一旦第一个文件完成,我们的模式将变为false,第二个模式操作语句将生效。
- Second
pattern-action
statements checks to see if first entry of your idfile is present in the array. If it is, it prints it. As a result you get the output based on the sequence found in idfile. - 第二个模式操作语句检查idfile的第一个条目是否存在于数组中。如果是,就打印出来。因此,您可以根据在idfile中找到的序列获得输出。