Tag Info

Hot answers tagged

28

grep by default searches standard input if no files are given: grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. If you just do grep doc grep expects standard input to ...


11

Here is a general pattern: find /directory/containing/files -type f -exec grep -H 'pattern_to_search' {} + Here at first find will search all files in the directory containing necessary files, you can also use wildcards e.g. *.txt to look for only files ending with .txt. In that case the command would be: find /directory/containing/files -type f -name ...


11

grep is waiting for input. From man grep: [...] DESCRIPTION grep searches the named input FILEs (or standard input if no files are named [...]


6

If you want to search for the regexp that is the concatenation of the arguments with space characters in between, that would be: #! /bin/sh - IFS=' ' # used for "$*" grep -r --color=always -e "$*" . | nolong "$*" joins the positional parameters with the first character (actually byte except with yash) of $IFS. Note (as you seem to be confused by the ...


6

grep searches for the first argument (the pattern) in the files passed on the command line or stdin if no files are passed. Without the quote your shell will expand lect* to all the files in the directory that begin with lect. Your command will then be: grep lect1.txt lect2.doc lect3.doc which means search for the text lect1.txt in both .doc files. ...


5

On GNU system, you could use sed (the GNU implementation): sed -i -n '/YISHA/p' file1 The FreeBSD or OS/X equivalent: sed -i '' -n '/YISHA/p' file1 or using sponge from moreutils: grep "YISHA" file1 | sponge file1


5

Another valid method is: find /directory/containing/files -type f -print0 | xargs -0 grep "test to search" The print0 / option -0 is to allow for the case where filename strings contain special characters (like spaces).


5

Use something like this perhaps (if gnu grep). grep -r 'content pattern' --include==*.cpp man grep --include=GLOB Search only files whose base name matches GLOB (using wildcard matching as described under --exclude) Also see the options for null delimiters. -Z, --null Output a zero byte (the ASCII NUL character) instead of the character that ...


4

Via awk awk 'NR==FNR{A[$4]=$0;next}{print A[$1]}' file2.txt file1.txt Or sorted output via join: join -o 2.1 2.2 2.3 2.4 -2 4 <(sort file1.txt) <(sort -k4 file2.txt)


4

grep will show a matching line, so all you have to do is to find the two double-quotes... grep '".*"' If you want to extract a word that is within double quotes you can, for example, do... awk 'match($0,/"[^"]*"/) {print substr($0,RSTART+1,RLENGTH-2)}'


4

If you want to grep recursively in all .eml.gz files, you can use: find -name \*.eml.gz -print0 | xargs -0 zgrep "STRING" You have to escape the first '*' so that the shell does not interpret it. "-print0" tells find to print a null character after each file it finds; "xargs -0" reads from standard input and runs the command after it for each file; ...


4

grep -x '[[:alnum:]]*\.[[:alnum:]]* = [[:alnum:]]*\.[[:alnum:]]*'


4

The Issue Let us start by defining a server name: $ servername=SomeName Now try: $ echo "$servername_log_20150312" $ The above returns nothing because (a) underline is a legal character in a shell variable name, and (b) we never defined servername_log_20150312. Now consider: /server/directory/$servername_log_20150312* After variable expansion, ...


4

join works well here: join -a 1 -o 0,2.2 -e NA <(seq 12) A.txt 2>/dev/null I throw away stderr because join complains if the join field is not lexically sorted.


3

If your version of grep supports PCRE (GNU grep does this with the -P or --perl-regexp option), you can use lookaheads to match multiple words in any order: grep -P '(?=.*?word1)(?=.*?word2)(?=.*?word3)^.*$' This won't highlight the words, though. Lookaheads are zero-length assertions, they're not part of the matching sequence. I think your piping ...


3

wget includes features to support this directly: wget -r -A "*.ai,*.cdr" 'address-of-page-with-hyperlinks' -r enables recursive mode so it will download more than simply the given URL, and -A limits the files it will download and keep in the end.


3

You can use awk to do the counting. Though if nothing more complicated involved you can use tail -f output.log | awk '/Thread started/{n++}/Thread finished/{n--} END { printf ("%d Threads are running\n", n)}' output.log Better yet, use watch as follow: watch -n.2 -x awk '/Thread started/{n++}/Thread finished/{n--} END { printf ("%d Threads are ...


3

When removing data, you can write the file over itself and truncate it afterwards: { grep YISHA perl -e 'truncate STDOUT, tell STDOUT' } < file 1<> file Of course here, you can do everything in perl: perl -ne 'print if /YISHA/; END{truncate STDOUT, tell STDOUT}' < file 1<> file perl also has a -i option for in-place editing (the ...


3

Yes, its possible with the -n option of grep. From man grep: -n, --line-number Prefix each line of output with the 1-based line number within its input file. For example, if you have a file named file.txt having: this is foo test and this is bar test Now the output of grep -n "test" file.txt: $ grep -n "test" file.txt 2:foo test 4:bar ...


3

You can do this with an awk script: awk '{ while (NR + shift < $1) { print (NR + shift) " NA"; shift++ }; print } END { shift++; while (NR + shift < 13) { print (NR + shift) " NA"; shift++ } }' /tmp/test1 will produce the required output for /tmp/test1 (replace that with each file you wish to process). In a more readable form: #!/usr/bin/awk -f { ...


3

$ file corncob_lowercase.txt corncob_lowercase.txt: ASCII text, with CRLF line terminators Probably the DOS line endings are the source of your problem. CR is counting as a character for at least some purposes. Run it through dos2unix, or tr -d '\r', before greping.


3

If there is only one foo3 in line sed -n '/foo3=/{s/.*foo3=//;s/\S*=.*//;p}' file.txt Suppress printing any line (-n options) exept which pushed by p. For lines which consists foo3=: Exchange everything before foo3= with it included (.*foo3=) to nothing (//). Remove everything which starts with some(*) non-space (\S) symbols with =. Prints resedue ...


2

Recursion alone is easy: -r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option. -R, --dereference-recursive Read all files under each directory, recursively. Follow all ...


2

PCRE is installed on pretty much all server and desktop Linux systems, but you can't necessarily expect it on lightweight systems or embedded systems (phones, routers, TVs, and other IoT), as they often have very trimmed versions of the standard userland (basically, anything with a busybox base is all but guaranteed to lack PCRE). Debian has a Popularity ...


2

Grepping twice wouldn't bring you any benefit. I'd go for using egrep in combination with a regex that matches you need. ps ax | egrep '(a.*){2}' That would give you all processes, that have the 'a' twice. You can of course use that on any kind of input egrep can read.


2

you almost make it awk '(NR>1) && ($8 > 2 ) ' foo > bar where NR is number of record (that is number of line) $8 is eight field && is logical and foo is the original file, unchanged bar resulting file implicit default action is to print the current input line note that header is striped from foo to bar, to keep it awk ...


2

Your shell will likely get you a secure temp file on request: grep "YISHA" <<IN > file $(cat file) IN That will drop blank lines from tail of file though (which shouldn't be relevant unless you're grepping for blank lines). If that should matter, then just echo . after cat in the command substitution and drop the last line. Another option ...


2

Yes, you can make a script like this: for who_user in $(who|cut -f1 -d' '|uniq) do getent passwd|grep ${who_user} done If you have enough permission you could look for the password at your shadow file like this: for who_user in $(who|cut -f1 -d' '|uniq) do cat /etc/passwd|grep ${who_user} cat /etc/shadow|grep ${who_user} done As mentioned ...


2

You can try the following bash script: #!/bin/bash start_count=$(grep -c "started" /path/to/output.log) finish_count=$(grep -c "finished" /path/to/output.log) echo "$((start_count - finish_count)) threads are running" This considers any previously running threads that are out of the printable scope of tail -f. Here we have counted the occurrences of ...



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