New answers tagged

3

The output of tr is buffered. You can use stdbuf -o0 with tr to make it's STDOUT unbuffered: cdrecord -v ... | stdbuf -o0 tr '\r' '\n' | grep -i written


1

An awk solution: awk '$0!~/.*[[:alpha:]][[:digit:]]+$/ && $0!~/^[[:digit:]]+[[:alpha:]]+/' words.txt 789 hello he11o 88888


1

To actually edit the source file and create a new file with the discards is a bit trickier. I would do this $ cat file 789 hello 1hello 112121hello3323 he11o hello9 88888 $ perl -i -lne 'if (/^\d+\D|\D\d+$/) {warn "$_\n"} else {print}' file 2>file_nums $ cat file 789 hello he11o 88888 $ cat file_nums 1hello 112121hello3323 hello9 The matched lines ...


2

GNU grep grep -vP '^\d+\D|\D\d+$' produces 789 hello he11o 88888


2

Your command is right, just add to it the filename grep "Ju" file.txt | grep -v "w" Answering your comment, grep doesn't change files.


2

You can use: grep '^[^w]*Ju[^w]*$' file.txt ^ and $ denote start and end of the line respectively [^w]* matches everything excepts character w So the above Regex necessary matches Ju in the line and make sure that the line does not contain w ([^w]*).


2

The pattern should be specific, to avoid deleting lines containing a '#': sudo sed -r -i 's/^[[:space:]]*#.*$//' /etc/samba/smb.conf The question did not actually ask to delete blank lines; this command leaves them (as well as blanked lines resulting from the substitution). Like the other solutions, this relies on the -i option, which is non-standard ...


2

sed is one option that can "directly" edit a file (by making a temporary copy of it). This command will tell sed to edit /etc/samba/smb.conf -i in place, and to -n not print lines by default. sudo sed -i -n '/^[^#]/ p' /etc/samba/smb.conf The lines we tell it to 'p' print are the ones that match your grep regex, which I modified only slightly to remove ...


1

sed -i -e 's/#.*$//' -e '/^$/d' inputFile in addition to removing all comments, it removes empty lines as well. I know you asked it with grep, but I thought I'd suggest this one. Functionally it is the same or even better by removing empty lines, compacting the size of the file, which I am assuming is your target. EDIT: yes replace # with ; and you should ...


0

grep -ozP "(?s)(abc)[^(abc)]*(mno)" 1 abc yyy mno abc xxx mno


1

Just in case perl is an option for you: #!/usr/bin/env perl # saved lines to print out my @out = (); # should we save lines? my $saving = 0; while (<>) { if (/abc/) { if ($saving) { # this is the second /abc/, so dump what we were saving and start over @out = ($_); } else { # this is the first /abc/, so save it and start ...


1

Make grep do the work of strings. If you have GNU grep, pass the -z option to make it read null-delimited records instead of newline-delimited records. This will also match at the end of the file, but that should be ok in practice. find . -maxdepth 1 -type f -size +1M -print0 | xargs -0 grep -Eoz '[[:print:]]{3,}$' If you don't have GNU utilities, pass ...


1

