Tag Info

New answers tagged

1

You need something like this: { head -n1 S_313_IPS_S7995.coverage.sample_interval_summary; grep "chr1" S_313_IPS_S7995.coverage.sample_interval_summary; } >S_313_IPS_S7995.chr1.coverage or awk 'NR==1 || /chr1/' S_313_IPS_S7995.coverage.sample_interval_summary >S_313_IPS_S7995.chr1.coverage The problem is that the redirection affects only one ...


2

As a non-regex alternative, use pup: pup 'a[href] attr{href}' < yourfile.html Will find all a elements that have a href attribute, then display the value of the href attribute. To install pup, you need Go (a programming language): sudo apt-get install golang sudo go get github.com/ericchiang/pup The advantage of this solution is that it doesn't ...


0

wget -qO- google.com | tr \" \\n | grep https\*:// ...would probably do pretty well. As written, it prints: http://schema.org/WebPage http://www.google.com/imghp?hl=en&tab=wi http://maps.google.com/maps?hl=en&tab=wl https://play.google.com/?hl=en&tab=w8 http://www.youtube.com/?tab=w1 http://news.google.com/nwshp?hl=en&tab=wn ...


2

try ls -1 | grep -E '^[A-Z]?([a-z][A-Z])*[a-z]?$' EDIT: as pointed very correctly by @mikeserv, this won't work for non-ASCII chars. And indeed they do happen quite often (e.g. music files with foreign titles for filename). So the more robust way is: ls -1 | grep -E '^[[:upper:]]?([[:lower:]][[:upper:]])*[[:lower:]]?$' In the following, I retain just ...


5

As I said in my comment, it's generally not a good idea to parse HTML with Regular Expressions, but you can sometimes get away with it if the HTML you're parsing is well-behaved. In order to only get URLs that are in the href attribute of <a> elements, I find it easiest to do it in multiple stages. From your comments, it looks like you only want the ...


2

Not sure if you are limited on tools: But regex might not be the best way to go as mentioned, but here is an example that I put together: cat urls.html | grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" | sort | uniq grep -E : is the same as egrep grep -o : only outputs what has been grepped (http|https) : is an either / or a-z : is all lower case A-Z : is ...


4

If your grep supports Perl regexes: grep -Po '(?<=href=")[^"]*(?=")' (?<=href=") and (?=") are lookaround expressions for the href attribute. This needs the -P option. -o prints the matching text. For example: $ curl -sL https://www.google.com | grep -Po '(?<=href=")[^"]*(?=")' /search? https://www.google.co.in/imghp?hl=en&tab=wi ...


2

set aAbBaBbA bAbBaA bbAb AAaBbAa BBBaaa aBaB l= u=; printf %s\\n "$@" | grep -E "^([${l:=[:lower:]}][${u:=[:upper:]}])+[$l]?$|^([$u][$l])+[$u]?$" OUTPUT: aAbBaBbA bAbBaA aBaB But see Pierre's answer for the (obviously) better version of this. Still, there is another way: grep -vE '[^[:alpha:]]|[[:lower:]]{2}|[[:upper:]]{2}|^$' ...though ...


0

The solution is grep -E '^([a-z][A-Z])+$'


2

Type this command: ls |grep -E '^([[:upper:]][[:lower:]])+[[:upper:]]?$|^([[:lower:]][[:upper:]])+[[:lower:]]?$' To break this down. ls is the main command It is piped to grep The switch -P or -E are telling grep to search using regular expressions (regex) Within the single quote ' and the parentheses () the pattern is layed out. Basically it reads ...


3

Compare also: echo '.*[s]' file with echo .*[s] file This outputs the arguments as seen by the command. In your first example you pass your grep command exactly two arguments: the pattern and the file. In your second example your shell will handle the first argument and replace it with all the files starting with a dot and ending in "s". Therefore ...


4

Quotes (either single or double) around an argument inhibit glob expansion. Your first example passes a Regular Expression as an argument to grep. Your second example contains a glob pattern which the shell itself expands, passing filenames that fit that pattern as arguments to grep.


2

Grep exits by default with 0 on match and 1 on no match. As such you could do: grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log The -q suppress any output. To test say something like: grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log && echo OK || echo BAD If the $1 is actually replaced with a ...


1

grep is the way to go, it returns 0 if a match is found. You don't actually need to output the line, so just discard the line and use the test. In your case, it would just be lastline=$(tail -n1 logfile) if grep pattern <<<"$lastline" &>/dev/null; then echo "yay, found pattern" else echo "darn" fi Observe the "here string" ...


1

