How can I send to grep the search patterns through xargs after using awk? [duplicate] - linux

This question already has answers here:
Using output of awk to run command
(3 answers)
Closed 1 year ago.
How can I send to grep the search patterns through xargs after using awk?
awk -F”:” '($5 < 400) {print $1}' file1.txt | xargs grep file2.txt
The output I’m getting is this one:
No such file or directory
Thank you for your help.

Hi you can use the following command to pass the awk results as arguments for grep to search them in the second file
awk -F: '($5 < 400) {print $1}' file1.txt | xargs -I {} -0 grep "{}" file2.txt
Let me know if this help you Best regards

Related

Bash - Set grep -rl to return only file name , not full path [duplicate]

This question already has answers here:
How to get only filenames without Path by using grep
(3 answers)
Closed 6 years ago.
I'm runing a grep to find recursively filenames which files content contain a string.
grep -rl string-to-find $pwd
The command returns results as expeted, but with file name and path:
var/log/httpd/access.log
var/log/httpd/access.log.1
How could I set it return only file name not full path?
I would like to get back as result:
access.log
access.log.1
grep has no such flag. But you can pipe its output to a simple awk to get your desired output:
grep -rl string-to-find $pwd | awk -F/ '{ print $NF }'
The -F/ is to set the field separator to /,
and print $NF means to print the last field.

How to remove "-" and a space from the beginning in a bash script? [duplicate]

This question already has answers here:
Editing/Replacing content in multiple files in Unix AIX without opening it
(2 answers)
Closed 6 years ago.
I have an output that looks as below
- 0.1-1
- 0.1-2
- 0.1-3
- 0.1-6
- 0.1-7
- 0.1-9
How to use grep or something else so as to remove "-" and a space from the beginning.
0.1-1
0.1-2
0.1-3
0.1-6
0.1-7
0.1-9
With sed:
sed -e 's/^- //' input.txt
Or with GNU grep:
grep -oP '^- \K.*' input.txt
You may use grep also,
grep -oE '[0-9].*' file
With awk:
awk '{print $2}' file
You can use cut to remove the first two columns of every line:
cut -c3- input.txt

Sum out of grep -c

I am trying to find the number an even occured in my log file.
Command:
grep -Eo "2016-08-30" applciationLog.log* -c
Output:
applciationLog.log.1:0
applciationLog.log.2:0
applciationLog.log.3:0
applciationLog.log.4:0
applciationLog.log.5:7684
applciationLog.log.6:9142
applciationLog.log.7:8699
applciationLog.log.8:0
What I actually need is sum of all these values 7684 + 9142 + 8699 = 25525. Any suggestion I can do it? Anything I can append to the grep to enable it.
Any help or pointers are welcome and appreciated.
If you want to keep your grep command, pipe its output to awk, the quick and dirty way is down here:
grep -Eo "aaa" -c aaa.txt bbb.txt -c | awk 'BEGIN {cnt=0;FS=":"}; {cnt+=$2;}; END {print cnt;}'
Or use use awk regex directly:
awk 'BEGIN {cnt=0}; {if(/aaa/) {cnt+=1;}}; END {print cnt;}' aaa.txt bbb.txt
As addition to the already given answer by ghoti:
You can avoid awk -F: by using grep -h:
grep -c -h -F "2016-08-30" applicationLog.log* | awk '{n+=$0} END {print n}'
This means no filenames and only the counts are printed by grep and we can use the first field for the addition in awk.
See if this works for you:
grep -Eo "2016-08-30" applciationLog.log* -c | awk -F':' 'BEGIN {sum = 0;} {sum += $2;} END {print sum;}'
We use awk to split each line up with a delimeter of :, sum up the numbers for each line, and print the result at the end.
The grep command doesn't do arithmetic, it just finds lines that match regular expressions.
To count the output you already have, I'd use awk.
grep -c -F "2016-08-30" applciationLog.log* | awk -F: '{n+=$2} END {print n}'
Note that your grep options didn't make sense -- -E tells the command to use Extended regular expressions, but you're just looking for a fixed string (the date). So I swapped in the -F option instead. And -o tells grep to print the matched text, which you've overridden with -c, so I dropped it.
An alternative using for-loop and arithmetic expansion could be:
x=0
for i in $(grep -hc "2016-08-30" applciationLog.log*);do
x=$((x+i))
done
echo "$x"
An easy alternative is to merge all the files before grep sees them:
cat applciationLog.log* | grep -Eo "2016-08-30" -c
In my directory have have hundreds of files, each file contains lot of text along with a lines similar to this-
Job_1-Run.log:[08/27/20 01:28:40] Total Jobs Cancelled for Job_1_set0 = 10
I do
grep '^Total Jobs Cancelled' ./*
to get that above line.
Then I do a pipe
| awk 'BEGIN {cnt=0;FS="="}; {cnt+=$2;}; END {print cnt;}'
so my final command is-
grep '^Total Jobs Cancelled' ./* | awk 'BEGIN {cnt=0;FS="="}; {cnt+=$2;};END {print cnt;}'
and result is the sum. e.g. -
900
I am using Cmder # https://cmder.net/
Thanks to the answer by #alagner, #john above

How to grep for specific pattern in a file [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 8 years ago.
I have a bash file that has below line along with other lines.
var BUILD_VERSION = '2014.17.10_23';
I just want to extract 2014.17.10_23 and this value may change so something like grep for 2014* . However when I do that I get the whole line returned instead of the value 2014.17.10_23.
What would be the best way to achieve this?
Thanks
Using awk:
awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]"
And with sed:
sed -n "/BUILD_VERSION/s/.*'\([^']*\)'.*/\1/p" input
grep 'BUILD_VERSION' <your file> | sed -e 's/var BUILD_VERSION = //g'
Would get you '2014.17.10_23'; tweak the sed expression (or pipe it through a few more) to get rid of quotes.
It would be a 1 liner regex in Perl...
Here is another awk solution:
awk -F' = ' '/BUILD_VERSION/ {gsub(/\x27|;/,""); print $NF}'
You can use this awk
awk -F\' '/BUILD_VERSION/ {print $2}' file
2014.17.10_23

How can I use grep to get the line number without the output? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use grep to report back only line numbers
I only want to see the line number. I don't need to see the remaining output.
Pipe your grep -n output, which normally looks something like:
11: stuff that matched
43: more stuff that matched
through sed to strip out the matching parts:
grep -n pattern file | sed -e 's/:.*//g'
11
43
grep -n or --line-number option will do this for you. You can find this information in the grep help file, which you can find by using grep --help or grep --help | less to read it more carefully. Also consider using the manual page: man grep
You could use awk too.
grep -n word file | awk -F: '{ print $1 }'
As #Barmar pointed out you could just use an awk one-liner as such:
awk '/regex/ { print NR }' file
Since you don't have awk you could also use cut:
grep -n word file | cut -d: -f1

Resources