Tag Info

New answers tagged

2

Here's a sed solution (with -n, auto-printing disabled) that should work with unknown input: sed -n '/SomeTestAA/!p # if line doesn't match, print it : m # label m //{ # if line matches $!{ # and if it's not the last line n # empty ...


2

One option is to use perl compatible regular expression grep: pcregrep -Mv 'SomeTestAA.*\n' file The option -M allows pattern to match more then one line.


2

You might have better luck with something that looks at multi-line regions as single records. There's an sgrep which I haven't used much. There's also awk, where you can set the input record separator, and output record separator, to whatever you like. pat="^SomeTestAA" awk 'BEGIN{ RS=ORS="\nEndTest\n"} !/'"$pat/" foo Most of the awk program is ...


4

You can use grep with -P (PCRE) : grep -P -A 1 'SomeTest(?!AA)' file.txt (?!AA) is the zero width negative lookahead pattern ensuring that there is no AA after SomeTest. Test : $ grep -P -A 1 'SomeTest(?!AA)' file.txt SomeTestABCD EndTest SomeTestDEFG EndTest SomeTestACDF EndTest


1

POSIXly: <file tr -cs '[[:alnum:].@]' '[\n*]' | grep domain\.com bcd@domain.com cde@adomain.com efg@efg.domain.com


2

You are looking for grep -ho '\S*domain.com\S*' file where \S matches non-whitespace characters, so in this case grep will match everything between whitespaces with string "domain.com" in it. Alternatively if you want to explicitly allow only @ and . in the string (apart of alphanumeric characters obviously): grep -ho ...


2

Just grep with ' BINARY.ear'? grep -F ' BINARY.ear' md5sum.txt | awk '{print $1}' Or awk only solution: awk '{ if ($2 == "BINARY.ear") print $1 }' md5sum.txt


0

Change it like this: cat input | while read p; do { echo -n "$p "; lynx -dump "www.xyz.com/p?=$p"; } | awk '$2~/Hello/{print $1" "$3}' done Before the first pipe, you can group the commands using command grouping in the form { list; }. The advantage is redirections may be applied to the entire command list. The redirected output looks then like this: ...


3

grep -l "Nan" * Or, if you want to start nixing output, grep "NaN" * | grep -v ... I probably should also mention another possible next step, which is grep -l "NaN" * | xargs grep ... as grep needs filenames to work on for a second pass with a new search term.


1

POSIXly, you could do it with sed: sed ' h; # make a copy of the pristine line on the hold space :1 /\[[^]]*<[^]]*>[^]]*\]/{ # found a [...<x>...] g; # retrieve our saved copy and branch off b } s/\[\([^]]*\)\]/\1/g; # remove inner [...]s # and loop if that s command was successful t1 # no [...] left to ...


2

If your [, ] pairs are always matched and you don't criss-cross [...]s with <...>, and your grep supports the -P option (like GNU grep when built with PCRE support), you can do: grep -P '>(?!((?:[^]]|\[(?1)\])*)$)' That is, look for a > that is not followed by only matched [...] pairs. It uses PCREs' (?1) recursive matching mechanism.


0

This pattern with lazy matching (needing -P AFAIU) works for me: grep -P '\[[^\]]*?<.*>.*?\]'


0

I came up with the following solution: grep -P '\[[^.\]]*<.*>[^.\[]*\]' filename In other words we match a pair of [ and ] with a pair of < and > in between with the following constraints: between [] must not be closing and opening bracket accordingly before and after the <> pair "[^.\]]*". inside <> must be at least one character. This ...


1

If you need to use more expressive grep-style file patterns: tar -OT <(tar -tf /path/to/file.tar | grep 'FILE_PATTERN') -xf /path/to/file.tar \ | grep 'CONTENT_PATTERN' -O specifies the output to be stdout, and -T specifies a file containing names to extract, when used in conjunction with -x. If simpler pathname expansion is good enough, you can ...


1

You can do it with egrep like egrep "/[^/]+/" <file> which will find a / then anything that is not a slash at least one time, then another / Of course, this looks like the output of a find command, so you could instead just use the -mindepth 2 option like find . -mindepth 2 -type d


4

On a GNU system, you can use this: sed -i '/^#[[:blank:]]Person/{n;s/#root:[[:blank:]]\+marc/root:\tsomeone@something.tld/;}' file It searches for a line beginning with # Person. Then switches to the next line and replaces #root:<blanks>marc with root:<tab> .... The -i flag edits the file inplace. -i, \+ and \t are GNU extensions. The ...


5

A function would probably be better, yes, but in this case the problem probably is because you are using double quotes around it. The $ variables get expanded before the assignment to the alias. You can use a single quote on the outside instead, or, because you already have single quotes, escape the $ with \: alias findrails_pid="ps aux | grep rails | ...