If your data is very large, then grep is likely to be faster than more flexible tools such as awk. What I'd do is to quote the special characters in the text and call grep. pattern=$(printf '%s\n' "$literal_text" | sed 's/[\[.*^$]/\\&/g') grep "^$pattern" my-big-file If the text only contains ASCII characters, set the character set to C, so that grep ...


4

With GNU grep if built with PCRE support and assuming $string doesn't contain \E, you can do: grep -P "^\Q$string"


2

This solution uses awk. We'll pass both filenames as arguments, and use the if (FNR == NR) idiom to do something different depending on whether we're reading the first or second file. We'll use associative arrays to store the keys and the output lines. This is the file a.awk: # usage: awk -f a.awk keyfile1 datafile2 BEGIN { FS = "\t" ...


2

with awk awk -vword='miss.' 'index($0, word) == 1' file For multiple words awk 'BEGIN{for (i=2; i<ARGC; ++i)word[++j]=ARGV[i]; ARGC=2} {for (i=1; i<=j; ++i)if (index($0, word[i]) == 1){print; continue}}' file \ word1 word2 word3 I also fancy python for this python3 -c 'import sys words = tuple(sys.argv[1:]) for line in sys.stdin: ...


0

If you don't have too many patterns: perl -ne 'BEGIN {$exp = quotemeta("miss.")} print if /^$exp/' I don't know how to extend this efficiently to grep -f (read patterns from file, which you'd do if you had a serious number of patterns to deal with), but this SO answer on joining queries seems to be interesting.


3

First, you need to find out whether the messages you want are getting sent to stdout or stderr. In your first example, you've got stderr getting redirected to stdout - that's the "2>&1" part. You also have "/dev/null" on the command line, which doesn't entirely make sense. Why does "/dev/null" appear on command line? Second, you need to decide what ...


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


1

You need to replace the newlines in the variable with \newline. And the text to be inserted needs to be preceded by \newline. var=$(<file.txt) # Put backslash before newlines in $var var=${var// /\\ } printf "s1random stuff\ns2 more random stuff\ns1 final random stuff\n" | sed "/^s2/a \ $var"


0

Are you trying to do something like this?: LINE1=`cat test.file | sed '1!d'` LINE2=`cat test.file | sed '2!d'` LINE3=`cat test.file | sed '3!d'` LINE4=`cat test.file | sed '4!d'` echo $LINE1 echo $LINE2


3

The pattern argument to grep is in fact a newline-separated list of patterns. Thus grep $'\r\n' searches either a CR or the empty pattern (which matches every line). This is the same as grep $'\n' which searches the empty pattern or the empty pattern. To search for a CRLF sequence, search for a CR at the end of the line. grep -c $'\r$' file


2

A BRE doing what you're trying to in sed might look like: sed 's/ *\(\([^ ]*\) *\)\{[num]\}.*/\2/' ...or as an ERE for those seds which support it such as GNU and BSD versions: sed -E 's/ *(([^ ]*) *){[num]}.*/\2/p' ...either expression will begin its match at the first character of the [num]th group (where [num] is a positive integer) of [^ ...


1

netstat itself does not support such filtering. You probably have to do something like: sudo netstat -lp --inet | grep " $pid/"


2

In short: \K causes grep to keep everything prior to the \K and not include it in the match. It does not affect what comes after the \K(). This might be enough: " \K(.+)(?= )" Where (?= ) is a non capturing group. or perhaps better: " \K([^ ]+)(?= )" " \K(\w+)(?= )" or similar.


23

less has very powerful pattern matching. From the man page: &pattern Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in ...


0

you could set it to a variable if you didn't want to create a file. in bash; test=`ps -ef` && echo -e "$test" | grep fnord


0

IMO, grep -r works best. for instance, you want to search for the number 7546899 in a bunch of files: grep -r 7546899 it will show something like: <filename where is found the >:<something something>7546899<moretext>


1

From the GNU grep man page: -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.) -v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) so grep -f ...


3

awk might be more up to the task awk '{sep=$3 in a?",":"";b[$3]++; a[$3]=a[$3]sep$1}; END{for (k in a) print k, b[k], a[k]}' file


4

This answer is more suitable for a comment but it would be too long so I'm posting it as an answer. grep behavior piqued my interest and I think that OP is on the right track, I also found that newer versions of grep behave differently than older ones. In older versions if you didn't pass a parameter when using -r it only searched for a pattern in standard ...


1

You can also do a for but Orion's example is the simplest: for i in $(cat files.txt); do grep -i 'foo' $i ; done (For each file listed in the files.txt execute the grep command on it.)


1

Here's a nice little page on shellhacks.com that shows examples "Using BASH "Grep OR / Grep AND / Grep NOT" Operators", and it has the sed & awk equivalents. I'll paste it here, you could use them to search for $subject and/or/not $example, in a certain or any order. Using BASH "Grep OR / Grep AND / Grep NOT" Operators In the following article you'll ...


2

