Tag Info

Hot answers tagged

8

With GNU grep, you can use -a option to make it treats binary files as text files: grep -alir string -- file If your grep version does not support -a, you can use ack instead. With ack 1.x, you need to include -a option, with ack 2.x, you don't, since when searching include non-text file by default (only ignored non-text file when you did not specify any ...


8

The command strings will extract all ascii data from a file, if you then grep its output, you can search for your data: strings <filename> | grep "search text"


6

grep tcp *.tcp | grep open | sort -u Giving multiple filenames to grep will, by default, cause grep to prefix the matching output lines with the filename(s) they matched in. The only other change I made was to combine sort | uniq into sort -u (and to remove the quotes that are unnecessary here).


6

don's might be better in most cases, but just in case the file is really big, and you can't get sed to handle a script file that large (which can happen at around 5000+ lines of script), here it is with plain sed: sed -ne:t -e"/\n.*$match/D" \ -e'$!N;//D;/'"$match/{" \ -e"s/\n/&/$A;t" \ -e'$q;bt' -e\} \ ...


6

You could use grep with -A and -B to print exactly the parts of the file you want to exclude but add the -n switch to also print the line numbers and then format the output and pass it as a command script to sed to delete those lines: grep -n -A1 -B2 PATTERN infile | \ sed -n 's/^\([0-9]\{1,\}\).*/\1d/p' | \ sed -f - infile Another way with comm: comm ...


5

You have several options. With grep: grep -v , file > new_file With GNU sed or BSD sed: sed -i.bak '/,/d' file With gawk version 4.1.0 and above: gawk -i inplace -v INPLACE_SUFFIX=.bak '!/,/' file With perl: perl -i.bak -nle 'print unless /,/' file


5

Poking around with various inputs, I felt grep did its own magic for line-endings: $ printf "foo\rbar\n" | grep -oz $'\r' | od -c 0000000 \r \n 0000002 $ printf "foo\rbar\r\n" | grep -oz $'\r' | od -c 0000000 $ printf "foo\rbar\r" | grep -oz $'\r' | od -c 0000000 \r \n \r \n 0000004 (The -z was my lame attempt to make grep match everything.) And so ...


5

This works. ^ denotes start of line, plus the {32} you already had, then a $ for end of line. $ cat fileA fileB 12345678901234567890123456789012 123456789012345678901234567890123 12345678901234567890123456789012 123456789012345678901234567890124 $ grep -E "^.{32}$" fileA fileB fileA:12345678901234567890123456789012 fileB:12345678901234567890123456789012 $ ...


5

The better way is to use pgrep for that task. It seems to be available on most systems these days.


4

Adapting a solution from http://stackoverflow.com/a/9937241/1673337 you can use (g)awk to get: awk 'NR==FNR{a[$0]=1;next} {for(i in a){if($3~i){print;break}}}' File1 File2 which provides the given Output. While you could craft a RegEx to feed into grep to satisfy only matching on the third column, I feel using awk at this point is more understandable. ...


4

If grep is not necessary, one simple solution would be to use join for that: $ join -1 1 -2 3 <(sort file1) <(sort -k3 file2) Locus_1 3 3 Locus_40 etc_849 Locus_2 3 2 Locus_94 * Locus_3 2 3 Locus_4 Locus_50 * Explanation: join -1 1 -2 3: join the two files where in the first file the first (and only) field is used and in the second file the third ...


4

If your shell supports it (zsh, bash, some implementations of ksh), you could utilise process substitution grep <pattern> <(tail -n5 yourfile.txt) Where -n5 means get the five last lines. Similarly, grep <pattern> <(head -n5 yourfile.txt) would search through the 5 first lines of yourfile.txt. Explanation Simply speaking, the ...


4

If you don't mind using vim: $ export PAT=fff A=1 B=2 $ vim -Nes "+g/${PAT}/.-${B},.+${A}d" '+w !tee' '+q!' foo aaa bbb ccc hhh iii -Nes turns on non-compatible, silent ex mode. Useful for scripting. +{command} tell vim to run {command} on the file. g/${PAT}/ - on all lines matching /fff/. This gets tricky if the pattern contains regular expression ...


4

As long as stdout and stderr are line-buffered and the lines of one of the two are always spottable, mixing them is not a problem: consider the output of a program where both stdout and stderr are line-buffered, and where stderr is easily spottable: $ cat file xxxxxxxxxx tar: ---------- yyyyyyyyyy tar: ---------- zzzzzzzzzz tar: ---------- Extracting ...


