Tag Info

New answers tagged

2

As a grep standard regular expression \*.\?[A-Z]\?.* means search for: a literal asterisk * followed by zero or one of any character followed by zero or one of any character from A to Z inclusive followed by zero to any number of any characters (to end of line in this case) That is what it means, but the .\? and [A-Z]\?are rather meaningless (or at ...


0

For the record, here's the approach that I prefer: grep pattern $(find . -type f ! -path './test/main.cpp') By keeping the grep at the beginning of the command, I think this is a little more clear -- plus it doesn't disable grep's color highlighting. In a sense, using find in a command-substitution is just a way of extending/replacing the (limited) ...


1

I could write a book : "The lost art of xargs". The find ... -exec … '; launches a grep for each file (but the variant with -exec … + doesn't). Well, we're wasting CPU cycles these days so why not, right? But if performance and memory and power is an issue: use xargs: find . -type f \! -path 'EXCLUDE-FILE' -print0 | xargs -r0 grep 'PATTERN' GNU's find's ...


3

I don't think it's possible with GNU grep. You don't need pipes though. With find: find . ! -path ./test/main.cpp -type f -exec grep pattern {} + With zsh: grep pattern ./**/*~./test/main.cpp(.) (excludes hidden files, just as well to exclude the .git, .svn...).


3

grep can't do this for file in one certain directory if you have more files with the same name in different directories, use find instead: find . -type f \! -path './test/main.cpp' -exec grep pattern {} \+


0

Actually answering the question "What makes grep consider a file to be binary?", you can use iconv: $ iconv < myfile.java iconv: (stdin):267:70: cannot convert In my case there were Spanish characters that showed up correctly in text editors but grep considered them as binary; iconv output pointed me to the line and column numbers of those characters


1

If your find supports -path which was added to POSIX in 2008 but still missing in Solaris: find . ! -path ./test/main.cpp -type f -exec grep pattern /dev/null {} +


5

Use option -h. -h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.


2

These are effectively multi-line records separated by a blank line. Awk is great for handling this kind of data: pactl list sink-inputs | awk -v RS="" '/VLC/' If you want to be really nit-picky about not including the bottom part of the record after the first occurrence of "VLC", then: pactl list sink-inputs | awk -v RS="" -v FS="\n" '/VLC/{ for(i=1; ...


0

Use cut to extract the columns you need (providing the column numbers for us would have been a kindness), and sed to alter the text: grep ... | cut -d, -f2,9,10,11,26 | sed -e 's/CURRENCY/META/' -e 's/"//g' outputs 20150518 11:36:09,7A104802E728,4529800000,123456789,META Another approach, since your input data looks like well-formed CSV (ignoring the ...


5

With ed: ed -s <<'IN' r !pactl list sink-inputs /VLC/+,$d ?Sink Input?,.p q IN It reads the command output into the text buffer, deletes everything after the first line matching VLC and then prints from the previous line matching Sink Input up to current line. With sed: pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}' It appends ...


5

I'd use Perl's paragraph mode: pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' The -00 sets the input record separator to \n\n so a "line" is a paragraph. Then, the substitution will match everything until the first VLC and then anything until the 1st newline and save them as $1. Everything after that is removed (since we're ...


2

With any Bourne-like shell: { cat < bigfile | grep -v to-exclude perl -e 'truncate STDOUT, tell STDOUT' } 1<> bigfile For some reason, it seems people tend to forget about that 36 year old and standard read+write redirection operator. (the cat is for GNU grep that otherwise complains if stdin and stdout point to the same file).


2

could you use awk, at least in place of the second grep? Something like grep -a --binary-file=text "DLDM" /home/path/ldm.log-$1* | awk -F, "/$line/ {print \$2 \",\" \$9 \",\" \$10 \",\" \$11 \",META\"}" Note all the escaping is so you can use $line in the pattern, if you don't need it to be a variable you could use single quotes for the outer quotes and ...


6

The GNU grep can do it grep -z 'is\san\sexample\sfile.' file To fulfill some points which arise in comments there are some modifications to script: grep -oz '^[^\n]*\bis\s*an\s*example\s*file\.[^\n]*' file Regarding huge files I have no imagination of memory limitation but in the case of problem you are free to use sed sed '/\bis\b/{ :1 ...


6

Try this: pcregrep -M '\bThis\s+is\b' <<EOT This is an example file. EOT


5

grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function. The ^ prepended to the argument is a standard regular expressions modifier that matches the beginning of a line -- this way you can ensure that ...


1

You are referring to regex back-references. Please check these two references: http://stackoverflow.com/questions/4609949/what-does-1-in-sed-do http://www.gnu.org/software/grep/manual/html_node/Back_002dreferences-and-Subexpressions.html And see the output of grep '\([0-9]\)\1' /etc/services which will give you a resultset of lines where a digit is ...


4

You can just use grep as a pager: man -P 'grep NR' awk but it is way better to just search for pattern with / in less (that is probably your default pager), so: man awk and then /^ *NR This way you will find only headers (patterns at the beginning of the lines).


3

With the command you tried, echo is printing every word in the manual page on a single line. You would have had a better luck with: echo "`man awk`" | grep NR or better echo "$(man awk)" | grep NR or even better, given the fact echo is useless here: man awk | grep NR Note that most if not all man implementations detect their output is a pipe and ...


10

You still use grep... with the -v option, which tells grep to print only those lines which do not match the pattern: grep -v pattern myfile


0

With one pass and with one command and on a GNU system and enough memory to store the whole file content, and you don't care about the order of those last 3 include lines, you could do: awk '{c=c$0RT}/#include/{l[n++%3]=$0RS}END{printf"%s",(n>=3?l[0]l[1]l[2]:c)}' file.c


1

tail -3 will not print more than 3 lines; it prints 3 or fewer. So if 'grep' finds 3 or fewer matching lines, tail will pass them ALL to the stdout. If there are more than 3 matches, tail will limit them to 3 lines only. You have solved your problem before even posting it here as your command does exactly what you need... That's what happens when you're ...


0

There may be more elegant solutions to find what you're looking for than trying to grep everything in a filesystem, but you could use find to help and build a list of places to exclude like find / \( -path /proc -o -path /<other> \) -prune -o -type f -exec grep -H "pattern" {} + or something like that. It sure feels like there may be a better way ...


0

grep searches lines by default and can therefore normally not be used to find the character between lines. When I search for newline characters I usually replace the newline character (assuming UNIX line breaks here) with a different character that I know not to exist in the text, like so: cat file.html | sed 's/\n/%\n/g' | grep '<h2>%' | tr --delete ...


3

You need two things to match the line breaks (hence multiple lines) using grep : -z option of newer GNU grep, it will cause the lines to be separated by ASCII NUL rather than line breaks (?s) is called DOTALL modifier (with grep -P), it will cause the grep to match the line breaks (LF/CR) by . (dot) So in your case the following should work: grep -aPoz ...


0

This is an odd question, especially considering that the command you put forth in the original post does exactly what you want it to? For example, in file.c, there are 15 lines that match the grep expression: % grep -c "#include" file.c 15 % grep "#include" file.c | head -18 #(more than the number of matches) #include <sys/blah.h> #include ...


0

You can do like Sildoreth says without the ";" like this: test $(echo `grep "#include" tester.c` | wc -l) -ge 3 && {echo `grep "#include" tester.c` | head -n 3} || cat tester.c


0

Try this: lines=$(grep "#include" tester.c); test $(echo $lines | wc -l) -ge 3 && {echo $lines | head -n 3} || cat tester.c; unset lines Note: this solution can by typed as one line. I have separated it into multiple lines for readability only. EDIT In response to the comments, I suppose you could replace the semicolons with ...


2

Here is one approach: awk 'BEGIN{FS="\t"} (($5!=0)+($6!=0)+($7!=0)+($8!=0))>=2' And a terser variant for the C hackers: awk -F $'\t' '(!!$5+!!$6+!!$7+!!$8)>=2'


0

The wildcards in your regular expressions are expanded by the shell. The shell treats them as filename metacharacters. So, you have to tell the shell to not evaluate them as filename metacharacters and you do that by quoting them using single quotes, double quotes, or backslash character just before the metacharacter. Then, the shell sends the expression ...


3

As far as your patterns are concerned, this would be the safest to match only intended strings: grep 'AB.\{0,1\}-DEF' file.txt Or grep -E 'AB.?-DEF' file.txt . matches any single character, ? and \{0,1\} matches the previous token zero or one time, so in total .? and .\{0,1\}will match zero or one character before -DEF. If you use AB.*-DEF or AB.*DEF, ...


1

You can use: grep 'AB.*-DEF' file.txt


8