0

This bash/awk script will emulate some limited cases of grep -C and -m. You should edit it till it matches your needs. #!/bin/bash # emulate grep -m <number> -C <number> <pattern> <file>... with awk. context=0 todo=999999 while [ $# -gt 0 ] do case $1 in -m) shift todo=$1 ;; -C) shift ...


2

Assuming: hour=09 Just use that: grep "\.$hour" file With the single quotes in your example, the variable is not interpreted as variable. Therefore the pattern searches for $hour. Also the dot has to be escaped, else it would match any character.


3

The two options you mentioned are specific to GNU grep. -m 500 -- stop reading after the first 500 matching lines -C 0 -- print zero lines of output context The behavior with -C 0 is already the default behavior, so there's nothing special needed to emulate it on AIX. All you need to worry about is how to limit output to the first 500 matches. One way to ...


1

The man page for fgrep explains -m 500 as meaning "stop after matching 500 lines". The -C 0 shows no lines of context, which is the default. Without being able to see how you're calling fgrep I would suggest that this could be one approximation: fgrep ... | head -500


0

Not pure grep, but with bash: i=0 grep -rl 'pattern' * | while read l ; do i=$(($i+1)) echo $l if [ $i -ge N ] ; then echo "at least N matches" break fi done Will be faster if you have far more files matching than your threshold of N matches.


6

You can pipe grep result to head. Note, that in order to ensure stopping after Nth match, you need to using stdbuf to make sure grep don't buffer its output: stdbuf -oL grep -rl 'pattern' * | head -n10 As soon as head consumed 10 lines, it terminated and grep will receive SIGPIPE because it still output something to pipe while head was gone. This ...


1

Command grep takes two arguments: pattern and file. The file can be omitted if input is connected to stdin with pipe | or redirection <. In you examples this is not the case so the first grep doesn't know where to look for pattern. You would need to write instead: grep -x $idle Event.log | grep -x $dead | grep -x $busy Notice, that second and third ...


0

You'll probably want to quote the regex (to avoid shell word splitting rules), disable options (to avoid leading hyphens in regex from being treated as options to grep), and formally indicate that stdin is being grepped. $ x="hi there"; (echo hi there; echo bye there) | grep -- "$x" - hi there If you only need the result for the test condition, the -q ...


0

Here's how to emulate grep -B1 with sed: sed '$!N;/pattern/P;D' infile It's very similar to the one here. Just like the other one, it reads in the Next line but this time, it Prints up to first \newline if the pattern space matches, and then, just like the other one, Deletes up to first \newline and restarts the cycle. So with your sample input: sed ...


0

awk -F: '{ sub( "[ 0-9]+", "", $1 ) gsub( " +", "", $2 ) cz=split( $2,comp,"," ) for( c=1; c<=cz; c++ ) { if( comp[c] ) { if( ! allcomp[comp[c]] ) fifocomp[++fifoc] = comp[c] allcomp[comp[c]] = allcomp[comp[c]] " " $1 "\n" } } } END { for( c=1; c<=fifoc; c++ ) { ...


0

In Perl (one liner): perl -ne 'print "$1\n" if (m/^(a|"a")$/);' < filename $1 is the text captured in inside parentheses. Output: a "a" As a script you can write like this: #!/usr/bin/perl use strict; use warnings; open my $fh, "<", "abc.txt" or die $!; while (<$fh>) { chomp; print $_, "\n", if (m/^(a|"a")$/); } close $fh;


2

Here's a bit of an ugly "oneliner", that does the job exactly as specified by you in the question. for tag in `cat input |cut -d: -f2 |sed 's/<\/p>//g' |sed '/^[[:space:]]*$/d' |tr "," "\\n" | sed 's/[[:space:]]//g' | sort -u`; do echo "<p>-- $tag --</p>"; grep $tag input | awk '{print $2, $3}' | sed 's/://g' | sed 's/^/ <p>/' | sed ...


1

This should work: egrep '^("?)a\1$' <file See this question for more info: http://stackoverflow.com/questions/31730946/matching-in-matching-space-with-sed-since-when-it-is-supported/31731446


2

With awk: awk '!($1 in a){a[$1];print}' <file or: awk '!a[$1]++' <file


4

sort -k1,1 -u file Sort file by first column and take first unique entry only.


1

Well, assuming that a) the first two parts of the id-field are the key to group two lines together, and b) you may also use perl from the commandline, the following script will parse out the values - fields, and convert it to a html output from a given input-file: #!/usr/bin/perl my $fn=shift; my ($ABC_found,$ASCII,$RPI,$ID)=(0,"","",""); ...


