New answers tagged

0 votes

How to execute a command on all files whose names match a pattern and whose contents match a pattern?

Fun case of chained xargs: find /path/to/dir -name '*.[hc]pp' -print0 | xargs -0 grep -lZ 'FOO' | xargs -0 ... Depending on your command you may need to pass -n 1 to the second invocation of xargs to ...
Joshua's user avatar
  • 1,893
3 votes

What does "/$" mean to "grep"?

It's a regular expression to denote the end of a line. This command is being used to exclude directories from the ls output since they will end with a /. I'm not sure of a condition in which ls -lap ...
jesse_b's user avatar
  • 37.1k
1 vote

How to execute a command on all files whose names match a pattern and whose contents match a pattern?

find /path/to/dir -name '*.[hc]pp' -exec grep -l 'FOO' {} + \ | while IFS= read -r f; do cmd "$f"; done
cjs's user avatar
  • 680
4 votes

How to execute a command on all files whose names match a pattern and whose contents match a pattern?

First turn on the globstar option in Bash: shopt -s globstar Now you can simply do: for x in /path/to/dir/**/*.[hc]pp; do if grep -q 'FOO' "$x"; then ... fi fi The ** pattern ...
Kaz's user avatar
  • 8,476
11 votes

How to execute a command on all files whose names match a pattern and whose contents match a pattern?

With GNU implementations of those utilities (and a shell with support for ksh-style¹ process substitution), you could just do: xargs -r0a <(grep -rlZ --include='*.[hc]pp' FOO) cmd -- Above, with -...
Stéphane Chazelas's user avatar
21 votes
Accepted

How to execute a command on all files whose names match a pattern and whose contents match a pattern?

-exec … \; is also a test, it succeeds iff the command inside returns exit status 0. -exec … + can process many pathnames with one command and it always succeeds, so it's not a useful test. grep -q ...
Kamil Maciorowski's user avatar
2 votes

Find out if user name and database are matching | grep

Unfortunately your premise for grep -w is provably wrong: echo 'desired_string-22' | grep -w 'desired_string' desired_string-22 The -w flag is described in the documentation (see man grep) as, ...
Chris Davies's user avatar
0 votes

Find out if user name and database are matching | grep

grep with -w will not work in your case, as - is also considered a word separator. sudo --login -u postgres psql --command="\l" | awk '$1=="exactdbmatch" && $3=="...
Amartya Sinha's user avatar
1 vote

Interactive commands with pipe and grep

doing exactly that, "greping from interactive command", could be hard if not impossible, and if you found a solution, i think it would be rather convoluted. The way doing this is by learning ...
white-hat-er's user avatar
0 votes

How can I view only non-storage devices using lsusb, lsblk, or similar?