4

You have \r only as the end-of-line character for lines 2 onwards (up to line 10 at least). Line 1 has \n as the end-of-line character. eg. printf 'ABC\nXYZ\r123\r' | head output (to the screen) ABC This is a display artifact related to terminal output. The \r kicks back to the start of the line and the next line overwrites it, and the last line ...


4

I think it is line-ending related. Excel will save files with carriage return/line feed endings but head will be expecting line feeds only. What output does this display: tr -d '\r' < messy.csv | head -10 If it displays the 10 lines correctly, that's your answer. file can tell you the line ending for certain text files (it will print ..., with CRLF ...


4

You have some solid answers already, however I'll offer an alternative - the XML spec is quite strict, and files that don't start with < isn't actually XML at all. So a simple approach might be to simply test if the file is 'valid' or not. All XML parsers can do this, but here's an example: #!/usr/bin/perl use strict; use warnings; use XML::Twig; ...


4

Your question is about find binary files that contain a pattern (and we have already very good answers!). Complementary we may like to get the occurrences. I often use grep -aPo '.{0,20}pattern.{0,20}' binfile to get a surrounding context of 20-char.


3

The splitting into different parts (command & arguments) does not work if variables are used. Use eval for this case: eval $_rscmd --exclude=alternatives /etc/ $_mn | eval $_grep >> "$_rslog" In general, it is better to use shell functions or aliases than using variables: alias my_grep='grep -E "^deleting|[^/]$"' ... ... | my_grep >> ...


3

Another sed: $ sed -e '/X/{ $!N /\n.*Y/!D }' file 31 X 32 Y 33 X 34 Y 36 X 37 Y


3

Using sed: sed -n ':b /X/ { h; n; /Y/! b b; H; x; p; }' Output: 31 X 32 Y 33 X 34 Y 36 X 37 Y


3

You can give mpv a fake terminal by using the script(1) program, for instance: script -c 'mpv FILE' /dev/null | grep -v 'Error while decoding frame' For Mac OS X, the syntax seems to be: script /dev/null mpv 'FILE' | grep -v 'Error while decoding frame' [edit: also check out Trick an application into thinking its stdin is interactive for additional ...


3

n=$some_num { head -n"$(($(wc -l <in)-n))" >/dev/null grep 'match your string' } <in Unfortunately this requires reading the file entirely through w/ wc to get a line-count because it's not clear otherwise how many lines are in the file or how large $n is. That aside, this should be a very performant solution provided <in is a regular, ...


3

You can do it with: awk '{print NF}' filename


3

grep -F is POSIX. fgrep is not. Use grep -F and make it clear that your script depends on a POSIX grep. Clearly stated dependencies are all you need. I guess you could use both... if command -v fgrep then _grepf(){ fgrep "$@"; } else _grepf(){ grep -F "$@"; } fi >/dev/null ...but doing stuff like that is hacky and usually a waste of time. Set ...


3

To grep the first line of each file and print if they match you can use xargs and awk find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</' {} To print the filename of the files find . -type f -name "*.xml" -print0 | xargs -0 -I{} awk 'NR==1&&!/^</{print FILENAME}' {}


3

If your awk supports the nextfile statement (most do): find . -name '*.xml' -type f \( -size 0 -print -o -exec awk ' !/^</ {print FILENAME}; {nextfile}' {} + \)


3

There are a number of ways, more or less hacky and awkward. My favourite is to insert a double-backslash before a letter in my search string: ps -ef | grep ss\\hd The \\ is interpreted to \ by the shell, and \h is interpreted as equivalent to 'h' by grep. So it is effectively searching for 'sshd' as before. However, its own command string no longer ...


3

When available, just use pgrep which was designed to properly overcome this issue, eg: ps -f $(pgrep sshd) This method is cleaner than the hackish workarounds usually suggested.


2

I'm not sure I decrypted your prose correctly. The script below adds #include <stdint.h> to files in the current directory that (1) contain the word LARGE_INTEGER, and (2) don't already include <stdint.h>: sed -i -e '1i\' -e '#include <stdint.h>' $( egrep -c '#include +<stdint\.h>' $( fgrep -lw LARGE_INTEGER * ) | \ sed ...



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