ack command line ack-set doesn't seem to work - grep

I'm trying to search systemd .service files but ack can't seem to find anything. I tried these options to no avail:
ack --type-set sysd=.service --sysd MAINPID
ack --all MAINPID
ack --unrestricted MAINPID
When using type-set ack reckognises the new type:
# ack -type-set sysd=.service --help types | grep sysd
--[no]sysd .service
Using grep instead of ack finds what I'm looking for, but why isn't ack able to?
# grep MAINPID -R .
./multi-user.target.wants/vixie-cron.service:ExecStop=/bin/kill -TERM $MAINPID
...
ack version used is 1.96.

If ack --unrestricted can't find it, then something's wrong with how you're invoking it, because ack -u is effectively grep.
ack is designed explicitly for searching source code, not as a general-purpose grep replacement. It looks to me that you're not actually searching source code. I would suggest that you use grep for tasks that are not searching source code.

Related

using grep command to get spectfic word [LINUX]

I have a test.txt file with links for example:
google.com?test=
google.com?hello=
and this code
xargs -0 -n1 -a FUZZvul.txt -d '\n' -P 20 -I % curl -ks1L '%/?=DarkLotus' | grep -a 'DarkLotus'
When I type a specific word, such as DarkLotus, in the terminal, it checks the links in the file and it brings me the word which is reflected in the links i provided in the test file
There is no problem here, the problem is that I have many links, and when the result appears in the terminal, I do not know which site reflected the DarkLotus word.
How can i do it?
Try -n option. It shows the line number of file with the matched line.
Best Regards,
Haridas.
I'm not sure what you are up to there, but can you invert it? grep by default prints matching lines. The problem here is you are piping the input from the stdout of the previous commands into grep, and that can lack context at grep. Since you have a file to work with:
$ grep 'DarkLotus' FUZZvul.txt
If your intention is to also follow the link then it might be easier to write a bash script:
#!/bin/bash
for line in `grep 'DarkLotus FUZZvul.txt`
do
link=# extract link from line
echo ${link}
curl -ks1L ${link}
done
Then you could make your script accept user input:
#/bin/bash
word="${0}"
for line in `grep ${word} FUZZvul.txt`
...
and then
$ my_link_getter "DarkLotus"
https://google?somearg=DarkLotus
...
And then you could make the txt file a parameter.
etc.

Pipe psql error output to grep

I'm running a psql query like so:
$ psql --file=foo.sql "BAR-DB"
Where foo.sql contains the query. foo.sql has bad syntax, so the output is a list of error messages. My plan was to pipe the output to grep so I can filter specific error messages, but
$ psql --file=foo.sql "BAR-DB" | grep PATTERN
doesn't seem to do anything. psql's documentation shows the optional flag:
-o, --output=FILENAME send query results to file (or |pipe)
but I'm not sure how to use it to pipe the output to grep. What is the proper syntax?
You could redirect the stderr of psql to stdout and then pipe it to grep:
psql --file=foo.sql "BAR-DB" 2>&1 | grep -- PATTERN

grep: repetition-operator operand invalid

