grep

Git grep print only matching pattern

南笙酒味 提交于 2020-08-08 06:50:29
问题 In the manual for grep , we have -o to print --only-matching patterns. Say echo foobar | grep foo would return foobar , but adding -o to grep would give only foo . Many grep options like -P , -c , etc, can be used in conjunction with git to search through all files in the Git index. However, git grep -o PAT triggers an error: unknown switch o'`. How can I print only matched string for each file in the Git index? i.e. " git grep -o PAT " My trial: for f in `git ls-files`; do grep -o PAT $f;

What's the difference between grep -r and -R

孤街醉人 提交于 2020-08-04 05:38:09
问题 In the man page: -r Read all files under each directory, recursively, following symbolic links only if they are on the command line. what exactly does "being on the command line" means? Thanks. 回答1: "Being on the command line" refers to the arguments passed to grep. If you give a symbolic link as an argument to grep -r it follows it. However, if grep -r encounters a symbolic link while traversing a directory it does not follow it (in contrast to grep -R which does). Imagine you have a

filter ping result with grep and awk altogether doesn't work

落爺英雄遲暮 提交于 2020-07-22 05:40:49
问题 I want to pipe the ping output with only its delay time to a text. while I do , I get this as expected ping somesite PING somesite (220.181.57.217) 56(84) bytes of data. 64 bytes from 220.181.57.217: icmp_seq=1 ttl=52 time=43.4 ms 64 bytes from 220.181.57.217: icmp_seq=2 ttl=52 time=43.7 ms 64 bytes from 220.181.57.217: icmp_seq=3 ttl=52 time=43.4 ms Then I do this ping somesite | awk -F '[:=]' '{print $5}' 43.3 ms 43.2 ms 43.3 ms 43.2 ms 43.2 ms 43.1 ms 43.1 ms 43.3 ms 43.2 ms 43.6 ms 43.3

Find the value of key from JSON

陌路散爱 提交于 2020-07-17 05:27:32
问题 I'd like to extract the "id" key from this single line of JSON. I believe this can be accomplished with grep, but I am not sure on the correct way. If there is a better way that does not have dependencies, I would be interested. Here is my example output: {"data": {"name": "test", "id": "4dCYd4W9i6gHQHvd", "domains": ["www.test.domain.com", "test.domain.com"], "serverid": "bbBdbbHF8PajW221", "ssl": null, "runtime": "php5.6", "sysuserid": "4gm4K3lUerbSPfxz", "datecreated": 1474597357},

Greedy behaviour of grep

回眸只為那壹抹淺笑 提交于 2020-06-27 10:27:11
问题 I thought that in regular expressions, the "greediness" applies to quantifiers rather than matches as a whole. However, I observe that grep -E --color=auto 'a+(ab)?' <(printf "aab") returns aab rather than aa b. The same applies to sed. On the other hand, in pcregrep and other tools, it is really the quantifier that is greedy. Is this a specific behaviour of grep? N.B. I checked both grep (BSD grep) 2.5.1-FreeBSD and grep (GNU grep) 3.1 回答1: In the description of term matched, POSIX states

Bash grep ip from line

怎甘沉沦 提交于 2020-06-17 09:09:24
问题 I have the file ip.txt which contain the following ata001dcfe16f85.mm.ph.ph.cox.net (24.252.231.220) 220.231.252.24.xxx.com (24.252.231.220) and I made this bash command to extract ips : grep -Eo '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' ip.txt | sort -u > good.txt I want to edit the code so it extracts the ips between the parentheses ONLY . not all the ips on the line

How can I use grep to match but without printing the matches?

被刻印的时光 ゝ 提交于 2020-06-13 15:37:13
问题 I'm new to linux shell and am trying to do this, preferably in one line, with the following condition: It can't output anything to the terminal. /var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt' The problem is the .exe spits out XML data to terminal. I can't figure out how to grep, use the exit status, but not have the screen cluttered with the output of each match. If I use /dev/null 2>&1 , it pipes it quite but then I can't grep the data. Any idea

How can I use grep to match but without printing the matches?

纵饮孤独 提交于 2020-06-13 15:37:05
问题 I'm new to linux shell and am trying to do this, preferably in one line, with the following condition: It can't output anything to the terminal. /var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt' The problem is the .exe spits out XML data to terminal. I can't figure out how to grep, use the exit status, but not have the screen cluttered with the output of each match. If I use /dev/null 2>&1 , it pipes it quite but then I can't grep the data. Any idea

command to count occurrences of word in entire file

无人久伴 提交于 2020-05-29 08:15:46
问题 I am trying to count the occurrences of a word in a file. If word occurs multiple times in a line, I will count is a 1. Following command will give me the output but will fail if line has multiple occurrences of word grep -c "word" filename.txt Is there any one liner? 回答1: You can use grep -o to show the exact matches and then count them: grep -o "word" filename.txt | wc -l Test $ cat a hello hello how are you hello i am fine but this is another hello $ grep -c "hello" a # Normal `grep -c`

command to count occurrences of word in entire file

孤者浪人 提交于 2020-05-29 08:12:07
问题 I am trying to count the occurrences of a word in a file. If word occurs multiple times in a line, I will count is a 1. Following command will give me the output but will fail if line has multiple occurrences of word grep -c "word" filename.txt Is there any one liner? 回答1: You can use grep -o to show the exact matches and then count them: grep -o "word" filename.txt | wc -l Test $ cat a hello hello how are you hello i am fine but this is another hello $ grep -c "hello" a # Normal `grep -c`