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

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.

Related

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

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

How to take a text between "/" with Awk / cut? [duplicate]

This question already has answers here:
shell script to extract text from a variable separated by forward slashes
(3 answers)
Closed 4 years ago.
I have this command in a script:
find /home/* -type d -name dev-env 2>&1 | grep -v 'Permiso' >&2 > findPath.txt
this gives me this back:
/home/user/project/dev-env
I need to take the second parameter between "/" (user) to save it later in a variable. I can not find the way to just pick up the "user" text.
Using cut:
echo "/home/user/project/dev-env" | cut -d'/' -f3
Result:
user
This tells cut to use / as the delimiter and return the 3rd field. (The 1st field is blank/empty, the 2nd field is home.)
Using awk:
echo "/home/user/project/dev-env" | awk -F/ '{print $3}'
This tells awk to use / as the field-seperator and print the 3rd field.
Assuming that the path resulting from the grep is always an absolute path:
second_component=$(find .... -type d -name dev-env 2>&1 | grep -v 'Permiso' | cut -d / -f 3)
However, your approach suffers from several other problems:
You use /home/* as starting point for find. This will work only, if there is exactly one subdirectory below /home. Not a very likely scenario.
Even then, it works only if grep results in exactly one line. This is a semantic problem: What if you get more than one line - which one are you interested in? Assume that you know that you are always interested into the first line, you can solve this by piping the result though head -n 1.
Next, you redirect the stderr from find to stdout, which means that any error from find remains unnoticed; you just get some weird result. It would be better to have any error message from find being displayed, and instead evaluate the exit code from find and grep.
... | cut -d/ -f3
"Third field, as cut by slash delimiter"

how to print nth column in a file using awk [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 5 years ago.
How to print nth column in a file using awk?To print the second column of file, I try:
#!/bin/bash
awk '{print $2}' file
But if n is a variable, how to print nth column of file using awk?
#!/bin/bash
n=2
n=2
awk -v var="$n" '{print $var}' file
Give a try this, notice the -v option:
#!/bin/bash
n=3
awk -v x=${n} '{print $x}' file
From the man page:
The option -v followed by var=value is an assignment to be done before
prog is executed; any number of -v options may be present.
For more examples, you could check Using Shell Variables in Programs

Using Variable in awk command [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 6 years ago.
I want to add a word at the end of each line in my text file which is stored in variable. whenever i execute shell script instead of concatenate content stored in variable variable itself get concatenated. Below is the example for same:
Input:
cat output2.txt
12345
att1=Ramesh^Mumbai
awk '{print $0"^$att1"}' output2.txt >output3.txt
output:
12345^att1
Desired Output:
12345^Ramesh^Mumbai
Try this:
awk -v att1='Ramesh^Mumbai' -v OFS='^' '{print $0,att1}'
-v option allows to pass variable to awk
OFS is the output field separator (that will replace the , in the print statement by ^)
man awk:
-v var=val
Assign the value val to the variable var, before execution of
the program begins. Such variable values are available to the
BEGIN block of an AWK program.
you can use this;
#!/bin/bash
att1=Ramesh^Mumbai
awk -v att1=$att1 '{print $0"^"att1}' output.txt > output3.txt
Example;
user#host:/tmp$ cat output.txt
12345
abab
dafadf
adfaf
user#host:/tmp$, ./test.sh
user#host:/tmp$ cat output3.txt
12345^Ramesh^Mumbai
abab^Ramesh^Mumbai
dafadf^Ramesh^Mumbai
adfaf^Ramesh^Mumbai

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

Resources