Tag Info

Hot answers tagged

7

You can use perl compatible regular expressions grep: $ pcregrep -M '(searchString.*\n)(?!.*excludeString)' file foo2 searchString bar foo3 searchString bar foo4 searchString bar It searches searchString followed by any char ., repeated zero or more times *, followed by new line \n only if there is not (?!) pattern .*excludeString next to it. Option -M is ...


6

You can pipe grep result to head. Note, that in order to ensure stopping after Nth match, you need to using stdbuf to make sure grep don't buffer its output: stdbuf -oL grep -rl 'pattern' * | head -n10 As soon as head consumed 10 lines, it terminated and grep will receive SIGPIPE because it still output something to pipe while head was gone. This ...


5

nc writes its output to standard error, you need: nc -zvv localhost 31000-32000 2>&1 | grep succeeded The 2>&1 will redirect standard error to standard output so you can then pipe it to grep.


5

grep -e foo -e bar -e baz files or: grep 'foo bar baz' files If those foo, bar, baz are meant to be strings as opposed to basic regular expressions, add the -F option to those 2 grep commands above. For extended regular expressions: grep -E 'foo|bar|baz' files If the list of strings is in the positional parameters ($1, $2...): NL=' ' IFS=$NL # or ...


5

With sed: sed '/searchString/!d;$!N;/\n.*excludeString/!P;D' infile How it works: /searchString/!d deletes the line if it doesn't match searchString and reads in a new line, starting the command cycle over again (i.e. the remaining commands are no longer executed) if the line matches searchString, sed executes $!N;/\n.*excludeString/!P;D - see HERE ...


5

I do this with egrep -o or grep -E -o The -E flag in grep activates regex (which is what egrep does by default), and the -o flag prints only the matching string. grep -E -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /path/to/log 192.168.1.11


5

Here is a POSIX way to prune any non readable directory with find : find . \( -exec sh -c ' if [ ! -r "$1" ] ; then { exit 1 ; } ; else for i in "$1"/* ; do if [ -d "$i" -a ! -r "$i" ]; then exit 1; fi; done; fi ' sh {} \; -o -prune \) -a -print Note that if this is a full Solaris installation, GNU grep is available in /usr/sfw/bin/ggrep.


5

A function would probably be better, yes, but in this case the problem probably is because you are using double quotes around it. The $ variables get expanded before the assignment to the alias. You can use a single quote on the outside instead, or, because you already have single quotes, escape the $ with \: alias findrails_pid="ps aux | grep rails | ...


4

You're showing us the output of find . -maxdepth 1 -mtime -3 -print0 What's the output of find . -maxdepth 1 -mtime -3 -print0 | grep -z loader ?  I don't see any file names containing the word loader in the output that you did show.  If the output from the grep (which is the input to xargs) is nothing, then, naturally, nothing will be copied. Also, ...


4

I would use strings that way : strings 400Gfile.bin | grep -C 10 searchstring To start at a given offset (eg: 20G), dd if=400Gfile.bin bs=20G skip=1 | strings | grep -C 10 searchstring


4

sort -k1,1 -u file Sort file by first column and take first unique entry only.


4

On a GNU system, you can use this: sed -i '/^#[[:blank:]]Person/{n;s/#root:[[:blank:]]\+marc/root:\tsomeone@something.tld/;}' file It searches for a line beginning with # Person. Then switches to the next line and replaces #root:<blanks>marc with root:<tab> .... The -i flag edits the file inplace. -i, \+ and \t are GNU extensions. The ...


3

