Tag Info

Hot answers tagged

11

Generally, you only have to escape one time to make special character considered literal. Sometime you have to do it twice, because your pattern is used by more than one program. Let disscuss your example: man gcc | grep \\. This command is interpreted by two programs, bash interpreter and grep. The first escape causes bash knows \ is literal, so the ...


8

Use the -x option: grep -x -F word file -x will "consider only input lines that use all characters in the line ... to match an entire fixed string".


7

Your grep prints all lines containing non-punctuation characters. That's not the same as printing all lines that do not contain punctuation characters. For the latter, you want the -v switch (print lines that don't match the pattern): grep -v '[[:punct:]]' file.txt If, for some reason you don't want to use the -v switch, you must make sure that the whole ...


7

Using gawk and assuming that the year always ends the record: awk -F"[0-9]{4}$" '{print $1}' movies


7

In Bash, you can use process substitution with tee: tee >(grep XXX > err.log) | grep -v XXX > all.log This will put all lines matching XXX into err.log, and all lines into all.log. >( ... ) creates the process in the parentheses and connects its standard output to a pipe. This works in zsh and other modern shells too. You can also use the pee ...


7

For the stated question you can use find: find . -mindepth 1 ! -type l will list all files and directories in the current directory or any subdirectories that are not symlinks. mindepth 1 is just to skip the . current-directory entry. The meat of it is the combination of -type l, which means "is a symbolic link", and !, which means negate the following ...


6

The standard (POSIX) syntax is: find /path/to/parent -type f -exec grep 'XXX' /dev/null {} + (the /dev/null is to make sure grep always prints a file name). That will work on all POSIX systems including Solaris. The only known post-90s systems where that's known not to work is old (very old now) GNU systems. GNU initially introduced a -print0 predicate ...


6

This is a problem: if [ `grep vm.swappiness /etc/sysctl.conf` != "vm.swappiness=5" ]; then Since if grep doesn't find anything, it doesn't output anything, and the left hand side of this will be nothing, which is an error. It's also a problem if it does find something, since the output could contain whitespace (e.g., if vm.swappiness is in the file ...


6

bash: while read -r line; do if [[ $line =~ (.*)[[:blank:]]+[0-9]{4}$ ]]; then echo "${BASH_REMATCH[1]}" fi done < data sed: sed 's/[[:blank:]]\+[0-9]\{4\}$//' < data


6

From version 2.12 onwards, the -r option for GNU grep doesn’t dereference symbolic links unless you specify them by hand: -r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option. -R, --dereference-recursive ...


5

try using double quotes "": grep -oP "FW_6.0.0, SUCCESS" file OR (Because it is a fixed string, not a pattern): grep -oF "FW_6.0.0, SUCCESS" file from grep man page: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) ...


5

This is really quite simple. As long as the last field, the year, does not contain any whitespace (this is not clear from your question but I am assuming it is the case), all you need to do is remove the last field. For example: $ cat movies Casablanca 1942 Eternal Sunshine of the Spotless Mind 2004 He Died with a Felafel in His Hand ...


5

There's a part you can easily improve, but it isn't the slowest part. find /home/mydir/ -type f | sort | \ awk "/xml_20140207_000016.zip/,/xml_20140207_235938.zip/" This is somewhat wasteful because it first lists all files, then sorts the file names and extracts the interesting ones. The find command has to run to completion before the sorting can ...


4

Don't use grep --color=always, that's precisely why GNU grep (and maybe others) also have grep --color=auto which is equivalent to grep --color alone (from man grep): --color[=WHEN], --colour[=WHEN] Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and ...


4

It could be because your grep is not returning anything. As it isn't quoted, then you get that error message (unary operator expected). Try:- if [ "$(grep vm.swappiness /etc/sysctl.conf)" != "vm.swappiness=5" ]; then


4

Use nmcli's parameters: nmcli -t -f uuid c


4

If you want to use awk: awk '/FW_6\.0\.0/ && /SUCCESS/' file


