grep

Do not merge the context of contiguous matches with grep

孤街醉人 提交于 2020-12-08 05:46:04
问题 If I run grep -C 1 match over the following file: a b match1 c d e match2 f match3 g I get the following output: b match1 c -- e match2 f match3 g As you can see, since the context around the contiguous matches "match2" and "match3" overlap, they are merged. However, I would prefer to get one context description for each match, possibly duplicating lines from the input in the context reporting. In this case, what I would like is: b match1 c -- e match2 f -- f match3 g What would be the best

How to extract email headers extending on multiple lines from file

你。 提交于 2020-12-06 20:23:45
问题 I am trying to extract the To header from an email file using sed on linux. The problem is that the To header could be on multiple lines. e.g: To: name1@mydomain.org, name2@mydomain.org, name3@mydomain.org, name4@mydomain.org, name5@mydomain.org Message-ID: <46608700.369886.1549009227948@domain.org> I tried the following: sed -n -e '/^[Tt]o: / { N; p; }' _message_file_ | awk '{$1=$1;printf("%s ",$0)};NR%2==0{print ""}' The sed command extracts the line starting with To and next line. I pipe

How to extract email headers extending on multiple lines from file

江枫思渺然 提交于 2020-12-06 20:23:03
问题 I am trying to extract the To header from an email file using sed on linux. The problem is that the To header could be on multiple lines. e.g: To: name1@mydomain.org, name2@mydomain.org, name3@mydomain.org, name4@mydomain.org, name5@mydomain.org Message-ID: <46608700.369886.1549009227948@domain.org> I tried the following: sed -n -e '/^[Tt]o: / { N; p; }' _message_file_ | awk '{$1=$1;printf("%s ",$0)};NR%2==0{print ""}' The sed command extracts the line starting with To and next line. I pipe

How to add output “non_assigned” when there is no match in grep?

狂风中的少年 提交于 2020-11-29 11:06:49
问题 When I run a command (COMMAND) on one line of my input file (input.txt) I get an associated result where only one line is interesting, always starting by the world phylum. For instance: superkingdom 2759 Eukaryota clade 554915 Amoebozoa phylum 555280 Discosea order 313555 Himatismenida family 313556 Cochliopodiidae So I run : for p in $(cat input.txt) do COMMAND $p | grep "\bphylum\b" >> results.txt done In order to have in my file result.txt all the lines like : phylum 555280 Discosea

How to add output “non_assigned” when there is no match in grep?

折月煮酒 提交于 2020-11-29 11:03:40
问题 When I run a command (COMMAND) on one line of my input file (input.txt) I get an associated result where only one line is interesting, always starting by the world phylum. For instance: superkingdom 2759 Eukaryota clade 554915 Amoebozoa phylum 555280 Discosea order 313555 Himatismenida family 313556 Cochliopodiidae So I run : for p in $(cat input.txt) do COMMAND $p | grep "\bphylum\b" >> results.txt done In order to have in my file result.txt all the lines like : phylum 555280 Discosea

How to add output “non_assigned” when there is no match in grep?

匆匆过客 提交于 2020-11-29 11:03:06
问题 When I run a command (COMMAND) on one line of my input file (input.txt) I get an associated result where only one line is interesting, always starting by the world phylum. For instance: superkingdom 2759 Eukaryota clade 554915 Amoebozoa phylum 555280 Discosea order 313555 Himatismenida family 313556 Cochliopodiidae So I run : for p in $(cat input.txt) do COMMAND $p | grep "\bphylum\b" >> results.txt done In order to have in my file result.txt all the lines like : phylum 555280 Discosea

How to grep lines between two patterns in a big file with python

半城伤御伤魂 提交于 2020-11-27 04:04:57
问题 I have a very big file, like this: [PATTERN1] line1 line2 line3 ... ... [END PATTERN] [PATTERN2] line1 line2 ... ... [END PATTERN] I need to extract in another file, lines between a variable starter pattern [PATTERN1] and another define pattern [END PATTERN], only for some specific starter pattern. For example: [PATTERN2] line1 line2 ... ... [END PATTERN] I already do the same thing, with a smaller file, using this code: FILE=open('myfile').readlines() newfile=[] for n in name_list: A = FILE[

How to grep lines between two patterns in a big file with python

こ雲淡風輕ζ 提交于 2020-11-27 04:04:13
问题 I have a very big file, like this: [PATTERN1] line1 line2 line3 ... ... [END PATTERN] [PATTERN2] line1 line2 ... ... [END PATTERN] I need to extract in another file, lines between a variable starter pattern [PATTERN1] and another define pattern [END PATTERN], only for some specific starter pattern. For example: [PATTERN2] line1 line2 ... ... [END PATTERN] I already do the same thing, with a smaller file, using this code: FILE=open('myfile').readlines() newfile=[] for n in name_list: A = FILE[

Grep 'binary file matches'. How to get normal grep output? [duplicate]

这一生的挚爱 提交于 2020-11-26 04:55:15
问题 This question already has answers here : How to grep a text file which contains some binary data? (11 answers) Closed 3 years ago . I've got a grep script that searches through a directory recursively. grep -n -R -e 'search term' -e 'second search term' ./ However the results I get are the following. Notice there are found matches in JPGs but no actual result. Binary file ./jpg/00015928.jpg matches Binary file ./jpg/00015296.jpg matches Binary file ./jpg/00020072.jpg matches Is there any way

how perform grep operation on all files in a directory

可紊 提交于 2020-11-25 04:48:44
问题 Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file. I'm clear on the command I want to use and how to grep out string(s) as needed. But what I'm not clear on is how do I have it perform this command on each file, going to the next, until no more files are found. 回答1: grep $PATTERN * would be sufficient. By default, grep would skip all subdirectories. However, if you want