Using some logic operators (Starting from @jimmij) grep -q "$finding" information.txt behaves like a boolean answer (prints nothing just has a return status of 0 if found, non-zero otherwise). grep -F -- "$finding" ... search text instead of regexp (remove it if you want to search for regexp -- there may be some security risks... (thank you ...


2

xargs can read items from a file (like your files.txt list) with it's option: --arg-file=file -a file Read items from file instead of standard input. If you use this option, stdin remains unchanged when commands are run. Other‐ wise, stdin is redirected from /dev/null. So this should work too: xargs -a files.txt ...


0

The parts you do not want have a slash and three characters. The part that you want to keep also starts with a slash and have more than three characters, but the third character is an underscore, so we delete all the parts that look like /XXX but not /XX_ This leaves the leading slash on the part that we want to keep, so we finally also delete that one ...


4

To read a list of file names from stdin you can use xargs. E.g., cat files.txt | xargs -d'\n' grep -i -- 'foo' By default, xargs reads items from the standard input, delimited by blanks. The -d'\n' tells it to use newline as the argument delimiter, so it can handle file names containing blanks. (As Stéphane Chazelas points out, that's a GNU extension). ...


5

xargs grep -i -- foo /dev/null < files.txt assuming files are blank or newline delimited (where quotes or backslashes can be used to escape those separators). With GNU xargs you can specify the delimiter with -d (which then disables the quoting handling though). (unset IFS; set -f; grep -i -- foo $(cat files.txt)) assuming files are space, tab or ...


3

You seem to be grepping the list of filenames, not the files themselves. <(cat files.txt) just lists the files. Try <(cat $(cat files.txt)) to actually concatenate them and search them as a single stream, or grep -i 'foo' $(cat files.txt) to give grep all the files. However, if there are too many files on the list, you may have problems with number ...


0

sed 's|.*/\([^/]*_[^/]*\)/.*|\1| ' <<\INPUT /ABC/RTE/AD_900_VOP_123/OPP /ABC/RTE/TRE/AD_900_VOP_145/BBB /ABC/RTE/AN_900_VFP_124/FBF /ABC/RTE/HD_900_FOP_153/WEW /ABD/RDV/AD_900_VOP_123/OPP /ABC/RTE/WD_900_VOP_123/GRR/TRD /ABC/RTE/RTD/AR_900_VOP_443/SDD INPUT That will remove up to the second to last occurrence of / immediately preceding a _ ...


1

IMHO Perl offers the easiest and the most flexible solution: perl -nE 'say $1 if m{/(\w+\d+\w+\d+)/};' input_file Please note that input_file is optional: STDIN will be filtered if/when input file name is not given.


0

i=0 until [ "$((i+=1))" -gt 5 ] || { printf '\nPrompt: ' read finding && [ -n "$finding" ] };do printf '\nINVALID:\tTry again.\n' done <>/dev/tty >&0 grep -- "${finding:?INSUFFICIENT INPUT!}" <info.txt || ! printf '%s:\tNot found in database.' "$finding" >&2 You should be testing not only that $finding is ...


3

Add space after [ (it is a command) Use -n to test if length of string is nonzero, or -z to test if it is zero Put double quotes around variables So: read finding if [ -z "$finding" ]; then echo "You didn't enter anything" else grep "$finding" information.txt if [ ! "$?" -eq 0 ]; then echo "No such information in database." fi fi ...


0

When working w/ sed I typically find it easiest to consistently narrow my possible outcome. This is why I sometimes lean on the !negation operator. It is very often more simple to prune uninteresting input away than it is to pointedly select the interesting kind - at least, this is my opinion on the matter. I find this method more inline with sed's default ...


1

Here is an awk solution: awk '10<=NR && NR<=15 && /pattern/ {print NR,$0}' file If we must use only sed, then consider: sed -n '10,15 {/pattern/ {=;p}}' file | sed 'N;s/\n/ /' sed's = command will only print the line number on a separate line. The second instance of sed above is used to combine every two lines so that the line ...


1

Using nl: nl file | sed -n '10,15{/pattern/p }'


9

With sed you can use a range and quit input at a single completion: sed '/^182$/p;//,/^ABC$/!d;/^ABC$/!d;q' Similarly w/ GNU grep you can split the input between two greps: { grep -n -m1 '^182$'; grep -n -m1 '^ABC$'; } <<\IN 123 XXY 214 ABC 182 558 ABC 856 ABC IN ... which prints... 5:182 2:ABC ... to show that the first grep found a ^182$ ...


0

Sticking with just grep and adding tail & cut, you could... grep for the line number of the first match of 182: grep -m 1 -n 182 /var/log/file |cut -f1 -d: Use that to grep for all the ABC's only after the first matching line above, using tail's -n +K to output after the K'th line. All together: tail -n +$(grep -m 1 -n 182 /var/log/file |cut -f1 ...



Top 50 recent answers are included