Tag Info

Hot answers tagged

6

This searches for any TYPE=sometext on stdin (-Pis for Perl-like regex, where \w+ is "one or more non-whitespace characters) and prints only the matching parts of the input (-o). It then sorts it (required for using uniq), which then prints for every TYPE=something how often it occured (-c).


5

Just use double quote instead of single quote, and you don't have to use cat (See UUOC): grep -F -- "$PWD" file And remember that without -F, $PWD would be treated as a regular expression as opposed to a string to be looked for in the file.


5

What constitutes a word as far as selection with double-clicking is concerned is terminal (and/or X toolkit) dependant and for some terminals, customizable. For xterm, characters are organised in classes (letters, spaces...) and double clicking selects adjacent characters of the same class. The default is described there. In that default, : is not in the ...


5

grep, that is g/re/p is a basic tool to print the lines that match a regular expression. You want more here like a stream editor: sed '/^begin$/,/^end$/!d' or a more general text processing tool with an advanced language like awk, perl... as you already found out. Having said that, some grep implementations can go a little further. pcregrep -M ...


5

You could do it by using a simple join: join A.txt B.txt But, in order to work both files must be sorted on the join key (here the first (blank separated) field). To do it, just use sort -b filename.


4

One approach is to escape those special characters before calling grep. With zsh: string='123*.$[]' grep -e "${string//(#m)[[^.\\\$*]/\\$MATCH}\$" file or POSIXly: escaped_string=$(printf '%s\n' "$string" | sed 's/[[^.\$*]/\\&/g') grep -e "$escaped_string\$" file or supposing the $string doesn't contain \E, with GNU grep or pcregrep: grep -Pe ...


4

That message is output on stderr like all warning and error messages. You can either drop all the error output: tail -f file 2> /dev/null Or to filter out only the error messages that contain truncate: (tail -f file 2>&1 >&3 3>&- | grep -v truncated >&2 3>&-) 3>&1 Or with zsh or bash: tail -f file 2> ...


4

grep -T will work 7/8ths of the time. % for f in a ab abc abcd abcde abcdef abcdefg abcdefgh; do echo pattern > $f; done % grep -T pattern * a :pattern ab :pattern abc :pattern abcd :pattern abcde :pattern abcdef :pattern abcdefg:pattern abcdefgh :pattern From the GNU grep manual: -T --initial-tab Make sure that the ...


4

When a user invokes sudo -l it lists what sudo will allow them to do, so you could have a script ran as root that bumps through /etc/passwd and sudo's to each user, invoke the sudo -l, directing the output to /tmp/${USER}_sudo_i_can_do.txt But if you don't have root access, you won't be able to do what you want to do; the list of permissions is readable ...


4

grep -E '[0-9]{5}' is looking for numbers with at least 5 digits. What you need is 5 numbers with at least one digit: grep -E '[0-9]+([^0-9]+[0-9]+){4}' [0-9]+ - a number of at least one digit [^0-9]+[0-9]+ - a number with at least one digit, preceded by at least one non-digit character. We then repeat this 4 times to get 5 numbers separated by ...


4

With grep, filter out just the numbers: grep -Eo '[0-9]+-' file | sort -u | wc -l [0-9] Matches any character between 0 and 9 (any digit). + in extended regular expressions stands for at least one character (that's why the -E option is used with grep). So [0-9]+- matches one or more digits, followed by -. -o only prints the part that matched your ...


3

You probably want the -wflag - from man grep -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at ...


3

This cannot be done by grep itself as far as I can tell. Assuming your filenames don't have a : in them: grep ... | sed 's/:/ : /' Only the first : will be padded. Of course, you can tell grep to only print filenames: grep -l ...


3

Since you have also tagged awk: awk 'FNR == NR {a[$1] = $0; next}; {print a[$1]}' A.txt B.txt I don't think a single grep can do this, but a combination of xargs and grep: xargs -I{} grep -Fw -- {} A.txt < B.txt


3

You have to sort using names first. Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use `sort -u' without `uniq'. You can use the -t/-k options, to sort these fields: sort -t',' -k 3 marathon that sort regarding the 3rd field with the comma as separator. Then you can print ...


3

There is a lot to be improved about your approach: On most systems (if /proc is not mounted with hidepid) you don't need root privilege for ps. There is no need for two grep instances just to get rid of the first in the process list. Do this instead: grep '[w]get' There is no use in grep filtering input for awk. awk can do that pretty well on its own: awk ...


2

If grep doesn't get rid of the output, it's most likely being printed on standard error. The simplest way to get rid of that is to simply dump it: tail -f messages.log 2>/dev/null


2

It reads from its standard input (file descriptor 0) if not given any filename. grep foo is equivalent to: grep foo - That allows things like: grep foo < file Or: cmd | grep foo If grep foo is run at the prompt of an interactive shell in a terminal, then grep will read from the terminal device, so from what you enter via the keyboard.


2

From man grep: file A pathname of a file to be searched for the patterns. If no file operands are specified, the standard input shall be used. So it will wait for you type some text if there's no pipeline or redirection involved.


2

Try this with GNU grep: grep -f Pattern.txt File.txt


2

one can use awk too: awk 'NR==FNR{a[$0]=$0}NR>FNR{if($1==a[$1])print $0}' pattern_file big_file output: denovo1 xxx yyyy oggugu ddddd denovo22 hhhh yyyy kkkk iiii


2

I created the two files with the same contents as mentioned and used grep in the same way and it worked fine. I hope you are using the same file names (I see the .txt extension missing in the question). [sreeraj@server ~]$ grep -f file_A.txt file_B.txt > file_C.txt [sreeraj@server ~]$ cat file_C.txt comt241_c0_seq1 0 0 0 0 0 0 0 0 0 ...


2

Your grep command seems correct. Except you are using different filenames: grep -f list_A list_B instead of ~$ grep -f fileA.txt fileB.txt comt241_c0_seq1 0 0 0 0 0 0 0 0 0 comt868_c0_seq1 0 0 0 0 0 0 0 0 0 comt363_c0_seq1 0 0 0 0 0 0 0 0 0 comt384_c0_seq1 0 0 0 0 0 0 0 0 0 comt685_c0_seq1 ...


2

journalctl can display logs for all units - whether these units write to the log is a different matter. To list all available units and therefore all available for journalctl to use: systemctl list-unit-files --all


2

The usual way would be to use pgrep: $ pgrep init 1 2215 6300 $ ps ax | grep init 1 ? Ss 6:41 /sbin/init 2215 ? Ss 1:54 init --user --restart --state-fd 26 6300 ? S 0:00 init --user --startup-event indicator-services-start 17522 pts/10 S+ 0:00 grep --color=auto init Note that you might need to use other tricks ...


2

You could use something like this: find . -name "*.log" | xargs grep -E 'fatal|error|critical|failure|warning|' This will find every file with .log as extension and apply the grep command.


2

grep -E 'fatal|error|critical|failure|warning|' *.log


2

Another perl: $ perl -MList::Util=first -Tnle ' s/^\s+|\s+$//g; @e = split /\s+/; print if @e == 5 || @e == 6 and !first {/\D/} @e; ' file 10 2 12 1 13 Explanation s/^\s+|\s+$//g trim the line. @e = split /\s+/ split the line into array @e. We will print the line if: array @e contains 5 or 6 elements. And None of its ...


2

grep -E '^(\s*[0-9]+\s+){4,5}[0-9]+\s*$'


2

\> is the (zero length) regexp for the end of the word so C\> will probably not match last names that start with a 'C'. Maybe you should try \<C instead. [[:alpha:]] matches exactly one character so this is also very unlikely to match a real name. You should append a multiplier like * or + (only in ERE?).



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