grep -o '192.168.[0-9.]*' datfile | sort -u May not be portable to ancient versions of grep, but -o seems to be in both GNU and *BSD grep, so... (it's also not a perfect match on a IP address, but doing the proper number ranges with regex is super annoying.)


3

This means capture an underscore. Examples of matches include: A A_ AAA A_123 A_abc Use a site such as https://regex101.com/ to test your regular expressions and get an explanation on what each part means.


3

With the -F option, grep looks for exact matches (regex features turned off): $ grep -F "defabc" xyz.txt defabc grep sets an appropriate return code so that we can test for true or false: $ if grep -qF "defabc" xyz.txt; then echo True; else echo False; fi True $ if grep -qF "Defabc" xyz.txt; then echo True; else echo False; fi False Because we only ...


3

awk 'FNR == NR && $0 !~ /^[[:blank:]]*$/ { Dict[$0] = 1 } FNR != NR { i = 1 while( i <= NF && Dict[ $i] == 1) i++ if( i > NF) print } ' File1.txt File2.txt generic, non dependant to file2 number of field/word per line work with sorted and unsorted content of both file use memory to load first ...


3

egrep (or grep -E) can do OR: egrep "string|string|string" <file>


3

grep will return success if it finds at least one instance of the pattern and failure if it does not. So you could either add an else clause if you want both "does" and "does not" prints, or you could just negate the if condition to only get failures. An example of each: if grep -q "$user2" /etc/passwd; then echo "User does exist!!" else echo ...


3

The two options you mentioned are specific to GNU grep. -m 500 -- stop reading after the first 500 matching lines -C 0 -- print zero lines of output context The behavior with -C 0 is already the default behavior, so there's nothing special needed to emulate it on AIX. All you need to worry about is how to limit output to the first 500 matches. One way to ...


3

grep -l "Nan" * Or, if you want to start nixing output, grep "NaN" * | grep -v ... I probably should also mention another possible next step, which is grep -l "NaN" * | xargs grep ... as grep needs filenames to work on for a second pass with a new search term.


2

^=* means search for a line starting with zero or more equal signs. If you want lines starting with = just use ^=


2

The following script is compiling file1.txt into a single regex for grep -E. #!/bin/sh regex="^($(awk '{printf $0"|"}' $1) )+\$" grep -E "$regex" $2 Usage: $ ./script.sh file1.txt file2.txt SORTED_RECIPS HFILTER_HELO_5 $regex is compiled from file1.txt as follows: ^(ME_HTML_ONLY|SUSPICIOUS_RECIPS|SORTED_RECIPS|HFILTER_HELO_5| )+$ For thousands of ...


2

GNUly: find . -iname '*.gif' -print0 | grep -z loader | xargs -r0 cp -t /home/me POSIXly: find . -name '*.[gG][iI][fF]' -path '*loader*' -exec sh -c ' exec cp "$@" /home/me' sh {} + (I used -path so it be equivalent with the grep solution, but it sounds like you want loader to be found in the name of the file as opposed to its directory components, ...


2

I hope i understand it right. cat file1 file2 | grep -E '(^Restoration\ was\ successfully|^Server\ restoration\ is\ complete)' When the files start with single-quotes : cat file1 file2 | grep -E '(^\'Restoration\ was\ successfully|^\'Server\ restoration\ is\ complete)'


2

To make grep print only the file name, pass the -l option. To search for a substring rather than a regular expression, pass the -F option. To search recursively for files whose name matches a certain pattern, use find with the -type f and -name PATTERN primaries. Use -exec to invoke grep. find . -name '*.sas' -type f -exec grep -F -l 'Carhart' {} + If ...


2

How about this. Use of ">>" to append to file rather than overwriting the file each time. rm foo.txt for f in *.dump; do strings $f >> foo.txt; done To aid your reviewing, you might want to also pipe it through "sort -u" to remove any duplicates. Might take a while to run if you've many large .dump files.


2

(Extracted answer from edit) I got the result of the search by letting it run overnight. The command I used was: @ubuntu:~/WORKING_DIRECTORY/LC_ALL=C fgrep -rFf bar > ~/myfile Moving the results file, called myfile in the above command, definitely helped. I also sorted the strings in the file. It was originally called strings, and I learned from one ...


2

grep -of file1 file2|xargs -I {} grep -o "{}" file3 This starts by taking the input of file1 and feeding it in line by line into file2, returning the exact matched text if any. Then the results if any are fed into file3 line by line again returning only matched text.


2

Well, it's not that clear, what you actually want to do, but it's clear, that you need to parse those values out of the file of yours. To do this, just put the values in separate arrays, like this (you need to set YourFile with the actual file with the values): H=() V=() while read -r Line; do H+=("$( printf "%s" "$Line" | grep -o 'column=[^,]*' | sed ...


2

With awk: awk '!($1 in a){a[$1];print}' <file or: awk '!a[$1]++' <file



Only top voted, non community-wiki answers of a minimum length are eligible