I have this regular express (?<=heads\/)(.*?)(?=\n) and you can see it working here
http://regexr.com?347dm
I need this regex to work in the grep command but I'm getting this error.
$ grep -Eio '(?<=heads\/)(.*?)(?=\n)' text.txt
grep: repetition-operator operand invalid
It works great in ack but I dont have ack on the machine I need to run this on.
ack text.txt -o --match '(?<=heads\/)(.*?)(?=\n)'
text.txt
74f3649af36984e1b784e46502fe318e91d29570 HEAD
06d4463ab47a6246e6bd94dc3b9267d59fc16c2e refs/heads/ARC
0597e13c22b6397a1b260951f9d064f668b26f08 refs/heads/LocationAge
e7e1ed942d15efb387c878b9d0335b37560c8807 refs/heads/feature/311-312-breaking-banner-updates
d0b2632b465702d840a358d0b192198ae505011c refs/heads/gulf-news
509173eafc6792739787787de0d23b0c804d4593 refs/heads/jbb-new-applicationdidfinishlaunching
1e7b03ce75b1a7ba47ff4fb5128bc0bf43a7393b refs/heads/locationdebug
74f3649af36984e1b784e46502fe318e91d29570 refs/heads/master
5d2ede384325877c24db7ba1ba0338dc7b7f84fb refs/heads/mixed-media
3f3b6a81dd3baea8744aec6b95c2fe4aaeb20ea3 refs/heads/post-onezero
4198a43aab2dfe72d7ae9e9e53fbb401fc9dac1f refs/heads/whitelabel
76741013b3b2200de29f53800d51dfd6dc7bac5e refs/tags/r10
fc53b1a05dad3072614fb397a228819a67615b82 refs/tags/r10^{}
afdcfd970c9387f6fda0390ef781c2776aa666c3 refs/tags/r11
grep does not support the (?<=...) or *? or (?=...) operators. See this table.
$ grep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E
If you use GNU grep, you can use -P or --perl-regexp options.
In case you are using OS X, you need to install GNU grep.
$ brew install grep
Due to recent changes, to use GNU grep on macOS you either have to prepend the command with a 'g'
$ ggrep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E
Or change the path name
Try this
grep -Eoh 'heads/.*' text.txt | grep -Eoh '/.*' | grep -Eoh '[a-zA-Z].*'

grepping a not matching pattern with a pattern file and data from a pipe

I have an ignore.txt file:
cat ignore.txt
clint
when I do:
pip freeze | grep -v -f ignore.txt
I get:
GitPython==0.3.2.RC1
Markdown==2.2.1
async==0.6.1
clint==0.3.1
gitdb==0.5.4
legit==0.1.1
push-to-wordpress==0.1
python-wordpress-xmlrpc==2.2
smmap==0.8.2
but when I do:
pip freeze | grep -v clint
I do get the correct output:
GitPython==0.3.2.RC1
Markdown==2.2.1
async==0.6.1
gitdb==0.5.4
legit==0.1.1
push-to-wordpress==0.1
python-wordpress-xmlrpc==2.2
smmap==0.8.2
How can I achieve that with grep and command line tools?
Clarfication Edit: I use windows with cygwin so I believe this is GNU grep 2.6.3 (from grep --version)
Your syntax looks correct and works on my system.
There may be a problem with your ignore.txt file.
In particular, check that:
there are no leading or trailing spaces, tabs and the like around the word you are trying to filter (as suggested by Kent above)
the file has Unix line endings
the file is terminated by a single newline
About the latter, the Single Unix Specification says:
Patterns in pattern_file shall be terminated by a <newline>.
Which means that a file with no terminator, or with a different terminator (e.g. CR LF), might behave unexpectedly (though that might be system-dependent).

ack is not doing a recursive grep

ack (the grep tool written in Perl) does not find a file that grep -r finds, and I cannot find the right options to get it to work. The following shows ack did not find the target string, which is in a regular file in a sub-directory. It's on Bash shell (Ubuntu 11.04):
100 $ grep -r imbue *
hel/find: the fact that some shells including Bash imbue braces
## Note: grep find it as shown in the above.
101 $ ./ack-standalone imbue
## Note: ack didn't find it as shown in the above.
102 $ ./ack-standalone --version
ack 1.96
Running under Perl 5.10.1 at /usr/bin/perl
Copyright 2005-2011 Andy Lester.
This program is free software. You may modify or distribute it
under the terms of the Artistic License v2.0.
## This is the testing folder structure:
103 $ tree
.
ack-standalone
hel
|- dot
|- find
|- grep
|- jobs
perlman
perlre
perlrequick
perlrun
perlvar
xargs
1 directory, 11 files
Version 2 of ack, from apt-get package installation, got same results. In the stand-alone version (version 1) shown above. ack -f shows nothing, and I tried the -r and * options, all with the same results.
On another machine, Ubuntu 10.04, it works like a charm.
It works for me if I select to operate in all files regardless of its type, using -a switch (my version is same that yours):
ack -a imbue *

Resources