Hot answers tagged

29

There are two problems with your example. The primary one is that you're assuming that regular expressions work the same as glob patterns in that * is a wildcard meaning "any sequence of characters." In regular expressions, * means "any number of the previous atom" instead, so fil* means f followed by i followed by zero or more l characters. You need to say ...


13

No, [0-9] is not the same as [:digit:]. [0-9] matches the numerals 0 to 9. [:digit:] matches 0 to 9, and numerals in non-western languages as well (e.g. Eastern Arabic).


12

That's grep issue, not find. grep matches pattern using regular expression by default, the pattern schema_name. means any character follows the string schema_name. If you want to match the dot . literally, you have to escape it with a backslash \: find . -type f -name "*.sql" -exec grep -il 'schema_name\.' {} + or using -F option: find . -type f -name ...


12

You could do this, x | grep --color=never hello To quickly test it, you can do, ls -l /etc/ --color=always | grep --color=never .


9

grep -vxE '([0-9]{5}[,-])*[0-9]{5}' Would report the incorrect lines. Or if you also want to forbid 12345-12345-12345: num='[0-9]{5}' num_or_range="$num(-$num)?" grep -vxE "($num_or_range,)*$num_or_range"


7

This one-liner should be able to do what you want: grep -v ignore input.txt | sed 's/format300/format300\n/g' | grep -c "format300" basically you are replacing each occurrence of your keyword with the keyword itself and a newline character, which effectively makes your input stream have the keyword only once on any given line. Then grep -c is counting ...


6

You don't need the first cat, that it is known as a Useless use of cat (UUOC). Also, very useful is grep -o, that only outputs the matching patterns, one per line. And then, count lines with wc -l. grep -v ignore YOUR_FILE | grep -o format300 | wc -l This prints 3 for your small sample.


6

Pipelines run from left to right. More precisely, the processes run in parallel, but the data flows from left to right: the output of the command on the left becomes the input of the command on the right. Here the command on the left is grep tool. Since you're passing a single argument to grep, it's searching in its standard input. Since you haven't ...


6

You might need a word stemming algorithm for this. For example, Lingua::Stem is a word stemmer module written in Perl. If this fits your needs, you would need to install Lingua::Stem via CPAN. Then, the following Perl script would do the job: #!/usr/bin/perl require Lingua::Stem; # Read lines into array chomp(my @words = <STDIN>); # Stem in ...


6

Perl to the rescue! perl -ne 'BEGIN { $search = shift } print if /^.{41}\Q$search\E$/; ' -- "$myvar" "$filelist" -n reads the file line by line. The BEGIN block retrieves the $myvar from the first argument into Perl variable $search. \Q...\E quotes the inner part (see quotemeta). This handles all the special characters the variable can ...


6

In your first example cat f.txt | grep "someText" grep doesn't get a filename argument, only a string to search for. In that case grep will read the text to search from standard input. In this case that standard input is piped in from the output of cat f.txt, which outputs the content of the file not the filename. What you also could have done to make ...


5

sed operates on stdin, not on its arguments, unless you are giving it filenames. It's easier to specify what you want to remove than what you want to keep, with sed. Instead of sed -n 's/.*tex:/[preventColonFromResult]/p' ./BitTorrentSync/Gyn/1.12.2015.tex: Agents in young <40yr? perhaps you meant printf '%s\n' './BitTorrentSync/Gyn/1.12.2015.tex: ...


4

Just a quick hack: when grep is sending output to a pipe, it also commutes to no-changing-color mode x | grep hello | cat


4

For a good grep solution, see St├ęphane's answer. As an alternative, here's a Perl one: perl -ne 'print if grep{$_!~/^\d{5}$/} split(/[,-]/); ' file That will split each input line on , or - and then will look for members of the split array that don't consist of exactly 5 numbers. If any are found, the line is printed.


4

export myvar awk 'substr($0, 42) == ENVIRON["myvar"]' < "$filelist"


4

There are two main problems here: $( takes standard output from a command, not its exit code; if $bool checks if a command with the name matching the data in $bool returns 0, not if the bool is 0 or 1. That is, if $bool contained "foo", the command "foo" would be executed. Just do the check directly: if whois 100.43.81.149 | grep -q netname:; then ...


4

You could use fgrep find . -type f -name "*.sql" -exec fgrep -i -l 'schema_name.' {} + which on older Unix operating systems may very well be a lot faster (fgrep, grep and egrep used to be 3 different executables, and there fgrep was a lot faster because it omitted everything related to regex entirely - on eg GNU based systems these three programs are ...


3

Assuming glenn jackman's paraphrase of your question is correct, here is a solution using awk and substr(): awk '{key = substr($0,1,4)}; !(key in printed); {printed[key]}' file This sets "key" to the first four chars of a line, then prints the line unless it has seen that key before, then keeps track of the fact that that key has been printed.


3

This command is going to search for foo in the file filename and in the results for bar: grep foo filename | grep bar An alternative would be with awk: awk '/foo/ && /bar/' filename


3

You can use grep in different ways, for instance: cat myfile | grep keyword This above example will dump the file to stdout. The pipe character | will take that output and feed it to grep keyword command as stdin. In the end, the lines in myfile containing keyword will be written to the stdout, which is generally terminal screen. This is a very ...


3

You need perl regular expressions for this. With a grep that supports the -P flag: grep -oP '(?<=a)a' file | wc -l This is a positive lookbehind. It matches a single a which is preceded by another a. If you prefer perl (or your grep doesn't support the -P flag): perl -ne 'while(m/(?<=a)a/g){$a++}END{print "$a\n"}' file Example: $ cat file ...


3

echo -n "$filename|"; tr "\n" "," <"$filename"


3

Awk is good for this sort of thing: awk '$1>=1460333000 && $1 <=1460417100' $1 is the first field.


3

Short answer: ps -u user1,user2,user3 Your grep command is incomplete since the -f option requires an argument. Your cat command is receiving its input from the here document, so even if you fixed the grep command, its output would be discarded. If you were looking for a hard-coded user, you'd use ps -ef | grep alice (except that this isn't a good way ...


3

This is a different way to go; it first sets 'line' to be empty, and sets it if it finds that 'myvar' matches the trailing 3 fields. line= while IFS=' ' read -r hex int1 int2 rest do if [[ "$myvar" = "$int1 $int2 $rest" ]] then line="$hex $int1 $int2 $rest" fi done < filelist Here's yet another way, using bash's mapfile builtin and second ...


3

To be precise [0-9] is only guaranteed to be equivalent to [:digit:] if: the regexp parser supports [:digit:] (i.e. if it does not, then the existing [:digit:] probably doesn't do what you think it does), and: the input character set is one such as ASCII where the only digits are the characters 0 - 9 and they are adjacent. This might not be true in (e.g.) ...


3

You can use this command assuming your data in in the file test uniq -f 2 <test


3

Use: grep -o '\[.*apal' file.txt Replace file.txt with the actual filename. On the other hand, if you want to match [ at the start of the line: grep -o '^\[.*apal' file.txt


3

Using "awk" This will print lines with N0 < LIMIT: # -v sets variables which can be used inside the awk script awk -v LIMIT=10 ' # We initialize two variables which hold the two previous lines # For readability purposes; not strictly necessary in this example BEGIN { line2 = line1 = "" } # Execute the following block if ...


2

You can use either of these, depending on what you're trying to display: $ echo "lol llol" | grep -E "\blol" lol llol $ echo "lol llol" | grep -Eo "\blol" lol Putting the regex in quotes solves your matching problem. The -o flag causes grep to only print the matched string instead of the entire line.



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