I have a script that works great but in that script I am using grep to extract two lines
grep -e "string1" -e "string" my_fliles* >> //destination
The output of that looks good
filename1 string1
filename1 string2
filename2 string1
filename2 string2
filename3 string1
filename3 string2
That is the desired effect but in order to get it to work in a cron job I have to specify the full path to my_files
grep -e "string1" -e "string" /home/username/scriptfolder/logs/my_fliles* >> //destination
That creates output like
/home/username/scriptfolder/logs/filename1 string1
/home/username/scriptfolder/logs/filename1 string2
/home/username/scriptfolder/logs/filename2 string1
/home/username/scriptfolder/logs/filename2 string2
/home/username/scriptfolder/logs/filename3 string1
/home/username/scriptfolder/logs/filename3 string2
I want the filenames and the string outputs but not the path. I tried removing the path and specifying the path in the script with
export PATH=$PATH:/home/username/scriptfolder/logs/
but that did not work. anny suggestions?
grep -e "string1" -e "string" ./my_fliles* |
awk -F " " '{ n = split($1,RES,"/"); OUT=RES[n];
for( i=2; i<=NF; i++) { OUT=OUT" "$i } print OUT;}'
In the grep
results, the path and string are separated by a ' '
(space)
/home/username/scriptfolder/logs/filename1 string1
You can use the -F
flag to set the field separator used by awk
, the default separator is space.
This splits the input into $1, $2, . . . , $NF
fields.
awk
has a predefined function split
.
n = split( String, A, [Ere] )
This function splits the string specified by the String
parameter into array elements A[1], A[2], . . ., A[n]
, and returns the value
of the n
variable. The separation is done with the extended regular expression specified by the Ere
parameter or with
the current field separator (the FS
special variable) if the Ere
parameter is not given. The elements in the A array are
created with string values, unless context indicates a particular element should also have a numeric value.
for( i=2; i<=NF; i++) { OUT=OUT" "$i }
will concatenate the original input fields into the variable OUT separated by space. This is to ensure that we do not miss any fields due to spaces in the searched string string1 or string2. Note that the loop started with i=2
The final output will look like below:
filename1 string1
filename1 string2
filename2 string1
PATH
is the search paths to find an executable, not for regular files, so it won't work. Try this in your crontab command:
cd /home/username/scriptfolder/logs; grep -e "string1" -e "string" my_fliles* >> //destination
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With