2

Well, it's not that clear, what you actually want to do, but it's clear, that you need to parse those values out of the file of yours. To do this, just put the values in separate arrays, like this (you need to set YourFile with the actual file with the values): H=() V=() while read -r Line; do H+=("$( printf "%s" "$Line" | grep -o 'column=[^,]*' | sed ...


0

grep should suffice your solution grep -o "`grep -of file1 file2`" file3 the inner grep "grep -f file1 file2" will grep the pattern present in file1 and file2 and the pattern returned is searched in file3.


2

grep -of file1 file2|xargs -I {} grep -o "{}" file3 This starts by taking the input of file1 and feeding it in line by line into file2, returning the exact matched text if any. Then the results if any are fed into file3 line by line again returning only matched text.


1

You could use join 2 times on a row: join -1 1 -2 2 -o 1.1 <(join -1 1 -2 2 <(sort file1) <(sort -k2 file2)) <(sort -k2 file3) Prints only: 465 First look at the inner join. It joins file1 and file2 by using the field 1 in file1 and the field 2 in file2. Then all of this is joined again with file3. Notice, the files must be sorted on the ...


1

You could use find with two -execs, the second one will be executed only if the first one is successful, e.g. searching only in .cpp, .c and .cs files: find_code() { find ${SRCDIR} -type f \ \( -name \*.cpp -o -name \*.c -o -name \*.cs \) \ -exec grep -l "= ${1}" {} \; -exec grep -n -C5 "= ${1}" {} \; } so the first grep prints the filenames that contain ...


0

With grep and awk: grep 'document.getElementById("test-summary")' file.html | awk -F'</strong>|&' '{print $2}'


1

Not super elegant, but here you go: sed -ne 's/.*"test-summary".* \([0-9][0-9]* right [^&].*exceptions\)&nbsp.*/\1/p' For example: $ echo '<script>document.getElementById("test-summary").innerHTML = "<strong>Test Pages:</strong> 1 right, 0 wrong, 0 ignored, 0 exceptions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' | sed ...


2

(Extracted answer from edit) I got the result of the search by letting it run overnight. The command I used was: @ubuntu:~/WORKING_DIRECTORY/LC_ALL=C fgrep -rFf bar > ~/myfile Moving the results file, called myfile in the above command, definitely helped. I also sorted the strings in the file. It was originally called strings, and I learned from one ...


-1

The way you would do this is simple grep ford stenton/gen_ed/cars2 | wc -m


3

grep will return success if it finds at least one instance of the pattern and failure if it does not. So you could either add an else clause if you want both "does" and "does not" prints, or you could just negate the if condition to only get failures. An example of each: if grep -q "$user2" /etc/passwd; then echo "User does exist!!" else echo ...


1

The problem with grep is that it needs to have an entire line in memory. If that line is so large that it doesn't fit in memory, than grep bombs. The only way around this dilemma is to feed small chunks to grep. (which is really what grep should be doing itself, anyway, but it doesn't). Use dd so that you can specify an offset to start at, and use fold or ...


4

I would use strings that way : strings 400Gfile.bin | grep -C 10 searchstring To start at a given offset (eg: 20G), dd if=400Gfile.bin bs=20G skip=1 | strings | grep -C 10 searchstring


0

As all the IP addresses start with 192.168. you can do: grep -o '192\.168\.[^ ]\+' file.txt For example : $ grep -o '192\.168\.[^ ]\+' bar.txt 192.168.5.4 192.168.1.2


3

grep -o '192.168.[0-9.]*' datfile | sort -u May not be portable to ancient versions of grep, but -o seems to be in both GNU and *BSD grep, so... (it's also not a perfect match on a IP address, but doing the proper number ranges with regex is super annoying.)


5

I do this with egrep -o or grep -E -o The -E flag in grep activates regex (which is what egrep does by default), and the -o flag prints only the matching string. grep -E -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /path/to/log 192.168.1.11


0

sed -n 's/.*\(192.168.[^ ]*\).*/\1/p' filename should do the trick. \> indicates end of word.


0

Using awk awk -F'|' '{for (i=1; i<=NF;i++) {gsub(/([a-z]|[A-Z])+ /,"",$i); printf "%s",$i; if(i<NF) {printf "|"} else { printf "\n"}}}' foo Example % cat foo cat dog AHF123432 | 123432 | dhfshfjdh lion AFG23412 | 23412 | dfshjhfjdh % awk -F'|' '{for (i=1; i<=NF;i++) {gsub(/([a-z]|[A-Z])+ /,"",$i); printf "%s",$i; if(i<NF) {printf "|"} else ...



Top 50 recent answers are included