* in a regex is not like a filename glob. It means 0 or more of the previous character/pattern. So your examples would be looking for a A then 0 or more B then -DEF . in regex means "any character" so you could fix your pattern by using grep 'AB.*DEF'


0

The normal version of grep (including grep -P) always outputs a line feed with its match, so if you only have one result (or you only want the final added line feed to be removed), it suffices to simply remove the final character of the output, which you can do by piping it through head -c-1.


1

For input that you mentioned in a comment: server235: type14: destIP 345.56.78.78 srcIP 67.78.89.67: those details are spread out on different lines throughout the file. It looks a bit like a configuration file actually. I need it to get it to read on one line in a spread sheet . server235 - dest IP 345... srcIP 67. Try this command first, then read ...


0

Tried this with the sed sed -n '/===/{ h d } /night/{ G s/\(.*\)\n\(.*\)/\2\ \1/p }' inputFile This is not exactly what you wanted, but quite similar. === file23.txt night78 === file23.txt night === log3.c night3 /== hold current pattern in buffer /night match all lines containing token night G append current buffer to the current pattern space. /s ...


4

Some more choices. I have saved your example text in file for simplicity. grep and PCREs: $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' file purple yes violet affirmative To get them on the same line, just parse. For example $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' | paste -d" " - - – purple yes violet affirmative sed $ sed ...


2

Using Awk: awk -v RS="," -F= '/GRAPE/||/FRUIT/ {printf "%s ", $2}' Changes the record separator from a new line to , and the field separator from a space to a =, then match lines that contain the pattern GRAPE or FRUIT and print the second matching field on the same line separated by a space. Result: purple yes


0

Use grep with option "-h" . -h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search. find ./files/ -name "*.txt" -print0 | xargs -0 grep -h "5|20150507"


4

Different tools and versions thereof support different variants of regular expressions. The documentation of each will tell you what they support. Standards exist so that one can rely on a minimum set of features that are available across all conforming applications. For instance, all modern implementations of sed and grep implement basic regular ...


1

I suspect that grep and sed are deciding differently when to apply the [] and when to expand the \w. In perl regex \w means any word character, and [] define a group to apply any of the characters within as a match. If you "expand" the \w before the [] it will be a character class of all the word characters. If, instead you do [] first you will have a ...


1

grep command have -r option it searchs text recursive over directory example below: grep -r "5|20150507" ./ | awk -F ':' {'print $2'}


3

You are correct - \w is part of PCRE - perl compatible regular expressions. It's not part of the 'standard' regex though. http://www.regular-expressions.info/posix.html Some versions of sed may support it, but I'd suggest the easiest way is to just use perl in sed mode by specifying the -p flag. (Along with the -e). (More detail in perlrun) But you don't ...


0

find ./files/ -name "*.txt" -print0 | xargs -0 grep "5|20150507" | awk 'BEGIN{FS=":"} {print $2}' You may have to play with the delimiter "FS=" as it could be a special character.


2

You can do it with awk with something like this: awk '/===/ {SEC=$0;PRINTED=0} /night/ {if(!PRINTED) print SEC; print $0; PRINTED=1;}' <file> which will look for the === line and store that, and note that it has not yet printed that one. Then if it finds the pattern night it will check if it has already printed the section header or not, print it ...


14

Characters of code 0 to 31 in ASCII are control characters. When sent to a terminal, they're used to do special things. For instance, \a (BEL, 0x7) rings the terminal's bell. \b (BS, 0x8) moves the cursor backward. \n (LF, 0xa) moves the cursor one row down, \t (TAB 0x9) moves the cursor to the next tabulation... \r (CR, 0xd) moves the cursor to the first ...


5

\x0d is the character \r which brings the cursor to the start of the line, then \x20 is a space, so it overwrites the a with a space. If you're on a unix-y system you may want to consider just removing \r from your output/file since it's not needed if it's for text output. The \n "implies" it for *nix, but not for Windows.


1

With zsh on a GNU system: for f (**/*.out(.)) tac < $f | grep --label=$f -m1 string


0

With gnu tools, you could use a single invocation of gawk: awk '/pattern/{l=$0} ENDFILE{if (l) {print FILENAME ":" l; l=""}}' **/*.out (if line matches store it into l; at the end of each file, if l is not empty print file name and l then reset l) or sed: sed -ns '/pattern/h;${x;//{F;p}}' **/*.out (if line matches, copy to hold space; if la$t line, ...



Top 50 recent answers are included