4

Some quick ideas; If all the files are in a single directory, you can get rid of the find Your file name convention sorts itself by date, so you don't need the sort bit either With this two pieces out of the way, and if the date range is known, you can use a simple filename glob instead of awk. For example (assuming your shell is bash): All files of a ...


4

I think the issue is that the regex consumes the characters that it matches. You may be able to work around that to some extent using zero-length assertions, if your regex engine supports them. For example, if you only need to count the occurrences, you could use a PCRE consisting of a single character followed by a lookahead consisting of (21 - 1) ...


4

The best strategy would be to use a proper html parser that can spit out the value of all <a> tags. Here, xmlstarlet is specifically an XML parser, and your HTML may not be well-formed XML, but you might get the idea: echo '<html> <a href="000000.jpg" title="image name.jpg" target="_blank">Image name.jpg</a> </html>' | ...


4

To copy all lines between %packages and %end from file1 into file2: awk '$1=="%end" {f=0;next} f{print;next} $1=="%packages" {f=1}' file1 >>file2 This solution is designed to remove the lines %packages and %end. (If you want those lines to be transferred as well, there is an even simpler solution below.) Since awk implicitly loops over all lines ...


4

You can leverage the printf builtin. mo1 () { for file in *.txt; do grep -n -C1 "$(printf "%s.*" "$@")" "$file" done } This simple version inserts .* after the last element. It doesn't matter for this specific use case, but in other cases (e.g. grep -o) you may need to strip off the extra .* at the end. mo1 () { pattern=$(printf "%s.*" "$@") ...


3

I assume the movie data will look something like below. cat movies one flew over the cuckoo's nest 1975 taxi driver 1976 the shining 1980 Now, I also assume the years in the movie data will always be 4 characters at the end. So, now if you use the commands as below, awk '{ gsub (" ", "", $0); print}' movies | rev | cut -c 5- | rev ...


3

You could do something like: $ echo 'aabiicaa' | perl -lne ' while (/aa|ii/g) {print substr($`,-3)."[$&]".substr($'\'',0,2)}' [aa]bi aab[ii]ca iic[aa]


3

Maybe: $ nmcli -f NAME,UUID -p c | grep <your network card> Example: $ nmcli -f NAME,UUID -p c | grep p3p1 p3p1 95345734-ff3d-4888-bc8e-77a8a57ab958


3

For what it's worth, this is exactly the reason why --color defaults to --color=auto and not --color=always. If your goal is "Show me all lines that contain both A and m and highlight the matching A and m characters", it seems like the simplest solution would be to do all the highlighting after all of the matching, using one egrep to add the highlighting ...


3

Just for inspirational purposes, needs some tweaking maybe: #assuming there is only one line with vm.swappiness #otherwise you can use the test command with "grep -c vm.swappiness" first #tests if the correct line is in the file if grep -q -E '^vm.swappiness=5$' /etc/sysctl.conf; then echo "all good, do nothing"; else echo "removing possible lines ...


3

This would do it i hope. Events go to events file. And messages go to stdout. Save this file to myprogram.awk (for example): #!/usr/bin/awk -f BEGIN { s=0; ### state. Active when parsing inside an event nevent=0; ### Current event number printf "" > "events" } # Start of event /^ *Data control raising event/ { s=1; dentries=0; ...


3

So, it looks like gradle run doesn't comply with tee, pee, grep and io-redirection. It always stops reading after 4096 bytes. To circumvent this issue, I read each line of gradle run. I didn't test it yet, but I guess that reading a line that is over 4k characters long will also fail. Anyway, here is the code to solve my question specifically: #!/bin/bash ...


3

In zsh, this would be easy thanks to glob qualifiers: grep PATTERN **/*(.) The pattern **/ traverses subdirectories recursively. The glob qualifier . restricts matching to regular files. Without zsh, use find (see Michael Horner's answer). And in this particular case, GNU grep can do what you want (it's exactly what grep -r does) — but only since version ...



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