The idea: querying each device listed by lsusb and printing only lines beloging to devices that are not registered as "Mass storage" devices; Quite a bit of research has taught me (hopefully ...
kos's user avatar
  • 2,977
0 votes

How can I view only non-storage devices using lsusb, lsblk, or similar?

sudo lshw -businfo | grep -i usb | grep -v storage I'd personally filter out Host Controllers and Hubs because it's unlikely you wanna see them, so it'll be: sudo lshw -businfo | grep -i usb | grep -...
Artem S. Tashkinov's user avatar
0 votes

How can I view only non-storage devices using lsusb, lsblk, or similar?

You could grep -v "Mass Storage" with lsusb but I wouldn't be surprised if there are some edge cases where that string might not appear.
jesse_b's user avatar
  • 37.1k
2 votes
Accepted

How to catch all lines of a repeating pattern and do some actions with the subresults

Using GNU awk for multi-char RS, RT, and use of NUL (\0) to split the file into NUL-separated multi-line records: while IFS= read -r -d '' rec; do printf '=====\n%s\n=====\n' "$rec" done ...
Ed Morton's user avatar
  • 31.7k
0 votes

How to catch all lines of a repeating pattern and do some actions with the subresults

The question is kind of open ended without a specific output required for a specific input. There is a language for extracting data using multi-line patterns across text documents: TXR. Let's assume ...
Kaz's user avatar
  • 8,476
1 vote

How to catch all lines of a repeating pattern and do some actions with the subresults

I find the question to be a bit broad, but as a very generic answer, in Perl you could match the action(s) based on a pattern and then do something with them. perl -wne ' chomp; if (/^(Total:)$/) ...
kos's user avatar
  • 2,977
3 votes

How to catch all lines of a repeating pattern and do some actions with the subresults

There's no Right Tool©, but many appropriate ones, certainly including awk (but not the shell). The classic approach is to have a variable that changes value when you find your string. For example, ...
terdon's user avatar
  • 242k
0 votes

Listing a certain part of filenames and their contents

As the system is using busybox, I assume it won't have things like perl. busybox has a very capable awk implementation though, so you should be able to do: find . -name '*_*_*_*.config' -type f -exec ...
Stéphane Chazelas's user avatar
0 votes

Find files that are not in .gitignore

Late to the party, but if you don't mind installing the ag utility, this is quick and efficient and, by default, ignores files in .gitignore, .hgignore and ignore: ag -g ''
backseat's user avatar
  • 101
0 votes

Can grep output only specified groupings that match?

Compare Perl and Raku solutions: Using Perl (answers from @vault and @Gilles 'SO- stop being evil'): ~$ perl -lne 'print $1 if /^foobar (\w+)/;' file #OR: ~$ perl -lne 's/^foobar\s+// or next; s/\...
jubilatious1's user avatar
  • 3,235
1 vote

Multicolored Grep

I've often needed this, and I tried several solutions, including scripts implemented in bash like hhighlighter. But none of the solutions I tried worked reliably when using multiple patterns, ...
Eugen Dück's user avatar
0 votes

Script to replace matching pattern between file 1 (rev.fasta) and file 2 (necessary_files/index_rev.fasta) IN FILE 1 with information from file 2

My understanding of the problem is that you want to search the fatsa file based on your index file. In my response below the index file is called patterns.txt. I have also changed that file a little ...
user9101329's user avatar
  • 1,103
2 votes

How to use sed, grep, or awk to keep certain lines in a file depending on line numbers in another file

Using Raku (formerly known as Perl_6) #Sample Input: ~$ cat data.txt He is a boy. She is a cook. Okay. She went to school. She is pretty. Successive examples to show indexing: ~$ raku -e '.put for ...
jubilatious1's user avatar
  • 3,235
0 votes

multiline grep search into separate files per occurence

Using any awk: $ cat tst.awk /^ -1/ { if ( inBlock ) { print > out; close(out) } else { out = FILENAME "_" (++cnt) } inBlock = !inBlock } inBlock { ...
Ed Morton's user avatar
  • 31.7k
1 vote

multiline grep search into separate files per occurence

You can use GNU awk, which allows for regular expressions to be used as the record separator, as the thing that defines "lines". Here, we can set that as \n -1\n, so a newline, 4 spaces, -...
terdon's user avatar
  • 242k
0 votes

multiline grep search into separate files per occurence

If it were me: gawk '/^\s{4}-1$/ { X=X+1 } { print $0 >> ( "outfile" X ) }' <inputfile
symcbean's user avatar
  • 5,557
3 votes

multiline grep search into separate files per occurence

With -z (a non-standard GNU extension), grep works on NUL delimited records, it's not multiline grep¹, so: the matching is done on each NUL-delimited record independently or the whole input if not ...
Stéphane Chazelas's user avatar
2 votes
Accepted

multiline grep search into separate files per occurence

Another method to get whole blocks instead of grep First, I suggest to use sed to create the sed -n '/^ \{4\}-1$/,/^ \{4\}-1$/p' example.txt -1 15 1 0 0 11 -1....
aviro's user avatar
  • 5,552
0 votes

How to tail continuously a log file that is being deleted and recreated?

All you need to continuously tail a file across recreations and only output unique lines is: tail -F "$debug_file" 2>/dev/null | awk '!seen[$0]++' and if you only want to consider parts ...
Ed Morton's user avatar
  • 31.7k
0 votes

How to tail continuously a log file that is being deleted and recreated?

Using TXR Lisp: (open-tail "/path/to/debug.log") ;; returns a stream that follows rotating log You just read input from this stream, e.g. using something like: (with-stream (s (open-tail &...
Kaz's user avatar
  • 8,476
0 votes

How to tail continuously a log file that is being deleted and recreated?

It seems to me that you don't need the outer loop. piping tail output into read might allow you to operate in a kind of state machine, where you are in two states. a normal tail state, and a new file ...
avatarofhope2's user avatar
-2 votes

How to grep the last character of each row?

A simple example - found one invalid CNAME entry, and one w an extra space at the end. Ignoring the comment lines ( ;; in bind ) DNSint04 # cat /var/named/internal/db.abanet.org | grep -v ^; | grep -...
Harry Carmichael's user avatar
1 vote

I can't grep some inputrc string

Since you are using a double-quoted string for the regular expression, you have to add one extra backslash to escape the escaping of each literal backslash that you want to match. This is because the ...
Kusalananda's user avatar
  • 334k
0 votes

Find all scripts with a given shebang line with find & sed

If various types of executables regardless permissions (i.e. +x) or shebang: find . \ -type f `# Files only` \ -exec bash -c \ $'file -b -- \'{}\' | grep -qP -- \'executable$\'' \; `# ...
Artfaith's user avatar
  • 474

Top 50 recent answers are included