grep

grep lines that start with a specific string

こ雲淡風輕ζ 提交于 2021-02-05 00:56:30
问题 I want to find all the lines in a file that start with a specific string. The problem is, I don't know what's in the string beforehand. The value is stored in a variable. The naïve solution would be the following: grep "^${my_string}" file.txt; Because if the Bash variable my_string contains ANY regular expression special characters, grep will cry, and everyone will have a bad day. You don't want to make grep cry, do you? 回答1: You should use awk instead of grep for non-regex search using

Grep only last line after find needed files

╄→гoц情女王★ 提交于 2021-02-04 21:38:51
问题 Hi guys I have an extended question from this thread I need to find some files given file name and use grep on the last lines of these files to find a certain string. I currently have: find my_dir/ -name "*filename*" | xargs grep 'lookingfor' I'm new to using these commands so much help would be appreciated. Thank you in advance. 回答1: You can for example do: find my_dir/ -name "*filename*" -exec sh -c "tail -200 {} | grep lookingfor" \; setting 200 to the number of last lines you want. 回答2: I

Grep only last line after find needed files

China☆狼群 提交于 2021-02-04 21:38:18
问题 Hi guys I have an extended question from this thread I need to find some files given file name and use grep on the last lines of these files to find a certain string. I currently have: find my_dir/ -name "*filename*" | xargs grep 'lookingfor' I'm new to using these commands so much help would be appreciated. Thank you in advance. 回答1: You can for example do: find my_dir/ -name "*filename*" -exec sh -c "tail -200 {} | grep lookingfor" \; setting 200 to the number of last lines you want. 回答2: I

Remove all text from last dot in bash

北战南征 提交于 2021-02-04 15:31:13
问题 I have a file named test.txt which has: abc.cde.ccd.eed.12345.5678.txt abcd.cdde.ccdd.eaed.12346.5688.txt aabc.cade.cacd.eaed.13345.5078.txt abzc.cdae.ccda.eaed.29345.1678.txt abac.cdae.cacd.eead.18145.2678.txt aabc.cdve.cncd.ened.19945.2345.txt If I want to remove everything beyond the first . like: cde.ccd.eed.12345.5678.txt cdde.ccdd.eaed.12346.5688.txt cade.cacd.eaed.13345.5078.txt cdae.ccda.eaed.29345.1678.txt cdae.cacd.eead.18145.2678.txt cdve.cncd.ened.19945.2345.txt Then I will do for

Remove all text from last dot in bash

本小妞迷上赌 提交于 2021-02-04 15:30:41
问题 I have a file named test.txt which has: abc.cde.ccd.eed.12345.5678.txt abcd.cdde.ccdd.eaed.12346.5688.txt aabc.cade.cacd.eaed.13345.5078.txt abzc.cdae.ccda.eaed.29345.1678.txt abac.cdae.cacd.eead.18145.2678.txt aabc.cdve.cncd.ened.19945.2345.txt If I want to remove everything beyond the first . like: cde.ccd.eed.12345.5678.txt cdde.ccdd.eaed.12346.5688.txt cade.cacd.eaed.13345.5078.txt cdae.ccda.eaed.29345.1678.txt cdae.cacd.eead.18145.2678.txt cdve.cncd.ened.19945.2345.txt Then I will do for

Parsing text using grep

别来无恙 提交于 2021-01-29 04:55:27
问题 I have a textfile called netlist.txt with the following contents: M1 nmos1 M2 nmos2 P1 pmos1 M3 nmos3 M4 nmos4 P2 pmos2 I want to retrieve only the line which starts with a tab/space and matching all the "M" values that are indented, using regex. In order to accomplish this I entered the following expression in bash: egrep [:space:]*[M][0-9]+ netlist.txt But it doesn't recognize the space. It retrieves all the lines regardless of having a space or not. Please give me some advice on this.

Send grepped tail output to netcat

我怕爱的太早我们不能终老 提交于 2021-01-28 14:27:08
问题 I am trying to run the following command, and nothing is getting sent to netcat tail -F file.txt | grep test | nc host 9999 If I remove the grep, the tail successfully is followed and sent to netcat. If I just run the following, data comes back, so I know that data should be getting sent to the nc pipe: tail -F file.txt | grep test Any ideas? UPDATE I added the following to unbuffer the piped output and nothing goes through: tail -F file.txt | stdbuf -o0 grep test | nc host 9999 When I turn

Delete files except those whose name matches a string

删除回忆录丶 提交于 2021-01-28 07:24:44
问题 I'm trying to delete all files (include subdir) in a directory but only files which not match specific file name: "equipe" "match" "express" I'm trying to do it with this command find . -type f '!' -exec grep -q "equipe" {} \; -exec echo rm {} \; That doesn't work. It echos files with "equipe" inside How can I do it with multiple strings? ("equipe" "match" "express") 回答1: You may use this find : find . -type f -not \( -name '*equipe*' -o -name '*match*' -o -name '*express*' \) -delete 来源:

Match regex across multiple lines in bash

巧了我就是萌 提交于 2021-01-28 07:02:10
问题 I want to match all patterns that start with [% and end with %] in a file. I've tried multiple tools such as awk, sed, pcregrep and none of them seem to work, although they are suggested as top answers on similar questions. [% FOREACH selection = selections -%] case SELECTION_ID_[% SELECTION_NAME %]: { const [% selectionType %]& source = this->[% selectionName %](); rc = bcem_AggregateUtil::toAggregate(result, d_selectionId, source); } break; [% END -%] [% foo ] [% INCLUDE

How do I grep recursively in files with a certain extension? [duplicate]

☆樱花仙子☆ 提交于 2021-01-27 14:06:23
问题 This question already has answers here : grep recursively for a specific file type on Linux (4 answers) Closed 2 years ago . To find all file paths with .out extension in subdirectories, I use find . -name '*.out' To grep a pattern in all files ending in .out , I use grep pattern *.out How do I combine these two commands, so that it finds all files and then greps in those files? I am looking for an elegant alternative to grep -r 'pattern' . | grep '.out' 回答1: find allows you to run a program