New answers tagged

0

You can simply do from the base directory you want to search cp $(grep -r -l "the pattern" *) target_directory grep -r means recusive -l means list the file path not the match in it. putting $() around the command will execute it and place the result in place in the outer command. cp will take a list of files, and as long as the last path is a directory ...


4

Do man xargs and look at the -I flag. find /path/to/directory -type f -exec grep -il "your pattern" {} \; | xargs -I % cp % /dest/dir/ To save typing put this command in shell script : #!/usr/bin/ksh # Script name: locate find /path/to/directory -type f -exec grep -il "$1" {} \; | xargs -I % cp % /dest/dir/ To run type locate searchstring where ...


3

With awk it's as simple as awk -F, -vOFS=, '{$1=n++; print}' n=800 file You set the input and output delimiters to , via -F, and -vOFS=, and initialize a variable n to 800 using n=800. The "action" statement {$1=n++; print} executes for each record, setting the first field to a post-incrementing n and printing the reconstituted record


0

This is a real bug found in find version 4.4.2, but the bug has been fixed in find version 4.6.0.


0

The program you're looking that can easily do what you want is called awk. :-) It can do programmed actions on matched RE patterns. Untested, simplified, rote, example awk program that should work with your example input and specified patterns: BEGIN { eights = 0; fives = 0; threes = 0; } /8888/ { eightln[eights] = $0; eights++; } ...


3

Alternative way with sed, e.g. for N=2 sed '1N;$!N;/.*\n.*\n.*pattern.*/P;D' infile On 1st line this will read the next N-1 lines in the pattern space then start a N;P;D cycle - read another line in and if the last line in the pattern space matches, print the first line in the pattern space then delete it, starting a new cycle. The downside is that ...


8

A buffer of lines needs to be used. Give a try to this: awk -v N=4 -v pattern="example.*pattern" '{i=(1+(i%N));if (buffer[i]&& $0 ~ pattern) print buffer[i]; buffer[i]=$0;}' file Set N value to the Nth line before the pattern to print. Set patternvalue to the regex to search. buffer is an array of N elements. It is used to store the lines. Each ...


2

tac file | awk 'c&&!--c;/pattern/{c=N}' | tac But this has the same omission as the 'forwards' use case when there are multiple matches within N lines of each other. And it won't work so well when the input is piped from a running process, but it's the simplest way when the input file is complete and not growing.


7

That code doesn't work for previous lines. To get lines before the matched pattern, you need to somehow save the lines already processed. Since awk only has associative arrays, I can't think of an equally simple way of doing what you want in awk, so here's a perl solution: perl -ne 'push @lines,$_; print $lines[0] if /PAT/; shift(@lines) if $.>LIM;' file ...


2

There is no such kernel option as far as I can tell. You can take a look yourself through the kernel source if you want; relevant things to search for are set_personality and current->personality. The flag you want is PER_LINUX32. Short of patching the kernel, your solution of overriding init is probably the simplest one. This isn't really out of the ...


0

What about using https://github.com/zsh-users/zsh-autosuggestions? The thing is, for each command you type this plugin will show you some suggestions that could be accepted or not. Try it out and give us some feedback.



Top 50 recent answers are included