4

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?

Steve Byrum
  • 57
  • 1
  • 4

4 Answers4

2
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
ashishjv
  • 21
  • 4
1

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
Ye Liu
  • 8,946
  • 1
  • 38
  • 34
0

I am not sure if there is a way to suppress the path if you do grep outside the directory. You can however, use awk to strip the path. Something like:

awk -F/ '{print $NF}' <(grep -e "string1" -e "string" /home/username/scriptfolder/logs/my_fliles*) > destination
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

if you are using grep -e "string1" -e "string" ./my_fliles* >> //destination manually then make a script with these two lines and run that script

cd /home/username/scriptfolder/logs/
grep -e "string1" -e "string" ./my_fliles* >> //destination

if grep -e "string1" -e "string" ./my_fliles* >> //destination is a part of script then add

cd /home/username/scriptfolder/logs/
this line before

grep -e "string1" -e "string" ./my_fliles* >> //destination

Gangadhar
  • 10,248
  • 3
  • 31
  • 50