One way to go about this is to reverse the file using tac, find matches starting with mno and ending with abc, and reverse that to get the desired result. I got the following to work: $ tac test.txt | pcregrep -M 'mno(\n|.)*?abc' | tac abc yyy mno abc xxx mno (I'm using pcregrep for the multiline -M flag)


1

You just could use grep to do this. The second grep is to filter files with no matches. grep -rc 'Author' ${1} | grep -v ':0$'


0

Loop over the files: for file in "$1"/* ; do if [[ -f "$file ]] ; then printf %s: "$file" grep -o Author "$file" | wc -w fi done You can also use Perl: perl -lne 'BEGIN { $c = 0 } $c++ while /Author/g; if (eof) { print "$ARGV: $c"; $c=0 } ' "$1"/*


0

You should use wc -l for that, i.e. grep shell test.txt | wc -l. That returns 3.


5

since you can have the word "shell" multiple times on a line I would start with breaking up the text in single words per line and then do the grep < test.txt tr -s "[[:blank:]]" "\n" | grep "shell" | wc -w you can also use wc -l, or do away with wc and use grep -c "shell" And you can even remove the need for tr on the file that you have and use: ...


16

The command 'grep' is outputting the entire lines that "shell" appear on. Not just the word "shell." As can be seen below: grep shell test.txt for shell_A shell_B shell_C I would recomend using the option -o, --only-matching So: grep -o "shell" test.txt | wc -w


18

The wc command is counting the words in the output from grep, which includes "for": > grep shell test.txt for shell_A shell_B shell_C So there really are 4 words. If you only want to count the number of lines that contain a particular word in a file, you can use the -c option of grep, e.g., grep -c shell test.txt Neither of those actually count ...


0

you can run ps -ef | grep ora_pmon | grep -v grep and it will give you the process listings that you need. You can simplify this output by using very basic sed or awk commands, according to your needs.


4

Try grep: grep -iv dog inputfile -i to ignore case and -v to invert the matches. If you want to use sed you can do: sed '/[dD][oO][gG]/d' inputfile In sed, there is also the I flag, which should make the match case insensitive, but as far as I remember this does not work in all flavors of sed. For me, this works: sed '/dog/Id' inputfile but it ...


0

You could use grep and cut: ~$ mount | egrep -o 'addr=[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d '=' -f 2 12.156.0.212 112.166.60.12 123.10.10.12


2

I created a file called json that looks like this: [{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"},{"product":"Pear","id":"1111"},{"product":"Banana","id":"2222"}] Then I run this on the command line: cat json | sed -e 's/.\?{"product"\:\"\([^\"]\+\)\","id"\:\"\([[:digit:]]\+\)[^}]}\+.\?/Enter Product : \1\nYour Product id is : ...


1

sed is clumsy, but here is what I could get - sed 's/.*\(Apple\)[^:]*:"\([0-9]*\)".*/Enter product ID: \1\nYour product ID is: \2/' 3 Enter product ID: Apple Your product ID is: 2134 sed 's/.*\(Mango\)[^:]*:"\([0-9]*\)".*/Enter product ID: \1\nYour product ID is: \2/' 3 Enter product ID: Mango Your product ID is: 4567 Edited based on latest input ...


2

Use a json parser, not sed/grep/awk. Using Python json module: #!/usr/bin/env python2 import json with open('file.json') as f: f_json = json.load(f) print 'Enter product : ' + f_json[0]['product'] + '\nYour product id is : ' + f_json[0]['id'] Output: Enter product : Apple Your product id is : 2134


6

Use a JSON aware tool. Perl has the JSON library: #!/usr/bin/perl use warnings; use strict; use JSON; my $json = '[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]'; print 'Enter product: '; chomp( my $product = <> ); print 'Your product id is: ', (grep $_->{product} eq 'Apple', @{ from_json($json) })[0]{id}, "\n";


0

has_all_iregexps() { awk ' BEGIN { if (ARGC <= 1) exit for (i = 1; i < ARGC; i++) s[tolower(ARGV[i])] n = ARGC - 1 ARGC = 1 } { for (i in s) if (tolower($0) ~ i) { delete s[i]; if (!--n) exit } } END { if (n) { print "Those regexps were not matched:" for (i in s) ...


2

Just use perl and slurp the file whole: nginx -V 2>&1 | perl -0ne 'print "found\n" if m#nginx/1.9.10# && /ngx_pagespeed-release-1.9.32.10/ && /openssl-1.0.2f/ && /modsecurity-2.9.0/' Also, note that you have some hidden characters in the text of your question. I don't know if those are also there in your actual ...


1

output="$(grep SEARCHSTRING /logs/error_log)" test -n "$output" && echo "$output" | mailx -s subject name@emailaddress.co.uk


2

You can run mailx if the grep command returns success i.e. match is found: body="$(grep SEARCHSTRING /logs/error_log)" && echo "$body" | mailx -s subject name@emailaddress.co.uk Saving the output of grep (if any) to variable body, if the grep command succeeds then mailx will be run with $body as the body of the mail.


1

It seems you want the lines not having /0/ on the last column, you can do: grep -v '[^[:blank:]]*/0/[^[:blank:]]*$' file.txt Example: % grep -v '[^[:blank:]]*/0/[^[:blank:]]*$' file Row2: column1, column2, column3...column10, ht*p://narph0.net/page/328/narph.htm Row4: column1, column2, column3...column10, ht*p://www.the.com/thethat/que303/yeah/main.php ...


0

As mentioned, you should use the -f option to grep, and provide a list of patterns for grep to use. However, you also mention having special characters in your URLs, which makes sense. The correct answer is to use the -F flag to grep to only consider the patterns as fixed strings. So to accomplish what you want: First, put your list of safe websites in a ...


0

Another option you may consider is using the egrep form of grep, which will allow you to use Extended regular expressions so you can put multiple targets in a single string thusly: egrep -v "safesite1\.com|safesite2\.com|safesite3\.com" Details of these and other extended REs can be had from man 7 re_format.


2

Work with a little modification: [ $(nginx -V 2>&1 | grep -cFf <( echo 'nginx/1.9.10 ngx_pagespeed-release-1.9.32.10 openssl-1.0.2f modsecurity-2.9.0' )) -eq 4 ] && echo "has the stuff we need" || echo "missing something"


-1

It turns out my problem had to do with the data I was trying to parse. If I tried using the test output a b c d e and then using grep -vf file.txt to remove a, b, and c it worked like a charm. Since I was instead trying to ignore a bunch of websites with a variety of special characters, it never worked for me, even when trying to manipulate an outputted ...


2

awk '/openssl-1.0.2f/ {test1=1} /nginx\/1.9.10/ {test2=1} END { if (test1 && test2) print "has the stuff we need"; else print "missing something"}' You can also set the exit code of awk if you need that. update shorter version (treats input with line breaks as a single "line" assuming the input does contain 0x1 characters) awk -v RS='\1' ...


0

Rather than applying grep to the output of ls which is not post-processable reliably anyway, I'd rather filter on the list of files passes to ls, like: ls -lFhd --color -- .*(/) In zsh. Or as your own approach suggests you only want the directories last modified between 6 months ago and now and not the ones with special permissions: ls -lFhd --color -- ...


6

Parsing ls is often a bad idea. Often, but not always. Here's another suggestion for you, which collects the required directories together before passing the set to ls. find .* -maxdepth 0 -type d \( -name '.[^.]' -o -name '.??*' \) -exec ls -ld --color=always {} + It's been pointed out that the original code actually limits the list of directories to ...


0

Ok I finally figured out how to list all hidden dirs while preserving the Colors and not including dirs like "hello.world": ls -lhAF1 --color | grep -E "^d[rwx-]{9}.*[0-9]{2}:[0-9]{2} "$'\x1b'"\[([0-9]{1,2}m?)(;[0-9]{1,2}m?)?\."


2

The reason is that ls always colorizes its output even if it is connected to a terminal. From man ls: --color[=WHEN] colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below Many other tools such as grep do not retain colors when standard output is terminal but for some reasons ls was ...


6

--color adds escape sequences for the color. You can see this if you redirect the output (of ls --color) to a file. This is what it looks like: drwxr-xr-x 6 root root 4.0K Jan 9 08:23 ^[[01;34m.cabal^[[0m/ To account for this, try this instead: ls -lhAF1 --color | grep -E '^d.*[0-9]{2}:[0-9]{2} .*\.'


1

I think what you are looking for is something like grep -vf safe_websites inputfile -v to invert the matches you already know, and -f is to obtain patterns from the file safe_websites.


1

Pythonic way: >>> with open("/home/xieerqi/textfile.txt") as file: ... for line in file: ... if line.__contains__("foo"): ... VAR = line ... if line.__contains__("relevant=yes"): ... print VAR ... foo.bar.1 foo.bar.4 Put together in a script: DIR:/xieerqi skolodya@ubuntu:$ chmod +x relevance.py ...


3

Here's one way with sed: sed '/foo\.bar/h;/relevant=yes/!d;x;/foo\.bar/!d' infile Lines matching foobar are copied to hold space. All lines, except those matching relevant=yes are deleted. Exchange hold space with pattern space (this only happens when lines match relevant=yes) and delete if it doesn't match foobar.


5

If the input is processed line by line, then processing needs to go like this: if the current line is foo.bar, store it, forgetting any previous foo.bar line that wasn't enabled for output; if the current line is relevant=yes, this enables the latest foo.bar for output. This kind of reasoning is a job for awk. (It can also be done in sed if you like ...


5

That result you're getting is because . matches any single character From manpage >REGULAR EXPRESSIONS:    The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any meta-character with ...


0

PARAM=b grep -E "nir.*${PARAM}" txt or grep -E 'nir.*'"${PARAM}" txt


0

You were close. If you need the whole line: iptables -S | grep -- "-P" | grep -E "ACCEPT|DROP|REJECT" Output -P INPUT ACCEPT -P OUTPUT DROP -P FORWARD REJECT If you need only the matching words: iptables -S | grep -- "-P" | grep -oE "ACCEPT|DROP|REJECT" Output ACCEPT DROP REJECT


1

There is no reason to invoke grep if awk is involved anyway: iptables -S | awk '/^-P INPUT/ {INPUT=$3}; /^-P FORWARD/ {FORWARD=$3}; /^-P OUTPUT/ {OUTPUT=$3}; END {print INPUT; print FORWARD; print OUTPUT;}'



Top 50 recent answers are included