Tag Info

Hot answers tagged

6

$ sed ':again;$!N;$!b again; s/{[^}]*}//g' file This is that wants anyway. Explanation: :again;$!N;$!b again; This reads the whole file into the pattern space. :again is a label. N reads in the next line. $!b again branches back to the again label on the condition that this is not the last line. s/{[^}]*}//g This removes all expressions in braces. ...


6

Here's an example: $ echo "foo" > file $ grep foo file wrongfile; echo "Exit status: $?" file:foo grep: wrongfile: No such file or directory Exit status: 2 $ grep -q foo file wrongfile; echo "Exit status: $?" Exit status: 0 So, we have a file called file that contains the string foo. When I ran grep with -q on file and the nonexistent wrongfile, since ...


5

awk 'FNR==2 {if (/some string/) print FILENAME; nextfile}' ./* Some awks don't have "nextfile".


5

join fileA fileB That assumes files are sorted on the first column (which they are in your example).


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

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.


4

Processing XML as text is generally not a robust solution, but if you insist on doing it then you could perhaps make use of sed's hold space e.g. sed -e '/<id>[0-9]*<\/id>/h' -e '/<root>/{x;p;x;}' file.xml


4

You can extract a value in your example with grep and assign it to the variable in the following way $ x=$(wget -0 - 'http://foo/bar.html' | grep -Po '<value.*strValue="\K[[:digit:]]*') $ echo $x 57 Explanation: $(): command substitution grep -P: grep with Perl regexp enable grep -o: grep shows only matched part of the line \K: do not show in the ...


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

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> ...


3

You mix up character classes with subpatterns. You need the latter here: grep -E '201411(08|09|10|11)-(117|111)-' file "Character class" means: Every single char from the class (like [[:alpha:]]) or selection ([01][8901]) may appear. "Subpattern" (with |) means: "One of the patterns separated by | may appear."


3

Sed: sed '/{/{:1;N;s/{.*}//;T1}' multiline.file started since line with { and get the next line (N) until substitution ({}) can be made ( T means return to mark made by : if substitution isn't made) A little bit modify to be true if many curle bracked in one line sed ':1; s/{[^}]*}// ; /{/ { /}/!N ; b1 }' multiline.file Remove all symbols in the ...


3

Perl: perl -0777 -pe 's/{.*?}//sg' file If you want to edit in-place perl -0777 -i -pe 's/{.*?}//sg' file That reads the file as a single string and does a global search-and-replace. This will handle nested braced: perl -ne 'do {$b++ if $_ eq "{"; print if $b==0; $b-- if $_ eq "}"} for split //'


3

awk 'FNR == NR { onecol[$0]=1;next; }; $1 in onecol' fileB fileA


3

Edit in place, keeping only lines that match the pattern: sed -n -r -i "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml On BSD sed (Mac OSX), try: sed -n -E -i '' "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml Basing the file name off of an already existing variable for fname in ~/mpp/*.xml do egrep ...


3

At least with GNU grep 2.12: $ grep -Pzo 'xyz(?:.*\n)*?\K~this' file ~this If your grep version does not allow -P with -z, you can use pcregrep instead: $ pcregrep -Mo 'xyz(?:.*\n)*?\K~this' file ~this


3

By omiting the all-important quotes around the expansion of the RESP variable, you told the shell to tear the data apart on whitespace, try to modify the data by injecting filenames, then put it together into a single line before passing it to grep. That's why grep -m1 is giving you all the matches; it's giving you the matches for the first matching line, ...


3

As Rubo77 mentioned, the issue is solved by adding the --line-buffered to the first grep command: tail -f /var/log/syslog|grep --line-buffered s|grep s However, you then may ask, why isn't this needed for a single grep command? The difference between the two is that in the following command: tail -f /var/log/syslog|grep s STDOUT for grep is pointed ...


3

You need to add --line-buffered to grep tail -f /var/log/syslog|grep --line-buffered s|grep s


3

I have no idea what wget you're talking about but I am guessing that you want to download the file. If so, yes, you can download it and parse it with no intermediate temp file: $ value=$(wget -O - http://example.com/file.html | grep -oP 'strValue="\K[^"]+') $ echo $value 57


3

You must tell apart shell escaping and grep escapes. Basic regular expressions (BRE; which are used if grep is used without the option -E) treat (, ) and | as normal chars. The sequences \( and \| have special meaning. Without the quotes the shell treats the backslash as escape character and removes it i.e. grep doesn't see it. With quotes grep sees the ...


3

If you don't mind an extra column with a number in it you can use join and grep to do this. $ join <(grep -of patterns.txt file.txt | nl) \ <(grep -f patterns.txt file.txt | nl) 1 KO3322 proteinaseK (KO3322) 2 KO3435 Xxxxx KO3435;folding factor 3 KO3435 Yyyyy KO3435,xxxx


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

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 ...


2

You could simplify this significantly (and avoid the nasty problems inherent in parsing ls) by doing something like: file_nm=$(find . -maxdepth 1 -name "*abc*"); [ -e "$file_nm" ] && grep -q "search_txt" "$file_nm" && echo "string found" || echo "string not found" However, like your original example, that will fail if you have ...


2

Try the following script: #!/bin/bash logfile="$1" nfiles=$(grep -c 'checking file' "$logfile") failed_userid=($(grep -oP 'failed reading user id: \K[^ ]*' "$logfile")) corrupted_files=($(grep -oP '[^ ]*(?= is corrupt)' "$logfile")) echo "Total Number of Files Scanned - $nfiles" echo "Total Number of Unique User ID failed - ${#failed_userid[@]}" echo ...


2

It is not clear where your bottleneck is. Let us assume it is in reading the files. Depending on your storage system, it is faster to read the whole file before processing it. This is especially true for zipgrep which does a few seeks into the file: If the file is not completely in memory you will be waiting for the disk to seek. find ... | parallel -j1 ...


2

The authoritative source would be http://www.gnu.org/software/software.html from the Free Software Foundation rather than the Wiki page cited in the accepted answer. On that page grep is listed as a separate piece of software.


2

With the zsh shell, you can affect the order of globs with glob qualifiers. grep 'exit 0' a*(.Om) Om is to reverse o⃞rder by m⃞odification time. I also added .⃞ to select only regular files (not directories or pipes or devices or symlinks...).



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